Snapshot Testing and Its Pitfalls
Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.
What you will be able to do
#- Explain what a snapshot test captures and when it is useful.
- Recognize when a snapshot has become a rubber stamp.
- Prefer explicit assertions for behavior and snapshots for structure.
Overview
#What a snapshot is
A snapshot test records the output of a function or component on the first run and stores it in a file. On later runs it compares the new output to the stored one and fails if they differ. It is a change detector: it tells you the output changed, not whether the change is correct.
When snapshots help
Snapshots are useful for large, stable, structural output that is tedious to assert field by field: a serialized configuration, a rendered tree with many nodes, or a generated document. They are a fast way to notice an unexpected change.
When they hurt
A snapshot becomes a rubber stamp when a failing test is 'fixed' by updating the snapshot without reading the diff. At that point the test asserts nothing; it only records whatever the code currently does, including a bug. Large snapshots are also unreadable, so reviewers approve them blindly.
Snapshots do not check behavior
A snapshot captures the current output. It does not check that the output is correct. If the component renders the wrong label, the snapshot records the wrong label and passes forever after. Behavior needs explicit assertions on values the test author chose.
Keep snapshots small and intentional
Prefer small, focused snapshots over one giant tree. Assert the important values explicitly and snapshot only the part that is genuinely structural. If a snapshot is hundreds of lines, split the component or assert on specific parts instead.
Review snapshot diffs like code
A snapshot change is a code change. Read the diff, decide whether the new output is correct, and only then update. An unexplained snapshot update is a defect escaping review.
Inline snapshots
An inline snapshot lives in the test file next to the assertion. It is easier to review because the expected value is visible in context. Prefer inline snapshots for small outputs and external files only for large ones.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A component's label changes from 'Save' to 'Submit'. The snapshot was recorded earlier. What happens?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascript// snapshot stored: <button>Save</button> // current render: <button>Submit</button>Exercise 2Explain
Explain why a snapshot test can pass while the component is broken.
Reveal the reference answer
reference.javascriptA snapshot records whatever the component currently renders. If the component renders a wrong value, the snapshot records that wrong value and passes on every subsequent run, because the output still matches the stored snapshot. The test only detects change, not correctness. Explicit assertions on chosen values are needed to catch a wrong-but-stable output.The answer should distinguish change detection from correctness checking and recommend explicit assertions for behavior.
Exercise 3Implement
Implement `matchesSnapshot(value, snapshot)` that returns true when the JSON serialization of value equals the snapshot string. `console.log(matchesSnapshot({ a: 1 }, JSON.stringify({ a: 1 })))` should print `true`.
starter.javascriptfunction matchesSnapshot(value, snapshot) { // your code here }Reveal the reference answer
reference.javascriptfunction matchesSnapshot(value, snapshot) { return JSON.stringify(value) === snapshot; }Authored expected output: true
Serializing the value and comparing to the stored string is the core of a snapshot check. Key order matters in JSON.stringify, which is one reason snapshots are sensitive to unrelated changes.
Exercise 4Debug
This test was 'fixed' by updating the snapshot, but the component now renders the wrong value. Replace the snapshot with an explicit assertion that catches the defect.
starter.javascriptconst rendered = '<span>0 items</span>'; const snapshot = '<span>0 items</span>'; console.log(rendered === snapshot ? 'pass' : 'fail');Reveal the reference answer
reference.javascriptconst rendered = '<span>0 items</span>'; const expected = '<span>2 items</span>'; if (rendered === expected) { console.log('pass'); } else { throw new Error('expected ' + expected + ' but got ' + rendered); }Authored expected output: Error: expected <span>2 items</span> but got <span>0 items</span>
The snapshot matches the buggy output, so it passes and hides the defect. An explicit expected value chosen by the author, 2 items, fails against the wrong render and names the difference. The snapshot rubber-stamped the bug.
Exercise 5Test
Write an explicit assertion test for a task item that proves the title and status render, instead of snapshotting the whole tree.
starter.javascriptfunction renderTask(task) { return '<li>' + task.title + ' [' + task.status + ']</li>'; }Reveal the reference answer
reference.javascriptfunction assertEqual(actual, expected, label) { if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual); } const html = renderTask({ title: 'Write tests', status: 'todo' }); assertEqual(html.includes('Write tests'), true, 'title rendered'); assertEqual(html.includes('[todo]'), true, 'status rendered'); console.log('task render verified');Authored expected output: task render verified
Two explicit assertions check the values that matter. Unlike a snapshot, they fail if the title or status is wrong, and they document exactly what the render must contain.
Exercise 6Interview
In an interview, explain your policy for snapshot tests in a team.
Reveal the reference answer
reference.javascriptI allow snapshots only for large, stable structural output where explicit assertions would be tedious, and I keep them small. I require that any snapshot update is reviewed as a code change with the diff read, and I reject updates that are not explained. For behavior I insist on explicit assertions on chosen values, because snapshots only detect change, not correctness. If a snapshot is being updated on every commit, it is a rubber stamp and should be replaced.The answer should scope snapshots to structural output, require diff review, and prefer explicit assertions for behavior.
Common misconceptions
#- Updating a snapshot is always the correct fix for a failing snapshot test.
- Snapshots verify that the output is correct.
- A large snapshot is more thorough than explicit assertions.
Follow-up practice
#- testing-visual-regression
- testing-test-strategy
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.