Jest
Delightful JavaScript testing framework from Meta — zero config, snapshot testing, mocking, and code coverage built in.
Jest is the dominant JavaScript testing framework — it runs tests in parallel in isolated environments, provides snapshot testing for UI components, and has a built-in mocking library. Its zero-configuration setup works with React, Vue, Node.js, and TypeScript out of the box. Vitest is a modern alternative that reuses Vite’s build pipeline for faster runs in Vite-based projects.
Quick start
npm install --save-dev jest
# For TypeScript
npm install --save-dev @types/jest ts-jest
// math.js
function add(a, b) { return a + b }
function divide(a, b) {
if (b === 0) throw new Error('Division by zero')
return a / b
}
module.exports = { add, divide }
// math.test.js
const { add, divide } = require('./math')
describe('add()', () => {
test('returns sum of two numbers', () => {
expect(add(2, 3)).toBe(5)
})
test('handles negative numbers', () => {
expect(add(-1, 1)).toBe(0)
})
})
describe('divide()', () => {
test('divides two numbers', () => {
expect(divide(10, 2)).toBe(5)
})
test('throws on division by zero', () => {
expect(() => divide(5, 0)).toThrow('Division by zero')
})
})
// Mocking example
const fetchUser = jest.fn().mockResolvedValue({ id: 1, name: 'Alice' })
test('renders user name', async () => {
const user = await fetchUser(1)
expect(user.name).toBe('Alice')
expect(fetchUser).toHaveBeenCalledWith(1)
})
npx jest # run all tests
npx jest --watch # watch mode
npx jest --coverage
When to use
Jest is the default choice for JavaScript and TypeScript unit and integration tests. It’s pre-configured in Create React App, Next.js, and most scaffolding tools. For Vite-based projects (SvelteKit, Astro, Vite React), Vitest is faster because it reuses the existing build pipeline. For end-to-end browser tests, Playwright or Cypress are more appropriate.
// features
- Zero configuration — works out of the box with React, Node.js, TypeScript
- Built-in mocking — `jest.fn()`, `jest.spyOn()`, auto-mock modules
- Snapshot testing — assert that UI components don't change unexpectedly
- Parallel test execution across worker threads
- Built-in code coverage via V8 or Istanbul
- Watch mode with smart test selection based on changed files
- Rich assertion library via `expect()`
- jsdom environment for DOM testing without a browser
// installation
npm install --save-dev jest
pnpm add -D jest
yarn add -D jest