Jest · Unit tests that survive refactors

What are the best practices for a healthy Jest suite?

Hardbest practicesstrategytest suiteUpdated Oct 2026
Quiz this track

Short answer

Test behaviour at stable boundaries, keep tests independent, mock only external systems, and use snapshots sparingly. A good suite is fast, deterministic and easy to delete when the behaviour is gone.

A test suite is production infrastructure. Its value is not the number of assertions but the confidence it gives when changing code. That confidence depends on tests that fail for real regressions and stay quiet for refactors. Speed and determinism are part of the design, not a later optimisation.

Principles that hold up

  • Assert on observable behaviour, not internal state or hook call counts
  • Mock at system boundaries: network, clock, filesystem and third-party SDKs
  • Create per-test state and clean up mocks, timers and globals
  • Use the lowest level that still covers the risk: unit, integration or end-to-end
  • Keep snapshots small and reviewed, or use explicit assertions instead
  • Make failures informative by using specific matchers and clear test names
ts
test("returns a validation error instead of writing", async () => {
  const save = jest.fn();
  const result = await submitOrder({ save, items: [] });

  expect(result).toEqual({ error: "cart is empty" });
  expect(save).not.toHaveBeenCalled();
});
One behaviour, a stable boundary and an assertion on the observable result.

Not every behaviour deserves a test at every level. Push detail into unit tests and let a smaller number of integration tests cover wiring. This keeps the suite fast and reduces duplication. A test that duplicates a lower-level assertion adds maintenance cost without new signal.

Delete tests and snapshots that no longer protect a behaviour. A suite that only grows becomes slower and harder to trust. Coverage is a diagnostic, not the goal.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1How do you decide between a unit and an integration test?
  2. 2Why is a deterministic suite more valuable than a large one?
  3. 3When should you delete a test?
  4. 4How do you keep mocking from replacing the behaviour under test?

Say it in the interview

I aim for tests that fail on real regressions and stay quiet during refactors. That means asserting on observable behaviour, mocking only external boundaries, and keeping every test independent. I pick the lowest test level that still covers the risk, and I keep snapshots small and reviewed. A suite is healthy when it is fast, deterministic and easy to prune, not when it has the most assertions.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Jest mock interview and get scored feedback on depth, clarity and confidence.