Jest Mocking Patterns

Jest Mocking Patterns is a skill for Claude Code, Codex from PramodDutta/qaskills. It costs 44 tokens per session (2,462 once invoked), scanned A, original, MIT.

A guide to replacing outside dependencies in Jest tests with controlled substitutes, such as fake functions, module replacements, and restored spies.

In plain words
What is it for?
Use it to isolate the code under test from tools such as axios, file systems, payment services, and database clients.
Why use it?
It lets tests run without relying on networks, clocks, files, databases, or third-party services, while avoiding changes that leak into other tests.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/pramoddutta/qaskills/jest-mocking-patterns
Any agent
npx skills add PramodDutta/qaskills --skill jest-mocking-patterns
Clone the repo
git clone --depth 1 https://github.com/PramodDutta/qaskills

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for Jest Mocking Patterns

README.md
[![agentmods](https://agentmods.dev/badge/skills/pramoddutta/qaskills/jest-mocking-patterns.svg)](https://agentmods.dev/skills/pramoddutta/qaskills/jest-mocking-patterns)
Your own site
<a href="https://agentmods.dev/skills/pramoddutta/qaskills/jest-mocking-patterns"><img src="https://agentmods.dev/badge/skills/pramoddutta/qaskills/jest-mocking-patterns.svg" alt="Measured on agentmods" height="20"></a>
Per session 44 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,462 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 1 finding. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00044 $0.02462
Opus 5 $0.00022 $0.01231
Sonnet 5 $0.00009 $0.00492
Haiku 4.5 $0.00004 $0.00246

Measured 2d ago against content hash 8f0556dc4d94, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-05, from the pricing page.

Security

Grade A, and why

Jest Mocking Patterns scanned grade A with 1 finding against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

const { data } = await axios.get(`/api/users/${id}`);
seed-skills/jest-mocking-patterns/SKILL.md · 245 lines

How it starts

The opening of the file, as written. The whole thing — 245 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Jest Mocking Patterns

This skill makes the agent mock dependencies in Jest deliberately and reversibly: stubbing functions with jest.fn, controlling return values with mockReturnValue/mockResolvedValue, replacing whole modules with jest.mock factories, and spying on real implementations with jest.spyOn (always restored). The guiding rule: mock the boundary, not the unit under test, and always reset state between tests so mocks never leak.

Use this skill when the agent needs to isolate code from the network, the clock, the filesystem, a database client, or any third-party module (axios, fs, a payment SDK).

Core Principles

  1. Mock at the boundary. Mock network/DB/3rd-party clients, not the function you are testing. If you mock the thing under test, the test proves nothing.
  2. Reset mocks between tests. Configure clearMocks: true (or call jest.clearAllMocks() in beforeEach) so call counts and implementations never bleed across tests.
  3. jest.mock is hoisted. Calls to jest.mock('module', factory) are lifted above imports. The factory cannot reference outer variables unless they are prefixed mock.
  4. Prefer spyOn + mockRestore over jest.mock when you only need to override one method and want the real implementation back afterward.
  5. Type your mocks. Use jest.mocked() (or as jest.Mock) so the mock API is type-checked and autocompletes.
  6. Assert behavior, then interactions. Check the result first; use toHaveBeenCalledWith to verify the boundary was called correctly.

Workflow / Patterns

Pattern 1 — jest.fn and controlling return values

jest.fn() is a recording stub. Drive it with mockReturnValue, mockResolvedValue, mockRejectedValue, or queue per-call values with mockReturnValueOnce.

test('jest.fn return value control', () => {
  const calc = jest.fn();

  calc.mockReturnValue(10); // default for every call
  calc.mockReturnValueOnce(1).mockReturnValueOnce(2); // queued, then falls back

  expect(calc()).toBe(1);
  expect(calc()).toBe(2);
  expect(calc()).toBe(10);
  expect(calc).toHaveBeenCalledTimes(3);
});

test('async return values', async () => {
  const fetchUser = jest.fn<Promise<{ id: number }>, [number]>();
  fetchUser.mockResolvedValue({ id: 1 });

  await expect(fetchUser(1)).resolves.toEqual({ id: 1 });

  fetchUser.mockRejectedValueOnce(new Error('not found'));
  await expect(fetchUser(99)).rejects.toThrow('not found');
});

Read the full file on GitHub · 245 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 245 lines · 44 tokens per session scan A 8f0556dc4d94

Subscribe to this mod's changes

Jest Mocking Patterns is a skill published in the GitHub repository PramodDutta/qaskills (217 stars, last pushed 6d ago), licensed MIT. It adds 44 tokens to every session and 2,462 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 1 finding (makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.

Related

Other skills, from other repositories