Module Mocks in Jest
From the Jest cheat sheet · Mocking · verified Jul 2026
Module Mocks
Mocking entire modules and their exports
javascript
// Mock entire module
jest.mock('./api')
import * as api from './api'
// Mock with factory
jest.mock('./config', () => ({
apiUrl: 'http://localhost',
timeout: 1000
}))
// Partial mocks
jest.mock('./math', () => ({
...jest.requireActual('./math'),
add: jest.fn(() => 5)
}))
// Mock ES6 classes
jest.mock('./UserService')
import UserService from './UserService'
const mockUserService = UserService as jest.MockedClass<typeof UserService>💡 Place manual mocks in __mocks__ folder adjacent to module
⚡ Use requireActual for partial mocks to keep some real code
📌 Mock hoisting moves jest.mock to top of file automatically
🟢 TypeScript: cast mocked modules for proper types
mockingmodules