Generate Jest unit tests for JavaScript/TypeScript with mocking, coverage. Use for JS/TS modules, React components, test generation, or encountering missing coverage, improper mocking, test structure errors.
View on GitHubsecondsky/claude-skills
jest-generator
January 24, 2026
Select agents to install to:
npx add-skill https://github.com/secondsky/claude-skills/blob/main/plugins/jest-generator/skills/jest-generator/SKILL.md -a claude-code --skill jest-generatorInstallation paths:
.claude/skills/jest-generator/# Jest Generator Skill
## Purpose
Generate Jest-based unit tests for JavaScript and TypeScript code, following Jest conventions and best practices with proper mocking, describe blocks, and code organization.
## When to Use
- Generate Jest tests for JavaScript/TypeScript modules
- Create test files for React components
- Add missing test coverage to existing code
- Need Jest-specific patterns (mocks, spies, snapshots)
## Test File Naming
**Source to Test Mapping:**
- `src/components/Feature.tsx` → `src/components/Feature.test.tsx`
- `src/utils/validator.ts` → `src/utils/validator.test.ts`
- `src/models/User.ts` → `src/models/User.test.ts`
## Running Tests
```bash
# Preferred: Using bun (faster)
bun test
# Run specific file
bun test src/utils/validator.test.ts
# Run with coverage
bun test --coverage
# Watch mode
bun test --watch
# Alternative: Using npm
npm test
npm test -- --coverage
```
## Jest Test Structure
```typescript
import { functionToTest, ClassToTest } from './Feature'
jest.mock('./dependency')
describe('ModuleName', () => {
beforeEach(() => {
jest.clearAllMocks()
})
afterEach(() => {
jest.restoreAllMocks()
})
describe('ClassName', () => {
let instance: ClassToTest
beforeEach(() => {
instance = new ClassToTest()
})
it('should return expected result with valid input', () => {
// Arrange
const input = { key: 'value' }
const expected = { processed: true }
// Act
const result = instance.method(input)
// Assert
expect(result).toEqual(expected)
})
it('should throw error with invalid input', () => {
expect(() => instance.method(null)).toThrow('Invalid input')
})
})
})
```
## Jest-Specific Patterns
### Mocking
```typescript
// Mock entire module
jest.mock('./api', () => ({
fetchData: jest.fn(),
}))
// Mock with implementation
const mockFn = jest.fn((x: number) => x * 2)
// Spy on method
const spy = jest.spyOn(obj, 'method').mockReturnV