Stats
Actions
Tags
Help us improve
Share bugs, ideas, or general feedback.
From aai-stack-jest
Provides patterns for Jest mock functions including creation, implementations, assertions, and module mocking via auto, partial, factory, and manual methods. Useful for unit tests.
npx claudepluginhub bradtaylorsf/alphaagent-teamHow this skill is triggered — by the user, by Claude, or both
Slash command
/aai-stack-jest:jest-mockingThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Patterns for mocking in Jest tests.
Provides a checklist for code reviews covering functionality, security, performance, maintainability, tests, and quality. Use for pull requests, audits, team standards, and developer training.
Share bugs, ideas, or general feedback.
Patterns for mocking in Jest tests.
// Basic mock function
const mockFn = jest.fn()
// With implementation
const mockFn = jest.fn((x) => x + 1)
// With return value
const mockFn = jest.fn().mockReturnValue(42)
// With different return values
const mockFn = jest.fn()
.mockReturnValueOnce(1)
.mockReturnValueOnce(2)
.mockReturnValue(99) // Default after exhausted
// Async return
const mockFn = jest.fn().mockResolvedValue({ id: 1 })
const mockFn = jest.fn().mockRejectedValue(new Error('fail'))
// Replace implementation
const mockFn = jest.fn()
.mockImplementation((a, b) => a + b)
// Different implementation each call
const mockFn = jest.fn()
.mockImplementationOnce(() => 'first')
.mockImplementationOnce(() => 'second')
.mockImplementation(() => 'default')
// Access call arguments in implementation
const mockFn = jest.fn((callback) => callback('result'))
const mockFn = jest.fn()
// Call assertions
expect(mockFn).toHaveBeenCalled()
expect(mockFn).toHaveBeenCalledTimes(2)
expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2')
expect(mockFn).toHaveBeenLastCalledWith('final')
expect(mockFn).toHaveBeenNthCalledWith(1, 'first')
// Return assertions
expect(mockFn).toHaveReturnedWith(value)
expect(mockFn).toHaveLastReturnedWith(value)
// Access mock data
expect(mockFn.mock.calls).toEqual([['arg1'], ['arg2']])
expect(mockFn.mock.results[0].value).toBe('result')
expect(mockFn.mock.instances).toHaveLength(1)
// Mock entire module
jest.mock('./userService')
import { createUser, findUser } from './userService'
// All exports are now mock functions
(createUser as jest.Mock).mockResolvedValue({ id: 1 })
test('uses mocked module', async () => {
const user = await createUser({ name: 'John' })
expect(user).toEqual({ id: 1 })
})
jest.mock('./utils', () => ({
...jest.requireActual('./utils'), // Keep original
formatDate: jest.fn(() => '2024-01-01'), // Mock specific
}))
import { formatDate, formatNumber } from './utils'
test('formatDate is mocked', () => {
expect(formatDate()).toBe('2024-01-01') // Mocked
})
test('formatNumber is real', () => {
expect(formatNumber(1000)).toBe('1,000') // Original
})
// Mock with factory
jest.mock('./config', () => ({
__esModule: true,
default: {
apiUrl: 'http://test.api',
timeout: 1000,
},
getConfig: jest.fn(() => ({ debug: true })),
}))
// Create __mocks__/axios.ts
const mockAxios = {
get: jest.fn(() => Promise.resolve({ data: {} })),
post: jest.fn(() => Promise.resolve({ data: {} })),
create: jest.fn(() => mockAxios),
}
export default mockAxios
// In test file
jest.mock('axios')
import axios from 'axios'
test('uses mock axios', async () => {
(axios.get as jest.Mock).mockResolvedValue({ data: { id: 1 } })
const response = await axios.get('/users/1')
expect(response.data).toEqual({ id: 1 })
})
// userService.ts
export class UserService {
async create(data: UserData): Promise<User> { ... }
async find(id: string): Promise<User> { ... }
}
// test file
jest.mock('./userService')
import { UserService } from './userService'
const MockUserService = UserService as jest.MockedClass<typeof UserService>
beforeEach(() => {
MockUserService.mockClear()
})
test('uses mocked class', async () => {
const mockCreate = jest.fn().mockResolvedValue({ id: '1' })
MockUserService.prototype.create = mockCreate
const service = new UserService()
const user = await service.create({ name: 'John' })
expect(user).toEqual({ id: '1' })
expect(mockCreate).toHaveBeenCalledWith({ name: 'John' })
})
jest.mock('./Logger', () => {
return jest.fn().mockImplementation(() => ({
log: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
}))
})
const video = {
play() { return true },
pause() { return false },
}
// Spy without changing implementation
const playSpy = jest.spyOn(video, 'play')
video.play()
expect(playSpy).toHaveBeenCalled()
expect(video.play()).toBe(true) // Original still works
playSpy.mockRestore() // Restore original
const api = {
fetchUser(id: string) { /* real implementation */ }
}
const spy = jest.spyOn(api, 'fetchUser')
.mockResolvedValue({ id: '1', name: 'John' })
const user = await api.fetchUser('1')
expect(user.name).toBe('John')
spy.mockRestore() // Restore original
const obj = {
_value: 0,
get value() { return this._value },
set value(v) { this._value = v },
}
const getSpy = jest.spyOn(obj, 'value', 'get').mockReturnValue(42)
expect(obj.value).toBe(42)
const setSpy = jest.spyOn(obj, 'value', 'set')
obj.value = 100
expect(setSpy).toHaveBeenCalledWith(100)
const mockFn = jest.fn()
// Clear call history (keeps implementation)
mockFn.mockClear()
jest.clearAllMocks()
// Reset to initial state (no implementation)
mockFn.mockReset()
jest.resetAllMocks()
// Restore original implementation (for spies)
mockFn.mockRestore()
jest.restoreAllMocks()
const mockFn = jest.fn().mockName('myMockFunction')
// Better error messages
expect(mockFn).toHaveBeenCalled()
// Error: expect(myMockFunction).toHaveBeenCalled()
// __mocks__/fs.ts
export const readFileSync = jest.fn()
export const writeFileSync = jest.fn()
// test file
jest.mock('fs')
import { readFileSync } from 'fs'
test('reads file', () => {
(readFileSync as jest.Mock).mockReturnValue('content')
expect(readFileSync('test.txt')).toBe('content')
})
const mockDate = new Date('2024-01-15T12:00:00Z')
beforeAll(() => {
jest.useFakeTimers()
jest.setSystemTime(mockDate)
})
afterAll(() => {
jest.useRealTimers()
})
test('uses mocked date', () => {
expect(new Date().toISOString()).toBe('2024-01-15T12:00:00.000Z')
expect(Date.now()).toBe(mockDate.getTime())
})
const originalEnv = process.env
beforeEach(() => {
jest.resetModules()
process.env = { ...originalEnv }
})
afterAll(() => {
process.env = originalEnv
})
test('uses env variable', () => {
process.env.API_URL = 'http://test.api'
const { config } = require('./config')
expect(config.apiUrl).toBe('http://test.api')
})
global.fetch = jest.fn()
beforeEach(() => {
(fetch as jest.Mock).mockClear()
})
test('fetches data', async () => {
(fetch as jest.Mock).mockResolvedValue({
ok: true,
json: async () => ({ id: 1, name: 'John' }),
})
const response = await fetch('/api/users/1')
const data = await response.json()
expect(data).toEqual({ id: 1, name: 'John' })
expect(fetch).toHaveBeenCalledWith('/api/users/1')
})
Used by:
frontend-developer agentbackend-developer agentfullstack-developer agent