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
npx jest AuthController

# Directory
npx jest src/auth

# Git changed/uncommitted files
npx jest -o


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



// 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


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

Mocking a database:


// 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