Snapshot Tests
The 400-line diff nobody reads before pressing update
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.
// 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();
});// Say what matters. A diff here is three lines and means something.
it('prices and labels the product', async () => {
const res = await getProduct('sku-481');
expect({
price: res.price,
currency: res.currency,
addToBasket: res.accessibility.addToBasketLabel,
}).toMatchInlineSnapshot();
}); 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Testing AI features: evals as tests, and tests written by AISoftware Testing and Quality
- What not to test: the tests that cost more than they protectSoftware Testing and Quality