Skip to content

Jest Do's and Don'ts

This lesson distills earlier lessons into direct, side-by-side do's and don'ts you can scan quickly while writing or reviewing Jest tests.

Matchers and Assertions

Choosing the right matcher and writing assertions that reflect real intent is where most day-to-day Jest quality decisions happen. A few consistent habits here prevent a large share of confusing test failures down the line.

// Don't: reference equality on an object literal
expect(result).toBe({ id: 1 });

// Do: deep equality for objects/arrays
expect(result).toEqual({ id: 1 });

This single mistake (.toBe() on a fresh object literal) is one of the most common Jest bugs beginners encounter.

Mocking Do's and Don'ts

// Don't: mock everything, including trivial internal pure functions
jest.mock('./formatCurrency');

// Do: mock only external/slow dependencies
jest.mock('./emailClient');
  • Do mock network calls, databases, timers, and other slow/external dependencies.
  • Don't mock simple, pure, fast internal functions — just test them directly.
  • Do reset mocks between tests (clearAllMocks/resetAllMocks) in afterEach().
  • Don't leave .only() in committed code, silently disabling the rest of a suite.

Jest Do's and Don'ts Cheat Sheet

Quick side-by-side guidance across common Jest topics.

Topic Do Don't
Equality Use .toEqual() for objects/arrays Use .toBe() for objects/arrays
Async await/return promises in tests Leave promises unreturned in a test
Mocking Mock slow/external dependencies Mock trivial pure functions
Snapshots Review every snapshot diff Blindly run jest -u
Test names Describe observable behavior Use vague names like "works"
Timers Use fake timers for delays/intervals Use real setTimeout waits in tests
Test isolation Reset state in beforeEach()/afterEach() Rely on test execution order

Async Testing Do's and Don'ts

Async code is where silent test bugs hide most often, so a few extra do's and don'ts are worth calling out specifically here.

  • Do use async/await as the default pattern for testing promise-based code.
  • Do pair try/catch error assertions with expect.assertions(n).
  • Don't mix done() callbacks with async/await in the same test.
  • Don't assume a green test means an async assertion actually ran.

Snapshot Testing Do's and Don'ts

Snapshots are powerful but easy to misuse if updates become a reflex instead of a reviewed decision.

  • Do keep snapshots small and focused on one component or value.
  • Do normalize non-deterministic fields (timestamps, random IDs) before snapshotting.
  • Don't snapshot an entire page when a smaller, targeted snapshot would do.
  • Don't run jest -u without reading what actually changed first.

Common Mistakes

  • Using .toBe() for object/array comparisons instead of .toEqual().
  • Leaving unreturned promises inside tests, letting assertions run after the test already "passed".
  • Mocking simple pure functions that would be just as easy (and more meaningful) to test directly.
  • Blindly updating snapshots without reviewing the diff for unintended regressions.

Key Takeaways

  • A handful of consistent do's and don'ts prevent the majority of common Jest mistakes.
  • Most "don'ts" trace back to earlier lessons: equality matchers, async handling, and mocking discipline.
  • Reviewing this list periodically, especially with new team members, keeps standards consistent.
  • Automated lint rules can enforce several of these do's and don'ts without manual review.

Pro Tip

Turn this do's-and-don'ts list into a lightweight PR review checklist for test files specifically — it takes seconds to scan and catches the most common mistakes before they merge.