Skip to content

commandTesting

Mock External API

Builds a realistic mock of an external API for your tests, shaped from recorded or documented responses.

npx workspacetemplates add mock-external-api --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/mock-external-api.md15 lines · 1.2 KBFile
---
description: Builds a realistic mock of an external API for your tests, shaped from recorded or documented responses.
argument-hint: "[API name and the module that calls it]"
---

Create a mock of this external API for tests: $ARGUMENTS

1. Find where the code calls the API (client module, SDK wrapper, fetch calls). List the endpoints, methods and fields the app actually uses.
2. Gather real shapes from, in order: fixtures or logs already in the repo, the provider's OpenAPI file or docs, and a sample response the user pastes. Do not call the live API and do not read credentials.
3. Pick the project's mocking tool (msw, nock, responses, WireMock, httpmock, vcr, Mockito) and build handlers there. Keep the mock in one folder with one file per endpoint group.
4. Cover: a success reply, an empty result, pagination, a 4xx validation error, a 429 with Retry-After, a 5xx, and a timeout. Use realistic ids and timestamps.
5. Add a contract test that checks each fixture against the schema (zod, JSON Schema or types) so drift is caught.
6. Replace one existing slow test to prove it works, then run the suite offline.

Report files created, scenarios covered and which fields were guessed rather than documented.

What it does

Tests that call real services are slow and break on someone else's outage, but hand-made mocks drift from reality. This command builds the mock from real response shapes, including error replies, pagination and rate limits.

It adds a small contract check, so when the provider changes a field you find out from a failing test instead of production.

Example

you › /mock-external-api Stripe customers and refunds in src/payments

agent › Created tests/mocks/stripe/ (customers.ts, refunds.ts) with msw handlers for 7 scenarios and a schema check on each fixture.
Payments tests now run offline in 2 s instead of 19 s.
Guessed: refund.failure_reason values (not in samples). Confirm with the docs?

Questions

Does it call the real API?
No. It builds from fixtures, documentation or samples you provide.
How are stale mocks caught?
A contract test validates each fixture against a schema, so a provider change shows up as a failing test.
  • Add Edge Case Tests

    CommandTesting

    Free
  • Add Regression Test

    CommandTesting

    Free
  • Coverage Gaps

    CommandTesting

    Free
  • E2E Scenario

    CommandTesting

    Free