Skip to content

Jest Snapshot Test Failing Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Jest Snapshot Test Failing Fix. We cover key concepts, practical examples, and best practices.

Your snapshot test fails after a UI change: Snapshot mismatch — Jest compares the new render output against the stored snapshot file and found a difference.

The Problem

// WRONG — snapshot includes dynamic data
test('renders user profile', () => {
  const { container } = render(<UserProfile user={{ id: Date.now(), name: 'Alice' }} />);
  expect(container).toMatchSnapshot();
});
Received value does not match stored snapshot "renders user profile 1".

- Snapshot
+ Received

-      "id": 1719212345678,
+      "id": 1719256789012,

Every run generates a new id, causing the snapshot to change every time. The test is not useful — it fails constantly without detecting real regressions.

Step-by-Step Fix

1. Mock dynamic values

// RIGHT — mock Date.now for deterministic snapshots
beforeEach(() => {
  jest.spyOn(Date, 'now').mockReturnValue(1234567890000);
});

afterEach(() => {
  jest.restoreAllMocks();
});

test('renders user profile', () => {
  const { container } = render(<UserProfile user={{ id: Date.now(), name: 'Alice' }} />);
  expect(container).toMatchSnapshot();
});

2. Use property matchers

// RIGHT — use asymmetric matchers
test('renders user profile', () => {
  const { container } = render(<UserProfile user={{ id: 42, name: 'Alice', createdAt: new Date() }} />);

  expect(container).toMatchSnapshot({
    user: {
      id: expect.any(Number),
      createdAt: expect.any(Date)
    }
  });
});

Property matchers tell Jest to check the type rather than the exact value for dynamic fields.

3. Update snapshots intentionally

# Update all snapshots
npx jest --updateSnapshot

# Update specific file
npx jest --updateSnapshot --testPathPattern=UserProfile

# Interactive mode
npx jest --watch
# Press 'u' to update, 'i' to update individually

4. Review snapshot diffs before updating

npx jest --verbose

Read the diff output carefully. A snapshot change should match the expected UI change. If you see unexpected differences, investigate the component.

Expected output:

Snapshot: 1 updated, 3 passed
Tests:    4 passed

Prevention Tips

  • Mock Date.now(), Math.random(), and uuid in snapshot tests
  • Use expect.any(Number) and expect.any(Date) for dynamic fields
  • Review snapshot diffs in PRs
  • Keep snapshots small
  • Use toMatchInlineSnapshot() for small snapshots

Common Mistakes with snapshot update

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world JEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Should I update snapshots every time they fail?

No. Read the diff first. If the change matches your intentional UI modification, update. If the diff shows unexpected changes, investigate — a snapshot failure could reveal a regression.

How do I handle Jest snapshots in CI?

Run tests with --ci in CI. Add --updateSnapshot only when you intentionally updated snapshots in a PR. Pair snapshot review with code review.

What's the difference between toMatchSnapshot and toMatchInlineSnapshot?

toMatchSnapshot writes to a .snap file in __snapshots__ directory. toMatchInlineSnapshot writes the snapshot directly into the test file. Use inline for small output, file-based for larger trees.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro