What it is
Jest is a delightful JavaScript testing framework with a focus on simplicity, correctness, and developer experience. It supports unit, integration, and snapshot testing for JavaScript and TypeScript projects.
Jest allows developers to write tests using simple syntax and run them in a fast and isolated environment. It supports features like mocks, spies, snapshot testing, code coverage, and parallel test execution.
- Licence
- MIT
- Watch for
- Snapshot tests are easy to update thoughtlessly and stop catching anything
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Existing projects already configured for it
- React Native, where it is the standard
Look elsewhere when
- New Vite projects — Vitest is faster and needs almost no configuration
- ESM-heavy codebases, where Jest's module handling still causes friction
Installation
npm install --save-dev jestGetting started
The smallest useful thing you can do with it, and what each part means.
function sum(a, b) {
return a + b;
}
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});let counter = 0;
beforeEach(() => {
counter = 0;
});
test('counter starts at 0', () => {
expect(counter).toBe(0);
});Advanced usage
Where the library earns its place over a simpler alternative.
const myFunc = jest.fn();
myFunc('hello');
expect(myFunc).toHaveBeenCalledWith('hello');const user = { name: 'Alice', age: 25 };
test('user snapshot', () => {
expect(user).toMatchSnapshot();
});function fetchData() {
return Promise.resolve('peanut butter');
}
test('the data is peanut butter', async () => {
const data = await fetchData();
expect(data).toBe('peanut butter');
});jest.mock('./api');
import { fetchData } from './api';
fetchData.mockResolvedValue('mocked data');Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Test failed due to unexpected value
- Check the expected value, and ensure the function under test produces consistent outputs.
- Cannot find module
- Verify module paths and use `jest.mock()` appropriately when mocking dependencies.
- Timeout - Async callback was not invoked
- Ensure asynchronous tests call `done()` or return a promise, and adjust timeout if necessary.
Best practices
- Write small, focused tests for individual units of code.
- Use mocks and spies to isolate the code under test.
- Leverage snapshot testing for UI components and serializable objects.
- Run tests in watch mode during development for faster feedback.
- Organize test files alongside source files or in a __tests__ directory for clarity.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Jest was created by Facebook to test React applications, but it has grown to support any JavaScript project. Its zero-configuration setup, powerful mocking capabilities, and snapshot testing features make it widely popular for testing modern web applications.
