testing-with-api-mocks

A guide for using API mocks in tests. API mocks are pretend backend responses that let tests run without contacting a real service.

In plain words
What is it for?
Use it when a test calls an API, especially when creating, changing, or refactoring tests for React components that load or submit data.
Why use it?
It removes the need to manually prepare basic fake API responses and explains how responses are generated and saved for later 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/stacklok/toolhive-studio/testing-with-api-mocks
Any agent
npx skills add stacklok/toolhive-studio --skill testing-with-api-mocks
Clone the repo
git clone --depth 1 https://github.com/stacklok/toolhive-studio

Made for: Claude Code, Codex.

Per session 44 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,438 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. 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 $0.00044 $0.01438
Opus 5 $0.00022 $0.00719
Sonnet 5 $0.00009 $0.00288
Haiku 4.5 $0.00004 $0.00144

Measured 2d ago against content hash 4d26b466a92a, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

testing-with-api-mocks scanned grade A with 0 findings 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.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.claude/skills/testing-with-api-mocks/SKILL.md · 199 lines

How it starts

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

Testing with API Mocks

This is the starting point for all API mocking in tests. Read this skill first before working on any test that involves API calls.

This project uses MSW (Mock Service Worker) with auto-generated schema-based mocks. When writing tests for code that calls API endpoints, mocks are created automatically.

How It Works

  1. Run a test that triggers an API call (e.g., a component that fetches data)
  2. Mock auto-generates if no fixture exists for that endpoint
  3. Fixture saved to renderer/src/common/mocks/fixtures/<endpoint>/<method>.ts
  4. Subsequent runs use the saved fixture

No manual mock setup is required for basic tests.

Fixture Location

Fixtures are organized by endpoint path and HTTP method:

renderer/src/common/mocks/fixtures/
├── groups/
│   ├── get.ts          # GET /api/v1beta/groups
│   └── post.ts         # POST /api/v1beta/groups
├── workloads/
│   └── get.ts          # GET /api/v1beta/workloads
├── workloads_name/
│   └── get.ts          # GET /api/v1beta/workloads/:name
└── ...

Path parameters like :name become _name in the directory name.

Fixture Structure

Generated fixtures use the AutoAPIMock wrapper with types from the OpenAPI schema:

// renderer/src/common/mocks/fixtures/groups/get.ts
import type {
  GetApiV1BetaGroupsResponse,
  GetApiV1BetaGroupsData,
} from '@common/api/generated/types.gen'
import { AutoAPIMock } from '@mocks'

export const mockedGetApiV1BetaGroups = AutoAPIMock<
  GetApiV1BetaGroupsResponse,
  GetApiV1BetaGroupsData
>({
  groups: [
    { name: 'default', registered_clients: ['client-a'] },
    { name: 'research', registered_clients: ['client-b'] },
  ],
})

The second type parameter (*Data) provides typed access to request parameters (query, path, body) for conditional overrides.

Naming Convention

Export names follow the pattern: mocked + HTTP method + endpoint path in PascalCase.

  • GET /api/v1beta/groupsmockedGetApiV1BetaGroups
  • POST /api/v1beta/workloadsmockedPostApiV1BetaWorkloads
  • GET /api/v1beta/workloads/:namemockedGetApiV1BetaWorkloadsByName

Read the full file on GitHub · 199 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 · 199 lines · 44 tokens per session scan A 4d26b466a92a

Subscribe to this mod's changes

testing-with-api-mocks is a skill published in the GitHub repository stacklok/toolhive-studio (163 stars, last pushed 4d ago), licensed Apache-2.0. It adds 44 tokens to every session and 1,438 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.

Related

Other skills, from other repositories

tui-tester

Expert guidance for testing Gemini CLI behavior and visual output using terminal automation.

google-gemini/gemini-cli · 19 tokens

e2e-test

Skill "e2e-test" from maximhq/bifrost, covering playwright e2e testing, usage, workflow overview, auto-update workflow (sync mode) and step 0: detect what changed.

maximhq/bifrost · 0 tokens

harness-test-writer

Add regression test cases to the Bifrost provider harness (the Postman collection run via make run-provider-harness-test) based on a merged PR or a GitHub issue. Fetches the PR/issue, traces the affected wire path in the codebase, checks existing harness coverage, designs cases following harness conventions, inserts…

maximhq/bifrost · 133 tokens

agent-tests

MUST READ before calling RunAgentTests or touching agent-tier test infrastructure -- these tests spawn real AI clients (Claude Code, Codex) and SPEND SUBSCRIPTION USAGE; never run casually. Tier model, async runner rationale, auth/isolation rules.

dylanroscover/Embody · 56 tokens

agent-host-e2e-tests

Use when writing, recording, updating, or troubleshooting the agent host end-to-end tests under src/vs/platform/agentHost/test/node/e2e (black-box tests that drive the whole agent host over the AHP protocol, using a CapiReplayProxy record/replay system for Claude/Copilot/Codex). Covers adding a cross-provider test…

microsoft/vscode · 104 tokens

quality

Evaluates whether a GitHub issue is spam, empty, needs more information, or is OK to proceed.

google-gemini/gemini-cli · 24 tokens