jest/mock

Mock Functions

Create mock functions for testing

jest
mock
functions

Command

const mockFn = jest.fn()

Explanation

Mock functions allow you to test code in isolation by replacing dependencies with controllable test doubles.

Examples

Create mock function

const mockFn = jest.fn()

Mock with return value

const mockFn = jest.fn(() => "mocked value")

Spy on existing function

jest.spyOn(module, "functionName")