MSW (Mock Service Worker)
Intercept HTTP at the network layer so tests, Storybook and local development all hit realistic mocked APIs while your real fetching and parsing code runs.
MSW intercepts HTTP requests at the network layer — with a service worker in the browser and request interceptors in Node.
Your application code calls fetch exactly as it would in production; MSW answers. That one design decision is why it’s
become my default for API mocking. The argument for mocking at the network rather than in your code is in
Stop Mocking Fetch.
Minimal setup
// mocks/handlers.ts
import { http, HttpResponse } from "msw";
export const handlers = [
http.get("/api/workouts/:id", ({ params }) =>
HttpResponse.json({ id: params.id, name: "Push day" }),
),
];
// test setup (Node)
import { setupServer } from "msw/node";
export const server = setupServer(...handlers);
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
// one test overrides a handler to exercise the error path
server.use(http.get("/api/workouts/:id", () => new HttpResponse(null, { status: 500 })));
Habits that keep it honest
- Strict by default.
onUnhandledRequest: "error"fails a test that makes a request you forgot to mock. For tests that expect errors, silenceconsole.errorlocally rather than loosening the global setting. - Shared handlers. One set of handlers serves unit tests, integration tests, Storybook and local development.
- Override per test with
server.use()and reset after each test, so tests stay isolated. - Fake data factories (e.g.
@mswjs/data) give you a small in-memory database for realistic flows.
Contract-first development
The underrated use: run MSW in the browser and you have an offline mode. Agree the API contract with the backend team, write handlers from it, and build the UI before the endpoint exists. When the real API arrives, you switch the mock off.
The risk is drift — mocks can lie. Generate handlers from an OpenAPI spec where you can, keep a few contract or end-to-end tests against the real backend, and treat a mismatch as a bug in the contract, not the test.
In practice
I’ve used MSW to drive a coverage push across several services, to power Storybook for a component library, and to build screens against endpoints that didn’t exist yet. The offline mode turned out to be as valuable as the tests: demos and development stopped depending on a shared test environment being up. Pairs naturally with React Testing Strategy and Playwright.
- #msw
- #testing
- #mocking
- #api-contracts
- #developer-experience
↩ Linked from (8)
Mocked the network, not the code. I introduced MSW handlers into the test setup, so tests exercised the real data layer against realistic API responses (Stop Mocking Fetch). Writing tests got quicker, and they survived r…
- API ModellingEvergreen
With a model layer, components test against plain domain objects, and fixture builders make test data readable. Mock at the network so the adapter runs too (MSW (Mock Service Worker)).
- Bulletproof ReactGrowing
…W in the browser, with factory-built fake data, lets you build whole features before the backend exists — see MSW (Mock Service Worker). Name tests by intent: .test.ts for unit, .spec.ts for integration, stories for visu…
- ConvexSeed
Related: MSW (Mock Service Worker) · What Makes an AI Agent
- React Testing StrategyEvergreen
…Unit | Fast, simple, no React needed | | Hooks, components, whole pages | Integration with Testing Library + MSW (Mock Service Worker) | Exercises real data fetching, state and rendering together | | Critical user journ…
Failure fixtures at the network boundary. With MSW (Mock Service Worker) the same handlers can serve slow, failing and malformed responses to unit tests, Storybook and local development. Make the unhappy states first-cla…
- Stop Mocking FetchEvergreen
Mock at the network boundary with MSW. Define server handlers once, for the whole API, as if the app were talking to a real backend. They aren't specific to any one test.
- TDD as a Feedback LoopGrowing
…ubbing fetch in every test bakes assumptions into each one. Intercepting requests at the network layer (I use MSW (Mock Service Worker)) keeps the real data layer in play and lets the same scenarios drive tests, Storyboo…
↪ Links to (3)
Related by topic
- Growingcase study
Building a Graph Component Library
Frontend craft - Growingcase study
Building a Low-Code BPMN Workflow Platform
Frontend craft - Growingcase study
Chairing a Cross-Team UI Guild
Career - Growingcase study
Driving a TypeScript Codebase to Zero Errors
Frontend craft