Skip to content
Back to skills

Jest Testing Patterns

ASecurity

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

  • 3 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 15, 2026
ai-agentsreacttesting

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill jest-testing-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Jest Testing Patterns?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Jest Testing Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-jest-testing-patterns/badge)](https://www.skillsdirectory.com/skills/workspace-templates-jest-testing-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.