Skip to content

Snapshot mismatch: UI or output changed

Quick summary

Your snapshot test fails — the output changed from the stored snapshot, which may be an intentional change or a regression.

Why this happens

  • The component or function output changed intentionally and the snapshot needs updating

  • A code change introduced an unintended regression that altered the output

  • The snapshot includes volatile data like timestamps or random IDs that change between runs

Minimal example

✗ Broken code
test('renders greeting', () => {
  const { container } = render(<Greeting name="Alice" />);
  expect(container).toMatchSnapshot(); // fails if output changed
});
✓ Fixed code
test('renders greeting', () => {
  const { container } = render(<Greeting name="Alice" />);
  expect(container).toMatchInlineSnapshot(`
    <div>
      <h1>Hello, Alice!</h1>
    </div>
  `);
});

Using inline snapshots makes the expected output visible in the test file, making it easier to review and maintain.

How to diagnose

  • Review the diff between the actual output and the stored snapshot

  • Determine if the change is intentional (update snapshot) or a regression (fix code)

  • Check if the snapshot includes non-deterministic values that should be mocked or stripped

How to fix

  • If the change is intentional, update the snapshot with the update flag or command

  • If the change is a regression, fix the code to produce the expected output

  • Mock or strip volatile values like dates and IDs before snapshot comparison

How to prevent

  • Review snapshot diffs carefully in code review before accepting updates

  • Avoid including non-deterministic values in snapshots — mock dates, IDs, and random values

Related Resources

Related Glossary

Related Lessons

← Back to language