Snapshot tests without the noise: keep them small and reviewed
Short answer: Snapshot tests save the output of a component or function, such as rendered HTML or a JSON response, and fail when it changes. They're quick to write and catch unexpected changes, but they turn into noise when they're huge, full of changing data, or approved with -u without anyone reading the diff. Keep snapshots small and focused, replace generated values like dates and IDs with matchers, review snapshot changes like code, and use ordinary assertions for behaviour that really matters.
What snapshot tests are for
A snapshot test runs some code, serialises the result and compares it with a saved copy. The first run writes the snapshot; later runs fail if the output differs. Jest and Vitest, two popular JavaScript test runners, both support this, and similar tools exist for other languages.
They're good at answering "did anything change that I didn't expect?" for things like:
- A component's rendered markup, such as a price card or an order summary.
- An email template's HTML.
- A serialised API response or configuration object.
- Formatted output, such as a generated invoice line or a CSV-free plain-text receipt.
Jest's documentation describes the intended workflow: the snapshot file is committed alongside code changes and reviewed as part of code review, and it's formatted to be readable for that purpose.
Why snapshot suites go wrong
Teams often add snapshots enthusiastically and then stop trusting them. The usual reasons:
- Snapshots of entire pages. A thousand-line snapshot changes with every small edit. Nobody reads the diff; everybody runs the update command.
- Changing values inside. Timestamps, random IDs and today's date make the snapshot differ every run.
- Rubber-stamp updates. Jest's
--updateSnapshot(or-u) regenerates snapshots. Used blindly, it turns a failing test into a passing one without checking anything. - Testing implementation, not behaviour. A snapshot of internal markup fails when a developer swaps a
divfor asection, even though users see no difference.
When this happens, snapshot failures become a chore and real regressions get approved along with the noise.
Making snapshots useful
Keep them small. Snapshot one component in a defined state, not a whole page. If a snapshot is longer than a screen, it's probably too big to review.
Snapshot the important part. Rather than the whole object, select the field or element you care about, such as the formatted total or the list of line items.
Handle generated values. Jest's property matchers let you say that a field should be "any string" or "any Date" instead of a fixed value, which suits IDs and timestamps. For components that read the clock, Jest's docs suggest mocking Date.now() so it returns a constant.
Use inline snapshots for short output. Both Jest and Vitest support inline snapshots, written straight into the test file, so the expected output sits next to the code that produces it. Reviewers see it without opening a separate file.
Use file snapshots for readable formats. Vitest's toMatchFileSnapshot() writes the snapshot to a file with any extension, such as .html, so editors and code review tools can display it properly.
Reviewing snapshot changes
Treat a changed snapshot like a changed line of code:
- When a snapshot test fails, read the diff before updating.
- Ask whether the change was intended by this commit.
- Update only the snapshots you've checked, by running the update for a single test file or using the runner's interactive mode.
- In pull requests, review
.snapfile changes with the same care as source changes. If a diff is too long to review, that's a sign the snapshot is too big.
Some teams add a rule to their CI that fails a build if snapshots are written for the first time in CI, so missing snapshots are noticed rather than silently created.
When to use something else
Snapshots aren't a substitute for clear assertions. Prefer explicit checks when:
- A rule matters. "The discount never takes the total below zero" deserves its own assertion, not a line buried in a snapshot.
- Users interact. For "clicking Add to basket updates the count", test the behaviour with your UI testing library.
- Appearance matters. Snapshot tests compare text output, not pixels. Visual regression tools compare screenshots for layout and styling changes.
A healthy suite often has a few small snapshots for structure, many plain assertions for rules, and a handful of browser tests for key journeys.
Naming and organising snapshots
Good names make failures easier to read. Give each test a description that says what state it captures, such as "order summary with a discount code applied", rather than "renders correctly". When a snapshot fails, the name tells the reviewer what should have stayed the same. Keep snapshot files next to the tests that create them, which both Jest and Vitest do by default, so moving or deleting a component takes its snapshots along with it.
Cleaning up an existing suite
If your snapshots have already become noise:
- Delete obsolete snapshots; Jest reports and can remove them.
- Replace the largest snapshots with targeted assertions or smaller snapshots.
- Add matchers for dates and IDs.
- Agree as a team that
-uwithout reading the diff isn't acceptable in reviews.
A worked example
Picture a hypothetical Toronto meal-ordering startup whose React app had hundreds of full-page snapshots. Every design tweak broke dozens, and developers ran the update command by reflex. When a pull request accidentally removed the delivery fee line from the checkout summary, the change was approved inside a long snapshot diff. Afterwards, the team replaced full-page snapshots with small component snapshots, added explicit assertions for fees and totals, mocked the clock, and asked reviewers to open every changed .snap file.
FAQ
Should snapshot files be committed?
Yes. They're the expected output, so they belong in version control with the code.
Are snapshot tests bad practice?
No, but they're easy to misuse. Small, reviewed snapshots are useful; giant ones are mostly noise.
Can I snapshot API responses?
Yes, with matchers for IDs and timestamps. For public APIs, consider contract tests as well.
Do snapshots catch styling bugs?
Only if styles appear in the serialised output. For how things actually look, use screenshot-based visual testing.
If your test suite fails for the wrong reasons and passes for the wrong reasons, tell us how it's set up and we'll help you clean it up.