jest — Claude Code skill: Jest testing rules for your agent
A skill that tells your agent how to write Jest tests: structure, mocks, assertions, async. Useful when AI writes the tests in your repo.
npx skills add Mindrally/skills --skill jest -a claude-codeWhat jest does
The skill lays out how an agent should write Jest tests in JavaScript and TypeScript projects. Test names explain the expected behavior, describe blocks group related checks, and each test follows Arrange-Act-Assert while covering a single outcome.
Setup and teardown get their own rules. beforeEach and afterEach keep tests isolated, while beforeAll and afterAll are reserved for expensive setup worth sharing. Modules are mocked with jest.mock(), functions with jest.fn(), and jest.spyOn() is the pick when you want to track calls but keep the real implementation. Mocks are cleared between tests with jest.clearAllMocks(). The skill also warns against mocking everything — test real behavior where you can.
Assertions follow one rule: use the most specific matcher. toEqual for objects, toBe for primitives, snapshots rarely and with meaningful names. Async tests return a promise or use async/await, and coverage is tracked with the --coverage flag. Nothing gets installed into your project, so it won't clash with your existing Jest config — it is guidance for the agent only.
What it helps with
- write Jest tests in one consistent style
- group checks into describe blocks
- mock a module or a single function
- test async code without hanging promises
- track coverage with the --coverage flag
Example prompts for the agent
- Write Jest tests for the
fetchUsermodule: a successful API response and a failure. - Refactor these tests: move setup into
beforeEachand clear mocks withjest.clearAllMocks(). - Add negative cases to these tests and check coverage with
--coverage.
How to install jest
1Skills CLI (Vercel)
npx skills add Mindrally/skills --skill jest -a claude-code2Manually: put the files in place
~/.claude/skills/jest/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/jest/
1Skills CLI (Vercel)
npx skills add Mindrally/skills --skill jest -a codex2Manually: put the files in place
.agents/skills/jest/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add Mindrally/skills --skill jest -a cursor2Manually: put the files in place
.cursor/skills/jest/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install Mindrally/skills jest2Manually: put the files in place
.github/skills/jest/SKILL.md1Skills CLI (Vercel)
npx skills add Mindrally/skills --skill jest -a gemini-cli2Platform CLI
gemini skills install https://github.com/Mindrally/skills --path jestManually: put the files in place
.devin/skills/jest/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add Mindrally/skills --skill jest -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about jest
How do I install the jest skill in Claude?
Run npx skills add Mindrally/skills --skill jest -a claude-code. The skill file lands at ~/.claude/skills/jest/SKILL.md.
What does the jest skill do?
It gives the agent rules for writing Jest tests: test structure, setup and teardown, mocking, assertions, async checks and coverage.
Does the skill install Jest into my project?
No. It is guidance for the agent and does not touch the Jest package or your config.
Source text
SKILL.md · Apache-2.0
---
name: jest
description: Jest testing best practices for JavaScript and TypeScript applications, covering test structure, mocking, and assertion patterns.
metadata:
maintainer: Mindrally
source: https://github.com/Mindrally/skills
---
# Jest Testing Best Practices
You are an expert in JavaScript, TypeScript, and Jest testing.
## Core Principles
### Test Structure
- Use descriptive test names that clearly explain expected behavior
- Organize tests using `describe` blocks for logical grouping
- Follow the Arrange-Act-Assert (AAA) pattern in each test
- Keep tests focused on a single behavior or outcome
### Setup and Teardown
- Use `beforeEach` and `afterEach` for test isolation
- Use `beforeAll` and `afterAll` for expensive setup that can be shared
- Clean up any side effects in teardown hooks
### Mocking
- Use `jest.mock()` for module mocking
- Use `jest.fn()` for function mocks
- Use `jest.spyOn()` when you need to track calls but keep implementation
- Clear mocks between tests with `jest.clearAllMocks()`
- Avoid over-mocking: test real behavior when feasible
### Assertions
- Use the most specific matcher available
- Prefer `toEqual` for object comparison, `toBe` for primitives
- Use `toMatchSnapshot` sparingly and with meaningful names
- Include negative test cases (what should NOT happen)
### Async Testing
- Always return promises or use async/await in async tests
- Use `waitFor` from testing libraries for async assertions
- Set appropriate timeouts for long-running tests
### Coverage
- Aim for meaningful coverage, not just high percentages
- Test edge cases and error conditions
- Use `--coverage` flag to track coverage metrics
### Best Practices
- Keep tests DRY but readable (prefer clarity over brevity)
- Test behavior, not implementation details
- Make tests deterministic (no random data without seeding)
- Use factories or builders for test data creation
## Example Patterns
```javascript
describe('fetchUser', () => {
it('should return user data when API call succeeds', async () => {
const mockUser = { id: 1, name: 'John' };
jest.spyOn(api, 'get').mockResolvedValue(mockUser);
const result = await fetchUser(1);
expect(result).toEqual(mockUser);
expect(api.get).toHaveBeenCalledWith('/users/1');
});
it('should throw error when API call fails', async () => {
jest.spyOn(api, 'get').mockRejectedValue(new Error('Not found'));
await expect(fetchUser(999)).rejects.toThrow('Not found');
});
});
```
© Mindrally · Apache-2.0 · Mindrally/skills