API Mock Server
Generate a mock API from OpenAPI spec with stateful responses, fault injection, and team sharing.
Design & Schema
Define mock routes from OpenAPI spec, choose delay and fault injection, and the storage model.
Why
Mock servers front-load contract testing. If frontend and backend code against the same OpenAPI mock, integration drops from weeks to days. Fault injection (timeouts, 500s) catches frontend error handling before production, where it costs 10x more to fix.
When
Before frontend and backend diverge. Generate mocks from the OpenAPI spec in week 1 of a new API.
How
Example
Common Problems
- ⚠No OpenAPI spec — frontend and backend agree verbally, drift within a week, integration takes days to debug
- ⚠Static examples only — GET /users always returns the same 3 users, hiding pagination and empty-state bugs
- ⚠No fault injection — frontend never sees a 500 in dev, ships without an error boundary
Best Practices
- ✓Generate the mock from the OpenAPI spec, not by hand — the spec is the single source of truth for both mock and server
- ✓Use Faker for list endpoints so pagination, sorting, and empty states are testable with realistic volume
- ✓Add a fault-injection header (X-Mock-Status: 500) so QA can trigger error paths without code changes
Resources for this stage
Tools
Templates
Implementation & Dynamic Responses
Build stateful mocks, dynamic data via Faker, and conditional routing for realistic demos and tests.
Why
Static mocks break when the frontend needs to test create-then-list flows. Stateful mocks (in-memory store) let CRUD sequences work end-to-end in Storybook and tests without a backend, catching integration bugs before they reach staging.
When
After the static mock serves the happy path. Add stateful routes for CRUD demos and integration tests.
How
Example
Common Problems
- ⚠Stateless mock for a CRUD demo — POST then GET returns stale data, demo looks broken
- ⚠No store reset between tests — test A's created user leaks into test B, causing flaky failures
- ⚠Faker without a seed — generated data changes every run, snapshot tests fail randomly
Best Practices
- ✓Use a stateful in-memory store for CRUD endpoints so create-then-list flows work in demos and tests
- ✓Reset the mock store between test files (beforeEach) to prevent cross-test data pollution
- ✓Seed Faker with a fixed value (faker.seed(123)) in CI so generated data is reproducible
Deployment & Sharing
Deploy as a Docker image, npm package, or hosted service so the whole team shares one mock.
Why
A mock server only helps if the whole team uses the same one. A hosted/shared mock (Docker image or MSW committed to the repo) ensures frontend, mobile, and QA test against identical contracts, eliminating 'works on my machine' integration failures.
When
When the mock is stable for the happy path. Package it before frontend integration begins so there is one source of truth.
How
Example
Common Problems
- ⚠Everyone runs a different mock version — frontend tests pass, integration fails because contracts drifted
- ⚠No CI check on spec-handler sync — handlers go stale, mock returns fields the real API no longer has
- ⚠Shared mock exposed publicly without auth — someone discovers it and probes your API surface
Best Practices
- ✓Commit MSW handlers to the repo and update them in the same PR as the OpenAPI spec — one source of truth
- ✓Add a CI step that regenerates types from the spec and fails on diff — catches spec-handler drift automatically
- ✓Protect any shared hosted mock with an API key header and never deploy it to a public domain without auth
Resources for this stage
Tools
Templates
Full Developer Lifecycle
A 14-stage developer roadmap from idea to maintenance — visualize dependencies and track progress.
Next Step