Skip to content
IT glossary
TestingUpdated December 3, 2026

Jest

A testing framework that finds and runs automated checks for JavaScript and TypeScript code. When a check fails, it shows what the code did and how that differed from what the test expected.

Also known as: Vitest

What Jest does

Jest lets you turn an expectation about your code into a check you can run again after every change. If shipping is supposed to be free at $50, you can write a test for that exact boundary instead of checking it manually before each release. Jest runs the same suite on your laptop and in CI, where a failed test can block a merge.

How a test runs

Jest discovers test files using naming rules and project configuration. Names such as shipping.test.js and shipping.spec.ts are common. test (also called it) declares one case, describe groups cases, and expect pairs an actual value with a matcher that states the requirement. For example, toBe compares values using Object.is, while toEqual can compare the contents of objects and arrays. When an assertion fails, the report points to the failing line and shows what did not match.

Tests run in a Node.js environment by default. For component tests that need a document, you can install and configure jest-environment-jsdom. It provides browser-like APIs, but it does not lay out the page or paint pixels like a real browser. TypeScript can be transformed before Jest runs it; whether types are checked depends on the setup. Many projects run tsc --noEmit separately in CI.

Example: testing a boundary

The function below takes a subtotal in cents. test.each runs the same check for both rows and gives each case its own result.

function shippingFee(subtotal) {
  return subtotal >= 5000 ? 0 : 499;
}

test.each([
  [4999, 499],
  [5000, 0],
])('for a subtotal of %i cents, charges %i cents', (subtotal, fee) => {
  expect(shippingFee(subtotal)).toBe(fee);
});

If someone changes >= to >, the second case fails: a customer spending exactly $50 would be charged $4.99. The test checks the returned fee, so rewriting the function without changing its behavior does not require rewriting the test.

Where it fits

Jest can run unit tests for individual functions and integration tests that exercise several modules together. For UI tests, it is often paired with Testing Library, which helps find elements and interact with them. For a workflow that depends on real navigation or browser rendering, use a browser-based end-to-end test rather than relying on jsdom. Vitest is a separate framework with a similar test and expect API. Moving between them can still mean changing configuration or mock behavior; for instance, Vitest uses vi.fn() where Jest uses jest.fn().

Common mistakes and interview questions

Interviewers may ask how to test a rejected promise. Use await expect(loadUser()).rejects.toThrow() or return the assertion's promise. Without await or return, the test can finish before the assertion runs. They may also ask what to mock: replacing a payment service can make a unit test deterministic, but that test cannot show that the real client and service agree on the request format. Likewise, coverage can show which code ran, but not whether the assertions would catch a meaningful bug.