Claude Skills
CollectionsCompareWorkflowsNominate
Sign inSign up
© 2026 Curated Agent Skills·Learn more about Agent Skills
Back to repository

jest-mocking

verified

Jest mocking patterns and techniques

View on GitHub

Marketplace

alphaagent-team

the-answerai/alphaagent-team

Plugin

aai-stack-jest

stack

Repository

the-answerai/alphaagent-team

plugins/aai-stack-jest/skills/jest-mocking/SKILL.md

Last Verified

February 1, 2026

Install Skill

Select agents to install to:

Scope:
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-mocking

Installation paths:

Claude
.claude/skills/jest-mocking/
Powered by add-skill CLI

Instructions

# 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

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
7129 chars