Snapshot Tests

The 400-line diff nobody reads before pressing update

L112 MINTESTING
PREDICT

A 412-line snapshot of an API response fails on most pull requests, mostly because of timestamps and key order. This week a price bug fails it too. What does the team most likely do?

A snapshot test stores a serialised copy of some output and fails when the output changes. It cannot tell a change you meant from one you did not, so a human has to decide, and the decision is only as good as the diff. A large snapshot records everything, including noise, so it fails often and the diff is unreadable. That is the same trap as the sleep in eventual-consistency assertions: a test that is red for no reason teaches people to ignore red.

The fix is to snapshot only what matters, or to assert it explicitly, and to treat any snapshot diff as a review item.

▶ SIMULATION
TEST
snapshot: 412 lines, every field
noise fails it: 0 time(s)
CHANGES THIS WEEK
none yet
PRODUCTION
price: 19.99 GBP
aria-label: Add Trail Runner to basket
last—
✓ the build is red exactly when something that matters changed✓ no regression in price or accessibility reaches production
SCHEDULE
01[TEST]test: snapshot the whole response (412 lines)
02[DEV]change: updatedAt reads 09:41
03[REVIEW]red build: run update snapshots
04[DEV]change: refactor reorders the keys
05[REVIEW]red build: run update snapshots
06[DEV]change: price 1999 becomes 199
07[REVIEW]red build, the same as the last two: update snapshots
08[DEV]change: the aria-label disappears
09[REVIEW]red build: update snapshots
10[TEAM]report
Start of break: 412-line snapshot scenario. 10 steps.
// 412 lines, recorded once, updated with `jest -u` whenever it goes red.
it('renders the product', async () => {
  const res = await getProduct('sku-481');
  expect(res).toMatchSnapshot();
});

Watch the PRODUCTION column and the build colour. In the 412-line run, a price of 1.99 and a missing label ship behind builds that were told to update. In the targeted run, noise stays green and each real regression gets a short diff that a person can read.

BREAK IT NOT YET

The whole response is snapshotted and the reviewer updates the snapshot every time it goes red. Insert one change so that a real regression reaches production behind a green build.

GOAL: a price or accessibility regression ships

FIX IT NOT YET

Write snapshotOf(res), which returns the string the test stores. It must stay the same for noise: new updatedAt or requestId values, reordered keys or tags, and new unrelated fields. It must change for anything a customer sees or relies on: the name, the price and currency, availability, the add-to-basket accessibility label, and the price of every variant. The grader tries each noise change and each regression separately.

Mental model:

A snapshot is only as good as the diff a human can read. Record the few fields a user or caller depends on, leave noise out so a red build always means something, and treat updating a snapshot as a change to the specification that needs a reason in the pull request. Where you can name the value, assert it.

IN PRODUCTION

Jest toMatchSnapshot on React components, API response snapshots, golden files for compilers and renderers, approval tests, visual regression screenshots. Snapshots suit output that is large and stable, such as an error message or a generated config, and they suit it best when the diff is small. Related: eventual-consistency assertions, another way a green or red build stops meaning what the team thinks.

IN INTERVIEWS

“Your team has snapshot tests and keeps updating them. How do you stop regressions slipping through?” Shrink snapshots to what matters, use explicit assertions where you can name the value, filter or mask volatile fields, keep inline snapshots small enough to read in the pull request, and require a reviewer to read every snapshot diff rather than accept an update.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: TEST SELECTION →