Testing
Jest
A testing framework for JavaScript and TypeScript: runner, assertions, mocks and coverage in one package.
What it is
Facebook (Meta) created Jest in 2014, and today the project belongs to the OpenJS Foundation. It bundles everything for unit tests: running, expect assertions, mocks, timers, snapshot tests and coverage reports.
Tests run in parallel in isolated workers, and watch mode reruns only changed tests. jsdom provides a DOM, and for React you add React Testing Library.
Jest was the standard in the JS world for a long time. New Vite projects more often pick Vitest, but in many existing codebases (including React Native) Jest remains the default.
Use it when
- Unit and integration tests of JS / TS code.
- React Native or an existing project where Jest is already set up.
- You need snapshot tests and built-in coverage without extra packages.
Avoid it when
- A new Vite project with ES modules: Vitest is simpler to configure.
- End-to-end browser tests: use Playwright or Cypress.
Key concepts
- describe / it / expect
- Test groups, individual tests and result assertions.
- Mocks
jest.fn()andjest.mock()replace functions and modules.- Snapshot
- Saves output and compares against it in later runs.
- Fake timers
- Control of
setTimeoutandDatein tests. - Coverage
- A report of which lines and branches ran.
Example
Interview questions
1. How does jest.fn() differ from jest.mock()?
jest.fn()creates a mock function whose calls you can watch.jest.mock('module')replaces a whole module with a mock within the test.2. What is a snapshot test and when does it hurt?
A test compares output against a saved snapshot. It is quick to write, but nobody reads big snapshots and they just get updated. So keep them small and targeted.
3. How do you test async code?
Return a promise or use
async/awaitandexpect(...).resolvesorrejects. Fake timers handle timers.4. Why isolate tests?
Each test file runs in its own context, so one test's global state does not affect another and parallel runs are safe.
Interview questions from the bank
- How do unit, integration, and e2e tests differ from each other?JuniorTesting
- How do a stub, mock, spy, and fake differ?JuniorTesting
- Does high code coverage guarantee the quality of tests?JuniorTesting
- What is TDD, and what are its advantages and drawbacks?JuniorTesting
- What makes a good test structure, and what are Arrange, Act, Assert?JuniorTesting
- How do you test asynchronous code correctly in Jest or Vitest?JuniorTesting
- Why use parameterized tests?JuniorTesting
- What is a regression test, and how do you add one when fixing a bug?JuniorTesting
Comparisons with this technology
Related technologies
- VitestA fast Vite-based test runner with a Jest-compatible API and native TypeScript and ESM support.
- ReactA library for building interfaces from components: UI as a function of state.
- React NativeA framework for native mobile apps using React and JavaScript or TypeScript.
- TypeScriptA superset of JavaScript with static types that compiles to plain JS.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).