Skip to main content
skill CX Codex

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.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add Mindrally/skills --skill jest -a claude-code
Open on GitHub

What 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 fetchUser module: a successful API response and a failure.
  • Refactor these tests: move setup into beforeEach and clear mocks with jest.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-code

2Manually: put the files in place

~/.claude/skills/jest/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/jest/

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

Similar skills

Everything for Testing and QA →

From the same set168

swr SWR data-fetching guidelines for React and TypeScript apps. It guides agents on caching, request deduplication, and optimistic mutations. CX Codex +1 870installs django-rest-api-development A skill that gives your coding agent a house style for Django REST APIs: project layout, serializers, JWT auth and query performance. CX Codex +1 868installs data-jupyter-python Tells your agent how to write data analysis code: pandas and numpy, matplotlib and seaborn plots, reproducible notebooks, fewer loops. CX Codex +1 865installs openai-api-development A skill that steers your coding agent through Anthropic API work: Claude calls, function calling, embeddings and Assistants API, with Python examples. CX Codex +1 864installs ruby-rails This skill hands your agent Rails conventions: generators over boilerplate, ActiveRecord patterns, Hotwire, background jobs and tests. Useful on a fresh app or an existing one. CX Codex +1 862installs seo-best-practices Provides search optimization guidelines for web apps. Helps configure metadata, JSON-LD schemas, rendering modes, and Core Web Vitals. CX Codex +1 858installs fastify-typescript Gives the agent a rulebook for Fastify and TypeScript: validation schemas, plugins, Prisma access and Jest tests. Useful when you build a typed REST API. CX Codex +1 856installs performance-optimization A rule set for web performance: server-side rendering, CSS, JavaScript, assets and caching. Useful when your agent writes frontend code and you want a bar to hold it to. CX Codex +1 848installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor