Skip to main content



Jest is a Node-based runner. This means that the tests always run in a Node environment and not in a real browser. This lets us enable fast iteration speed and prevent flakiness.

While Jest provides browser globals such as window thanks to jsdom, they are only approximations of the real browser behavior. Jest is intended to be used for unit tests of your logic and your components rather than the DOM quirks.

We recommend that you use a separate tool for browser end-to-end tests if you need them. They are beyond the scope of Create React App.


# Run all tests once
npx jest

# Watch mode
npx jest --watch

# File name
npx jest AuthController
# We can have a path too
npx jest auth/AuthController

# Directory
npx jest src/auth

# Test name - see
npx jest -t=^GET /products
npx jest --testNamePattern=^GET /products

# Git changed/uncommitted files - see
npx jest -o
npx jest --onlyChanged

# Print code coverage - see
npx jest --coverage


npm i -E -D jest
npx jest --init # creates jest.config.ts

With TypeScript

To do the setup follow

npm i -D -E jest @types/jest ts-jest

Doing npx ts-jest config:init "will create a basic Jest configuration file which will inform Jest about how to handle .ts files correctly.".

The resulting file of npx ts-jest config:init is very basic. It's better to do npx jest --init which creates jest.config.ts but "add the line preset: "ts-jest" to the jest.config.,s file afterwards."

Folder structure

Code coverage

npx jest --coverage



// To compare objects use toEqual
expect(response.body.user).toEqual({ id: 1, name: 'Pere' })

expect(response.body).toHaveProperty('user.age') // can check nested properties
expect(response.body).toHaveProperty('user.age', 18) // optionally check value too

expect(response.body.user).toBeDefined() // check not undefined

// toHaveProperty checks existence, toBeDefined checks value
expect({ user: undefined }).toHaveProperty('user') // Success
expect({ user: undefined }.user).toBeDefined() // Failure
expect({ user: undefined }.user).toBeUndefined() // Success

// String partial match
expect('cat dog').toContain('cat') // Success
expect('cat dog').toMatch('cat') // Success
// String exact match
expect('cat dog').toBe('cat') // Failure
expect('cat dog').toBe('cat dog') // Success



Set clearMocks: true at jest.config.js/ts to "automatically clear mock calls, instances, contexts and results before every test".

How to change mock implementation for a single test with Jest? -

Mocking a database:

jest.clearAllMocks vs jest.resetAllMocks vs jest.restoreAllMocks explained -


// UserDatabase.ts
export async function getUserById(userId: number): Promise<User> {
// database.query('SELECT ...')

// ---

// UserController.test.ts
import { getUserById } from './UserDatabase'


describe('GET /users/:id', () => {
test('should get the user from the database', async () => {
const getUserByIdMock = jest.mocked(getUserById)
id: 6,
name: 'Joe',
email: '',

// ...


Equivalent ways to assert with mocks:

expect(myMock).toHaveBeenCalledWith('a', 'b')

Mock a single function of a module, while the other ones use the real implementation

There are various ways.

One way is to use jest.mock and jest.requireActual as explained at

The other is to use spyOn:

import * as Token from './token'

describe('My function', () => {
test('should...', async () => {
const generateTokenSpy = jest
.spyOn(Token, 'generateToken')
.mockImplementation(() => 'some-token')

// ...

// Optionally we can restore the original (non-mocked) implementation of the
// function 'generateToken' if we don't want to mock it on the other tests.

See more here:

Parametrized tests

test.each(table)(name, fn, timeout)

.each TypeScript Usage

In addition to test.each you can also use it.each:

describe('some function', () => {
`('if category is $category it should...', async ({ category }) => {