Jest · Unit tests that survive refactors
What is Jest and what does it provide?
Short answer
Jest is a JavaScript test runner with built-in assertions, mocking, snapshots, coverage and watch mode. It runs each test file in an isolated environment with its own module registry.
Jest is a complete testing framework rather than only an assertion library. It discovers test files, provides the global test and expect functions, compiles TypeScript or JSX through transforms, and reports results. Because it owns the runtime, it can reset modules and mocks between files.
import { sum } from "./sum";
describe("sum", () => {
test("adds two numbers", () => {
const result = sum(2, 3);
expect(result).toBe(5);
});
});What ships in the box
- A test runner that can filter by file name, test name or changed files
- Matchers for values, errors, spies and promises
- Mocking for functions, modules, timers and the network boundary
- Snapshot testing and coverage collection through Istanbul
- Watch mode and a CLI that is easy to run locally and in CI
Jest is strongest for unit and integration tests: pure logic, React components in jsdom, and service modules with mocked dependencies. It is not a browser automation tool. End-to-end tests that need real rendering, layout or a live backend belong in Playwright or Cypress.
jest.mock hoisting may need changes when moving between them.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How is Jest different from Vitest?
- 2When would you reach for Playwright instead of Jest?
- 3What does Jest do that a plain assertion library does not?
- 4How does Jest discover which files are tests?
Say it in the interview
Jest is a full test runner, so it handles discovery, execution, assertions, mocking, snapshots and coverage. I reach for it for unit and integration tests, including React components in a jsdom environment. It isolates each test file, which makes module state and mocks predictable. For real browser behaviour I use an end-to-end tool instead.
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.