Jest

JavaScript testing framework - zero config, snapshots, mocking, and code coverage out of the box

TL;DR

What: A delightful JavaScript testing framework with zero configuration.

Why: Fast, snapshot testing, great mocking, built-in coverage, widely adopted.

Quick Start

Install:

npm install --save-dev jest

Add to package.json:

{
  "scripts": {
    "test": "jest"
  }
}

Create test file (sum.test.js):

const sum = (a, b) => a + b;

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

Run tests:

npm test

Cheatsheet

MatcherDescription
toBe(value)Exact equality
toEqual(obj)Deep equality
toBeTruthy()Truthy value
toBeNull()Null check
toContain(item)Array contains
toThrow()Throws error
toHaveBeenCalled()Mock was called

Gotchas

Async testing

// Async/await
test('fetches data', async () => {
  const data = await fetchData();
  expect(data).toBe('data');
});

// Promises
test('resolves to data', () => {
  return expect(fetchData()).resolves.toBe('data');
});

Mocking

// Mock function
const mockFn = jest.fn();
mockFn.mockReturnValue(42);
mockFn.mockResolvedValue('async result');

// Mock module
jest.mock('./api', () => ({
  fetchUser: jest.fn().mockResolvedValue({ name: 'John' })
}));

Snapshot testing

test('renders correctly', () => {
  const tree = renderer.create(<Button>Click</Button>).toJSON();
  expect(tree).toMatchSnapshot();
});

Setup and teardown

beforeAll(() => { /* Run once before all tests */ });
afterAll(() => { /* Run once after all tests */ });
beforeEach(() => { /* Run before each test */ });
afterEach(() => { /* Run after each test */ });

Next Steps