Skip to content

skillTestingjavascripttypescript

Jest Testing Patterns

Structures Jest suites with factories, fake timers and module mocks that reset cleanly, so tests stay independent and order does not matter.

npx workspacetemplates add jest-testing-patterns --tool claude-code

Writes 1 file to .claude/skills/jest-testing-patterns/

.claude/skills/jest-testing-patterns/SKILL.md35 lines · 1.6 KBFile
---
name: jest-testing-patterns
description: Structures Jest suites with factories, fake timers and module mocks that reset cleanly, so tests stay independent and order does not matter.
---

# Jest testing patterns

Every test must pass alone, in any order, and clean up after itself.

## Structure

- Name tests as behavior: `it('rejects an expired coupon')`. One behavior per test, arrange / act / assert with blank lines between.
- Build data with small factory functions (`makeUser({ role: 'admin' })`) with defaults and overrides. Do not share mutable fixtures between tests.

## Mocks and isolation

- Prefer `jest.spyOn(obj, 'method')` over replacing modules. For modules, use `jest.mock('./x')` at the top and `jest.requireActual` when you only want to override part.
- Set `restoreMocks: true` and `clearMocks: true` in the Jest config so nothing leaks. Never rely on manual cleanup alone.
- Use `jest.isolateModules` when a module reads environment variables at import time.
- Reset anything global (env vars, `Date`, `fetch`) in `afterEach`.

## Time and async

- Use `jest.useFakeTimers()` and `advanceTimersByTime` or `runOnlyPendingTimers`. Restore with `useRealTimers` in `afterEach`. For promises plus timers use the async variants.
- Always `await` or return promises. Use `await expect(p).rejects.toThrow(...)`, never a bare try/catch.

## Rules

- Do not use `test.only` or leave `console` noise unchecked; fail on unexpected console errors.
- Do not snapshot large objects; assert the fields that matter.
- Run `jest --runInBand --randomize` once to prove independence.

## Report

Files changed, the isolation problems found and the config options added.

What it does

Jest suites tend to rot into a tangle of shared fixtures, leaked mocks and tests that pass only in a fixed order. This skill gives the agent the setup patterns that keep each test isolated and easy to read.

It covers data factories, fake timers, module and spy mocking with automatic restore, async assertions, and the config switches that catch leaks early.

Example

you › Our Jest tests only pass in order. Clean up the mocks in src/services.

agent › Found 3 leaks: a jest.mock of ./mailer without restore, a Date.now override never undone, and a shared user fixture mutated in two tests. Added restoreMocks to jest.config, converted fixtures to makeUser(), wrapped timers in afterEach. Suite now passes with --randomize across 5 seeds.

Questions

Does this work with Vitest?
Mostly yes. The structure and isolation rules carry over, but config names and the vi helpers differ, so the agent checks your runner first.
Should I use snapshots?
Only for small, stable output such as a short serialized string. Large snapshots get approved without reading and hide regressions.
  • Contract Testing

    SkillTesting

    Free
  • E2E Test Design

    SkillTesting

    Free
  • Flaky Test Diagnosis

    SkillTesting

    Free