PromptFork

Build an integration test harness with a mock API server

Produces an integration test harness that spins up a mock API server with recorded fixtures, and asserts your client behaves across success, error, retry, and streaming cases deterministically and offline.

Open in Studio
Prompt
You are a senior engineer who writes deterministic integration tests for API clients.

Build an integration test harness with a mock server for an API client. Context:
- Client under test: [DESCRIBE — e.g. 'an OpenAI chat client with retries and streaming']
- API shape: [OpenAI / Anthropic / custom REST — describe the endpoints]
- Language and test runner: [TypeScript + Vitest / Python + pytest / other]
- Mock server: [MSW / nock / httpretty / a real local server]
- Scenarios to cover: [LIST — e.g. 'happy path, 429-then-success, 500-give-up, abort mid-stream, malformed JSON']

Build a harness with:
1. A mock server that starts and stops per test (or per suite) on an ephemeral port, so tests are isolated and parallel-safe.
2. Fixtures for each scenario — canned request/response pairs, including streaming responses served as SSE and malformed or error payloads.
3. Assertions that the client behaves correctly per scenario: success returns typed data; 429 triggers a retry then succeeds; 500 after max retries raises the right typed error; abort cancels an in-flight stream; a malformed response raises a parse or validation error.
4. Request verification: assert the client sent the right method, path, headers (auth present, no leaked secrets), and body for each scenario.
5. Determinism guarantees: no real network, no flaky sleeps (use fake timers or event-based waits), fixed ports or dynamic with capture.
6. A clear way to add a new scenario from a real captured response (record-once, replay-after), documented in a comment.

Requirements:
- Tests must pass offline in CI with no API key and no internet.
- Each test isolates state; no cross-test leakage.
- Secrets are never present in fixtures or logs; redact on capture.

Output, in this exact order:
1. A design overview (mock server, fixtures, isolation, determinism).
2. The harness setup file (server start/stop, helpers).
3. At least four representative scenario tests, one per category: success, transient-error-then-retry, give-up-after-max-retries, abort-mid-stream.
4. The 'add a new scenario' guide with a worked example.
5. A checklist: offline, parallel-safe, no sleeps, no secrets in fixtures.

Success signal: the output is good only if the full suite passes offline with no API key, each scenario asserts both correct client behavior and correct outbound request shape, and adding a new recorded scenario is documented and mechanical.

Use case

Use when you need to test an API client end-to-end without hitting the real provider, with deterministic, replayable scenarios for rate limits, errors, and streaming.

When to use this

When adding integration tests for an external API client. Not for unit-testing pure logic that has no I/O.

Follow-up prompts

  • Add a record/replay proxy that captures real responses once, then replays them offline.
  • Generate chaos scenarios (random latency, dropped connections) and assertions for each.
  • Add a contract test that diffs the mock fixtures against the live API schema.
#testing#api#integration-tests#mock-server#typescript
Source
promptfork seed
License
CC-BY-4.0
Published
6/22/2026

More prompts you might like

Build a typed API client wrapper with retries, streaming, and structured errors

Produces a production-grade API client wrapper around an OpenAI/Anthropic-style API with typed requests and responses, streaming, exponential-backoff retries, and structured errors, instead of a bare fetch that dies on the first 429.

#api#typescript
New

Build a rate-limit and cost-tracking middleware layer for API calls

Produces a middleware layer that enforces client-side rate limits, tracks token and request spend against a budget, and short-circuits before you blow a quota, with typed budgets, alerts, and an observability hook.

#api#rate-limiting
New
Editor’s pickJournaling & Self-ReflectionSeed

Turn ChatGPT into a gentle shadow work guide

A prompt that makes AI lead a real shadow-work session — one probing question at a time, reflecting patterns back, ending with an integration practice.

0002

Prompt: an endless creative writing prompt generator

Give it your genre and tone; get original story sparks on demand — each with a character, a situation, and built-in conflict.

0001

Midjourney logo & brand mark — scalable marks that pass the favicon test

Vector-ready logos built on negative space and geometric precision — includes the favicon scalability test, two style examples (monoline vs emblem), and the Ideogram workflow for adding text that doesn't look garbled.

0001
Editor’s pickGame DevelopmentSeed

Open-world (GTA-style) game build prompt

Scopes a 3D open-world prototype realistically — character controller + drivable vehicle + map first, bigger systems phased.

0001