Jest
- Pronunciation
- JEST
In short
Jest is a popular JavaScript testing framework that bundles a test runner, assertions, mocking, snapshots and code coverage in one package with little setup.
What is Jest?
Jest was created at Facebook, now Meta, and has been part of the OpenJS Foundation since 2022. Before it, JavaScript projects usually combined several tools: one to run tests, one for assertions, another for mocks and another for coverage. Jest bundles all of these, so npm install --save-dev jest and a test file are enough to start.
Tests are written with test (or it) and grouped with describe. Inside, expect checks results with matchers such as toBe, toEqual or toThrow. Jest finds files ending in .test.js or .spec.js on its own, runs them in parallel worker processes, and in watch mode re-runs only the tests affected by the files you changed.
Mocking is built in: jest.fn() creates fake functions that record how they were called, and jest.mock() replaces whole modules, such as an API client, so unit tests stay fast and don't depend on the network. Snapshot tests save the output of a component or function and fail if it later changes unexpectedly, and --coverage reports which lines the tests ran.
A common misconception is that Jest tests run in a real browser. By default they run in Node.js, optionally with jsdom, a simulated browser environment; for real browsers, end-to-end tools such as Playwright are used. In projects built with Vite, many teams now choose Vitest, which offers a Jest-compatible API.
Key takeaways
- Jest is an all-in-one JavaScript testing framework.
- It includes a test runner, assertions, mocking, snapshots and coverage.
- Tests use describe, test and expect with matchers such as toEqual.
- jest.fn() and jest.mock() replace real dependencies in unit tests.
- Tests run in Node.js or jsdom, not in a real browser.
Example
// cart.test.js
import { total } from "./cart";
describe("total", () => {
test("adds up item prices", () => {
expect(total([{ price: 10 }, { price: 5 }])).toBe(15);
});
test("applies a discount from a fake service", () => {
const getDiscount = jest.fn().mockReturnValue(0.1); // a mock
expect(total([{ price: 100 }], getDiscount)).toBe(90);
expect(getDiscount).toHaveBeenCalledTimes(1);
});
});Readers ask
What is the difference between Jest and Vitest?
Both are JavaScript test runners with very similar APIs. Vitest is built on Vite and reuses its configuration, so it fits Vite projects and starts quickly; Jest is older, framework-independent and very widely used.
Is Jest only for React?
No. Jest became popular with React, but it tests any JavaScript or TypeScript code, including Node.js services, utilities and other frameworks.
What is snapshot testing in Jest?
A snapshot test saves the output of a function or component to a file the first time it runs, then fails later if the output changes, so unintended changes are noticed during review.
See also
- Unit TestTesting & Quality, p. 35A unit test is a small, automated check that verifies one function, method, or class behaves correctly in isolation from the rest of the program.
- Test RunnerTesting & Quality, p. 33A test runner is a tool that finds a project's automated tests, executes them, and reports which ones passed or failed, usually from a single command.
- MockingTesting & Quality, p. 16Mocking is a testing technique that replaces a real dependency, such as a database or API, with a fake you control so that code can be tested in isolation.
- Snapshot TestingTesting & Quality, p. 25Snapshot testing is a technique that saves the output of code to a file on the first run and fails later runs if the output no longer matches that copy.
- Test CoverageTesting & Quality, p. 30Test coverage is a metric that measures how much of a program's source code is executed when its automated tests run, usually shown as a percentage.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
Spotted a mistake or something missing on this page?Suggest an edit