plugins/aai-stack-jest/skills/jest-mocking/SKILL.md
February 1, 2026
Select agents to install to:
npx add-skill https://github.com/the-answerai/alphaagent-team/blob/main/plugins/aai-stack-jest/skills/jest-mocking/SKILL.md -a claude-code --skill jest-mockingInstallation paths:
.claude/skills/jest-mocking/# Jest Mocking Skill
Patterns for mocking in Jest tests.
## Function Mocks
### Creating Mocks
```typescript
// 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'))
```
### Mock Implementations
```typescript
// 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'))
```
### Mock Assertions
```typescript
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)
```
## Module Mocks
### Auto Mocking
```typescript
// 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 })
})
```
### Partial Mocki