Snapshot testing captures the serialised output of a component or function and saves it as a reference. Future test runs compare output against the saved snapshot — any difference fails the test. Best for: UI component regression (React component markup), serialisation format verification, and large generated data structures. Snaps are fast write, but require discipline: approve only intentional changes, keep snapshots small and readable, avoid snapshotting random/time-dependent output.
Snapshot anti-patterns and best practices.
A 500-line snapshot diff is unusable — developers approve it without reading. Keep component snapshots under 50 lines by testing leaf components, not full page trees. Inline snapshots (toMatchInlineSnapshot) keep tests self-contained.
The most common snapshot anti-pattern: developer runs 'jest -u' to silence failures without reviewing the diff. This turns snapshot tests into rubber-stamps. Add a PR policy requiring snapshot diffs to be explained.
Snapshot tests catch 'something changed' but don't tell you if the something was correct. Combine with explicit assertions: snapshot the component structure AND assert specific props. The snapshot catches regressions, the assertion defines correctness.
Sign in to share your feedback and join the discussion.