All topics
Testingintermediate

Snapshot Testing

Capturing a serialized representation of a value or rendered output and comparing future test runs against that saved baseline, flagging any unexpected differences.

Snapshot testing is a testing technique where, instead of writing out explicit assertions describing every expected property of a complex output, you capture a serialized snapshot of the output the first time a test runs, save it as a baseline file, and on every subsequent test run, automatically compare the current output against that saved snapshot — flagging a failure if anything differs, prompting you to review whether the difference is an intentional change (in which case you update the snapshot) or an actual regression (in which case you fix the code).

Snapshot testing is like a security system that photographs a room's exact layout once and, on every subsequent check, compares a fresh photo against that saved baseline, immediately flagging anything that's moved — but the system is only useful if a human genuinely looks at the flagged differences and decides whether they're expected (someone rearranged furniture on purpose) versus a real problem, rather than reflexively approving every alert without looking.

Key Concepts

1
This is especially popular for testing UI component output (like React component rendering) where writing exhaustive manual assertions about every single rendered DOM node, attribute, and text value would be extremely verbose, but a diff against a previously-approved snapshot can catch any unintended change to the rendered structure immediately and holistically, without needing every individual detail to be manually asserted on up front.
2
The practical workflow: the first time a snapshot test runs, there's no existing baseline, so the framework (Jest's built-in snapshot testing being the most common) simply saves the current output as the new baseline and passes; every subsequent run compares the fresh output against that saved file, and a mismatch fails the test with a readable diff showing exactly what changed. When a change is genuinely intentional (you deliberately updated a component's rendered markup), you explicitly re-run tests with an 'update snapshots' flag, which overwrites the old baseline with the new output — and this update step should always be a deliberate, reviewed action (checking the diff in your code review, not blindly accepting every failure), since blindly updating snapshots without actually reviewing what changed defeats the entire purpose of the technique.
3
The well-known criticism of snapshot testing is exactly this last point: because updating a snapshot is often just running one command, it's easy for a snapshot test to devolve into a rubber-stamp that gets blindly updated whenever it fails, rather than genuinely scrutinized, at which point it stops providing any real regression protection at all — it's really only valuable when developers treat a snapshot diff with the same scrutiny as a genuine code review, actually reading what changed rather than reflexively re-approving it.