Snapshot Testing
In one line
Snapshot testing saves a 'snapshot' of output on first run, then compares future runs against it — flagging any changes.
In simple words
Snapshot testing is like taking a photo of your output and comparing future photos to it. On the first test run, Jest saves the output to a snapshot file. On every subsequent run, it compares the current output to the saved snapshot.
If they match, the test passes. If they differ, the test fails and shows you the exact diff — what changed. You then decide: is this change intentional (update the snapshot) or a bug (fix the code)?
Snapshot testing is especially useful for:
- UI component rendered output
- Large configuration objects
- Serialized data structures
The danger: it's easy to blindly update snapshots with --updateSnapshot without reviewing what changed. Always review the diff!
Example
test('button renders correctly', () => {
const { container } = render(<Button label="Click" />);
expect(container).toMatchSnapshot();
});
// First run: creates .snap file
// Next runs: compares against .snap fileFirst run saves the rendered HTML; subsequent runs compare against it.
Common confusions
Confused with: Regular Assertion
The difference: Regular assertions check specific values; snapshots capture entire output and detect any change, expected or not.
Related terms
Related Resources
Related Lessons
- Snapshot Testing
Using snapshots effectively.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.