Code Reference

Mocks

Jest · Reference cheat sheet

Mocks

Jest · Reference cheat sheet


📋 Overview

jest.fn and jest.mock replace dependencies with controllable fakes. Track calls, set return values, and restore with clearAllMocks / restoreAllMocks.

🔧 Core concepts

APIRole
jest.fn()Mock function
jest.mock(path)Module mock (hoisted)
jest.spyOnWrap existing method
mockReturnValue / mockResolvedValueOutputs
mockImplementationCustom body

Manual mocks live in __mocks__ beside modules.

💡 Examples

jest.fn:

const send = jest.fn().mockReturnValue(true);
send('hi');
expect(send).toHaveBeenCalledWith('hi');
expect(send).toHaveBeenCalledTimes(1);

Async mock:

const fetchUser = jest
  .fn()
  .mockResolvedValue({ id: 1 })
  .mockRejectedValueOnce(new Error('network'));

Module mock:

jest.mock('./api', () => ({
  getUser: jest.fn().mockResolvedValue({ id: 1 }),
}));

import { getUser } from './api';

test('uses mock', async () => {
  await expect(getUser()).resolves.toEqual({ id: 1 });
});

Partial mock:

jest.mock('./utils', () => {
  const actual = jest.requireActual('./utils');
  return { ...actual, randomId: () => 'fixed' };
});

⚠️ Pitfalls

  • jest.mock is hoisted — don't rely on runtime vars above it without jest.doMock.
  • Forgetting to reset mocks between tests → cross-test pollution.
  • Mocking the wrong path (definition vs import site).
  • Over-mocking until nothing real is tested.
  • ESM + Jest mocking needs experimental/transform setup.

On this page