Testing React components · mid
Snapshot testing: when it helps and when it hurts
A snapshot test saves serialised output the first time and fails when it changes, which is cheap protection for small, stable outputs (a formatter, an error message, a config) but becomes noise for large component trees that everyone updates without reading.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- toMatchSnapshot writes a .snap file on the first run; toMatchInlineSnapshot writes it into the test file, which is easier to review.
- The first run always passes, so a wrong value becomes the 'truth' unless someone reviews it. In CI, missing snapshots should fail (Jest --ci).
- Good uses: small serialisable outputs, error messages, generated code, API response mapping, CLI output.
- Bad uses: whole page component trees. Diffs are huge, people press 'u' to update, and the test does not say what behaviour matters.
- Visual regression (Playwright toHaveScreenshot, Vitest toMatchScreenshot, Chromatic) compares pixels, not DOM text; it catches CSS bugs that DOM snapshots miss.
- Playwright ARIA snapshots (toMatchAriaSnapshot) compare the accessibility tree, a middle ground that ignores markup noise.
The red pen
Common traps: mistake → fix
Mistake: "Snapshots are our main test strategy"
Fix: they detect change, not correctness; behaviour assertions say what matters.
Mistake: "Update failing snapshots to make CI green"
Fix: you might be accepting a bug; read the diff first.
Easy to hard
Questions interviewers ask, easy to hard
- What is snapshot testing? Pros and cons?
- When would you use an inline snapshot?
- DOM snapshot vs visual regression vs ARIA snapshot: what does each catch?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.