Snapshot testing
Snapshot testing is automated testing that records the output of a component or function, stores it as a reference file beside the test, and fails when a later run produces different output. In Jest the snapshot is serialised text, such as rendered markup, so it catches changes to structure and content and never sees pixels, CSS or layout.
The longer version
Jest's documentation (version 30.5) introduces snapshot tests as a very useful tool whenever you want to make sure your UI does not change unexpectedly, and describes the typical one: it renders a UI component, takes a snapshot, then compares it to a reference snapshot file stored alongside the test. In code that is one line, expect(tree).toMatchSnapshot(). The first run writes the reference into a .snap file and passes. Every later run compares the new output with the stored text and fails on any difference, which Jest reads in one of two ways: the change was unexpected, or the reference needs updating to the new version. Updating is jest --updateSnapshot, or -u, which re-records every snapshot that fails in that run and can be limited with a test pattern or --testNamePattern. In watch mode Jest offers an interactive mode that steps through failed snapshots one test at a time. Inline snapshots, written with toMatchInlineSnapshot(), store the reference in the test file itself. Generated values such as ids and dates would fail every run, so Jest accepts property matchers like expect.any(Date) in their place. The docs ask for snapshot files to be committed and reviewed as part of normal code review, and since Jest 20 snapshots are not written automatically on CI unless --updateSnapshot is passed; the --ci flag makes a missing snapshot fail the test instead of being stored. Jest also notes that snapshots can capture any serialisable value, so the technique is wider than React components.
What a snapshot can see is what was written into it. A snapshot of a rendered button records the element, its attributes, its class names and its text. A change to the stylesheet, such as a new colour, a larger margin, a missing font or a z-index that puts a cookie banner over the button, alters none of those lines, so every snapshot passes. Component tests usually render into jsdom, and the jsdom README says plainly that jsdom still does not do any layout or rendering, returning zeros for many layout properties. Overflow, clipped text, overlapping elements and a layout broken at one phone width are therefore invisible to a DOM snapshot by construction. The opposite error happens too: moving a wrapper element or reordering class names fails the snapshot while the page looks identical. A snapshot also detects change and says nothing about correctness. Its reference is whatever the code produced the first time it ran, so a bug present on that day is recorded as the expected result.
Snapshot testing and visual regression testing are often confused because both compare against a stored baseline. Jest's FAQ draws the line: visual regression tools take screenshots of web pages and compare the images pixel by pixel, while snapshot testing stores values in text files and compares them with a diff algorithm. Playwright offers both. expect(page).toHaveScreenshot() generates reference screenshots on its first run and compares later ones with the pixelmatch library, within a configurable threshold, and npx playwright test --update-snapshots refreshes them; its docs warn that browser rendering can vary with the host operating system, version, settings and hardware, so baselines should be generated in the same environment the tests run in. Its toMatchSnapshot() compares text or binary values. On iOS the word usually means images: Ash Furrow's 2018 account of snapshot testing at Artsy describes screenshots of the interface stored in the repository as reference images, and names ios-snapshot-test-case, a framework Facebook created and Uber now maintains, as the tool that makes it easy. So the first question about any snapshot suite is whether it compares text or pixels. Neither replaces explicit assertions, and Jest's own FAQ says the aim of snapshot testing is not to replace existing unit tests.
The weak point is the update. When a large snapshot fails after a change somebody meant to make, running -u is quicker than reading the diff, and after a few weeks of that the suite approves whatever the code does. Stefano Magni, writing on DEV in July 2022, describes snapshot changes being accepted without a proper understanding of them, which leaves the snapshot useless. A regression approved that way becomes the new reference and protects itself from then on. The habits that prevent it are the ones the Jest docs point towards and a few more: keep each snapshot small and about one component, replace dynamic values with property matchers, run CI with --ci so a pipeline never writes a snapshot, read .snap diffs in the pull request as carefully as code, update one named test at a time instead of the whole suite, and delete snapshots nobody reads. Where the risk is visual, the check has to run in a browser, through a screenshot comparison or a person looking at the change on a real screen.
Common questions
What is snapshot testing?
Snapshot testing is a kind of automated test that saves a component's or function's output the first time it runs and compares every later run against that saved copy. Any difference fails the test, and a developer either fixes the code or accepts the new output as the reference. Jest made the technique common in JavaScript.
How do you update Jest snapshots?
Run jest --updateSnapshot, or jest -u, to re-record every snapshot that fails in that run, and add a test file pattern or --testNamePattern to limit it to the snapshots you meant to change. In watch mode, press the key Jest shows for interactive snapshot mode to review and accept failed snapshots one test at a time.
How do you do snapshot testing?
Render the component or call the function, pass the result to expect(...).toMatchSnapshot(), and run the test once to create the .snap file. Commit that file with the test. From then on the test fails whenever the output changes, and the diff in the failure shows exactly which lines moved.
What is the purpose of a snapshot test?
A snapshot test exists to make an unintended change to output visible, cheaply. Writing explicit assertions for every element of a large component takes time; one snapshot records all of it. The cost is that the test checks for change only, so a person still has to judge whether each change is correct.
Does snapshot testing catch CSS or layout changes?
A Jest snapshot of rendered markup does not. The snapshot holds elements, attributes, class names and text, and jsdom, where such tests usually run, does no layout or rendering. A change to a stylesheet, a font or a breakpoint passes every snapshot. Catching it takes a screenshot comparison or a person looking at the page.
What is the difference between snapshot testing and visual regression testing?
Snapshot testing compares serialised text, such as markup or JSON, with a diff. Visual regression testing takes screenshots in a browser and compares the images pixel by pixel. Jest's documentation describes them as two distinct ways of testing user interfaces with different purposes, and many teams run both.
How does Playwright use screenshot testing?
Playwright's expect(page).toHaveScreenshot() saves reference screenshots on the first run and compares later runs against them with the pixelmatch library, within a threshold you can set. Running npx playwright test --update-snapshots refreshes the references. Playwright advises generating them in the same environment the tests run in, because rendering varies by operating system and hardware.
Should snapshot files be committed to version control?
Jest's documentation says all snapshot files should be committed alongside the modules and tests they cover, and treated as part of the test. A snapshot that is not committed is regenerated on the next machine and compares the code with itself, so it can never fail.
Bug reports that arrive ready to triage.
Collect replay, console, network and browser context with the Free plan.