Skip to content

Lesson 8 of 14 · 14m

Snapshot Tests: Diff Detectors, Not Oracles

A snapshot pins what the code produced last time, not what's correct — earn their keep on serialized payloads and codegen output, and review `-u` diffs like PR diffs.

expect(value).toMatchSnapshot() does exactly one thing: serializes the value, writes it to __snapshots__/test.snap on the first run, and diffs against that file on every run after. The snapshot is not the correct output — it's the output the code produced the day the file was written. Treating it as an oracle is the most common way a suite rots while staying green.

The rubber-stamp failure

The failure mode is mechanical: CI goes red, the developer runs jest -u, gets a 200-line diff they don't read, commits, and the suite is green. The bug the snapshot just caught is now the blessed output. The suite detected the change and approved it anyway. Nothing failed except the review.

This scales badly. A codebase with thousands of snapshot assertions turns every routine dependency bump into a wall of red that gets resolved with muscle-memory -u. Nobody reads a 4,000-line diff. The suite didn't catch regressions — it manufactured approval fatigue.

One process rule makes this survivable: snapshot updates go in their own commit, separated from the code change that caused them. A reviewer scanning .snap diffs inside a 40-file feature diff reads none of them; the diff nobody can review is the bug everybody approves.

Jest's --ci flag exists for exactly this: in CI, new snapshots fail instead of silently writing, so "the snapshot didn't exist" is loud. Turn it on.

For small values, toMatchInlineSnapshot() keeps the expected output inside the test file where a reviewer actually sees it:

expect(formatPrice(order)).toMatchInlineSnapshot(`"$135.00 USD"`);

Configure the serializer — stable key ordering, a pretty-printer — because the snapshot's only value is a readable diff.

Where snapshots earn

Serialized outputs where the exact diff is the review:

  • API response payloads — the shape consumers depend on.
  • Codegen and AST dumps — the compiler's output, pinned.
  • CLI --help text, SVG output, migration SQL — artifacts where "what changed" is the whole question.

Rust's insta does it best: inline snapshots (assert_snapshot!) live next to the assertion in the source file, and cargo insta review walks each changed hunk in a TUI — accept or reject, like a code review. Go's convention is testdata/golden/ files plus a hand-wired -update flag; the standard library uses it for parser and format output. Both make the diff reviewable, which is the entire point.

Most suites end up needing a custom serializer for the nondeterministic fields — addSnapshotSerializer masking timestamps and generated IDs — because the alternative is teaching every reviewer which lines to ignore, which is exactly how the 4,000-line diff gets approved.

Where they rot

  • React DOM trees. A 90-line HTML snapshot is an unreadable diff; the meaningful assertion — "the error banner appears" — is buried in markup noise. Testing Library's role-based queries (getByRole("alert")) read better and break less.
  • Nondeterministic values. A snapshot containing created_at: "2026-11-16T..." fails every run. Inject the clock and the ID generator first.
  • Everything-as-snapshot. toMatchSnapshot() on every test because it's easy leaves a pile of diffs nobody reviews.
  • Orphans. Deleting a test leaves its snapshot entry behind; Jest cleans them on -u, and cargo insta test --unreferenced reject fails the build on orphaned files — worth wiring into CI so the snapshot directory doesn't accumulate ghosts.

Snapshots are also the cheap version of characterization testing: assert_snapshot! against a legacy endpoint's current response pins today's behavior before you refactor it — the same golden-master move from the earlier lesson, minus the fixture plumbing.

Field notes

  • Review -u diffs like PR diffs. If you can't explain every changed line, the snapshot is noise — split it or write a real assertion.
  • A stable key ordering and a pretty-printer keep diffs readable; configure the serializer, don't accept the default blob.
  • toMatchInlineSnapshot keeps small values in the test file where they're reviewable; big blobs go in the snapshot file.
  • Snapshots are a layer, not a strategy — they complement assertions about behavior, they don't replace them.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.