← Back to React Course | Chapter 12: Testing React Applications | Lesson 8 of 9

Snapshot Testing

Snapshot testing is like taking a photo of your room today and comparing it to a photo from yesterday, so you instantly notice if anything moved without you meaning to.

What a Snapshot Test Does

toMatchSnapshot() renders a component, serializes its output to text, and saves it to a snapshot file the first time the test runs. On every subsequent run, Jest compares the new output against the saved snapshot and fails the test if anything differs.

Note: Snapshot tests are best for catching UNINTENDED changes — they don't tell you WHAT is correct, just whether something changed.

Warning: A first run always passes by creating the snapshot — review the initial snapshot file carefully to confirm it's actually correct.

Example: What a Snapshot Test Does

markup
// Run in your local React project (npm install required)
import { render } from '@testing-library/react';
import Card from './Card';

test('Card renders correctly', () => {
  const { container } = render(<Card title="Hello" />);
  expect(container).toMatchSnapshot();
});

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Reviewing a Snapshot Diff

When a snapshot test fails, Jest shows a diff between the saved snapshot and the new output. You must read this diff and decide: was this change intentional (in which case you update the snapshot) or a real bug (in which case you fix the component instead)?

Note: Treat a failing snapshot the same way you'd treat a failing code review comment — investigate before accepting the change.

Warning: Blindly running the 'update snapshots' command whenever a test fails defeats the entire purpose — it would silently accept real bugs as the new expected output.

Example: Reading and Updating a Snapshot Diff

bash
$ npx jest
 FAIL  src/Greeting.test.js
  ✕ renders a greeting (12 ms)

  - Snapshot
  + Received

  - <h1>Hello, Guest</h1>
  + <h1>Hello, World</h1>

# after confirming the change is intentional:
$ npx jest -u
 PASS  src/Greeting.test.js
 1 snapshot updated.

⚠️ Run this command in your terminal.

When to Use (and Avoid) Snapshots

Snapshots work best for small, stable components where any change is meaningful and easy to review. For large, frequently-changing components, snapshots tend to produce huge diffs that are hard to review carefully, making them less useful than targeted assertions about specific behavior.

Note: Prefer specific assertions (like screen.getByText(...)) for behavior you care about, and reserve snapshots for smaller, more stable presentational components.

Warning: A giant snapshot of a complex page makes even a tiny, correct change produce an overwhelming diff that's tempting to approve without really reading.

Example: When to Use (and Avoid) Snapshots

markup
// Run in your local React project (npm install required)
// Good snapshot candidate: small, stable
test('Icon renders', () => {
  const { container } = render(<Icon name="star" />);
  expect(container).toMatchSnapshot();
});
// Less ideal: a whole complex Dashboard page — prefer targeted assertions there instead
Common Mistakes
  1. Blindly running 'update snapshots' whenever a test fails, without actually checking whether the change was intentional.
  2. Snapshotting huge, complex component trees where a tiny unrelated change produces a massive, unreadable diff.
  3. Committing a stale snapshot file that no longer matches intentional recent changes.
Chapter Summary
  • Snapshot tests save a serialized representation of rendered output and compare future runs against it.
  • toMatchSnapshot() creates the snapshot file on first run, then compares against it on subsequent runs.
  • A failing snapshot test means the output changed — you must confirm if that change was intentional.
  • Snapshot files should be reviewed like code changes, not blindly regenerated.
Browser Support

Requires npm install jest (built-in snapshot support) — runs in Node, not this browser sandbox.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.