Jest · Unit tests that survive refactors
What are the pitfalls of snapshot testing?
Short answer
Large snapshots are hard to review, fail on irrelevant changes and get updated without thought. Keep them small, prefer targeted assertions, and delete snapshots that no longer protect a real contract.
Snapshots are easy to create and easy to ignore. The failure mode is a huge diff that a developer accepts because the update is faster than understanding it. At that point the snapshot still passes but no longer expresses intent.
The usual problems
- A whole page snapshot changes whenever any child changes
- Generated class names, timestamps or IDs create noisy diffs
- One large snapshot hides which specific behaviour regressed
- Reviewers approve an updated snapshot without reading it
- Obsolete snapshots remain after the component changes
test("shows the total", () => {
render(<Invoice total={120} />);
expect(screen.getByTestId("total")).toHaveTextContent("$120.00");
});Keep snapshots narrow: serialize one small component or one payload, not a whole route. Use property matchers or a custom serializer to replace unstable values. Prefer inline snapshots for short output, because they sit next to the test and are harder to update unnoticed.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How do you keep a snapshot small?
- 2How do you handle unstable values in a snapshot?
- 3When should you delete a snapshot?
- 4Why is an inline snapshot easier to review?
Say it in the interview
The main pitfall is size. A large snapshot becomes noise that people update without reading, so it stops protecting anything. I keep snapshots narrow, remove unstable values with serializers, and prefer explicit assertions for the contract. If a snapshot changes constantly for cosmetic reasons, I replace it with a behavioural assertion.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Jest mock interview and get scored feedback on depth, clarity and confidence.