All topics
Testingbeginner

Snapshot Testing

Understand what snapshot tests capture, their legitimate uses, and why they're easy to misuse as a substitute for real assertions.

A snapshot test renders a component (or serializes some other value) and saves the resulting output to a file; on subsequent test runs, the current output is compared against that saved snapshot, and the test fails if they differ, prompting the developer to either fix an unintended regression or explicitly update the snapshot if the change was intentional. Jest's built-in snapshot testing (toMatchSnapshot()) is the most common implementation in a React testing setup.

Snapshot testing is like keeping a photograph of a room's exact layout and comparing new photos against it to spot if anything's moved — genuinely useful for noticing an unexpected change, but if you get in the habit of just re-taking and accepting a new 'reference' photo every time something looks different without actually checking why, the photo stops meaningfully protecting against anything.

Key Concepts

1
Snapshot tests are legitimately useful for catching unintended changes to a component's rendered output structure — particularly for components whose exact markup matters (like a design system's base components) where any accidental change is worth flagging for review, even ones that don't obviously break functionality.
2
The common misuse pattern is generating and blindly accepting large, whole-component snapshots without actually reading and understanding what they capture, then reflexively running --updateSnapshot whenever a test fails without checking whether the diff represents an intentional change or an actual regression — at that point, the test provides a false sense of coverage without any real verification happening, since a rubber-stamped snapshot update passes regardless of what changed.
--updateSnapshot
3
Interviewers ask candidates to critique an over-reliance on snapshot testing, expecting them to distinguish snapshot tests' legitimate narrow use (catching accidental structural regressions in stable, well-understood output) from the anti-pattern of using them as a lazy substitute for writing specific, meaningful behavioral assertions with tools like Testing Library's queries.