Skip to main content

Jest

Pronunciation
JEST
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/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

A unit test with a mocked functionjavascript
// 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings