MSW (Mock Service Worker)
API mocking library that intercepts HTTP requests at the network level using Service Workers (browser) or native Node.js interceptors. MSW mocks work identically in browser and Node.js without changing application code — no dependency injection or adapter patterns needed. Define request handlers once, use them in development (browser), integration tests (Node.js/Vitest/Jest), and E2E tests (Playwright/Cypress). Considered the gold standard for frontend API mocking.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
MIT open source. No external network calls. Service Worker sandboxed in browser. No credentials or auth required. Excellent security profile for a local dev/testing tool.
⚡ Reliability
Best When
You're building web frontends or full-stack JavaScript apps and want network-level API mocking that works identically across development, unit tests, and E2E tests.
Avoid When
You need server-side API mocking, non-JavaScript stack mocking, or want a GUI for configuring mocks — Mockoon or WireMock are better fits.
Use Cases
- • Mock REST and GraphQL API responses in React/Vue/Angular apps during development without a backend running
- • Use identical mock handlers in Vitest/Jest unit tests and Playwright E2E tests without maintaining separate mock configurations
- • Simulate API error states (500 errors, network timeouts, partial data) for comprehensive frontend error handling testing
- • Develop agent frontend interfaces against mocked agent API responses before the agent backend is implemented
- • Mock third-party API integrations (Stripe, Twilio, etc.) in test suites to avoid real API calls and costs
Not For
- • Backend API testing — MSW intercepts requests from the client side; use Mockoon or WireMock for server-side API mocking
- • Non-browser/non-Node.js environments — MSW targets web and Node.js; mobile (React Native) needs @mswjs/react-native
- • Performance/load testing — MSW is for functional mocking, not simulating latency or load
Interface
Authentication
Pure JavaScript library — no auth required. Runs in browser (Service Worker) or Node.js process. No external network calls.
Pricing
MIT open source, no commercial offering. Free forever.
Agent Metadata
Known Gotchas
- ⚠ MSW 2.x has breaking changes from 1.x — rest.get/rest.post replaced by http.get/http.post and require using Response/HttpResponse instead of res(ctx.json(...)); migration required
- ⚠ Browser Service Worker requires HTTPS or localhost — agents serving MSW over non-HTTPS non-localhost URLs get cryptic Service Worker registration errors
- ⚠ MSW's Service Worker must be served from the same origin as the app — copying mockServiceWorker.js to the public directory is required and easy to forget
- ⚠ Unhandled request warnings can flood console during development — configure onUnhandledRequest to 'warn' or 'bypass' based on desired behavior
- ⚠ MSW handler order matters — first matching handler wins; agents defining overlapping handlers must order from most specific to least specific
- ⚠ MSW doesn't intercept requests made directly to localhost from Node.js tests unless passthrough mode is configured — verify handler coverage includes all request patterns in tests
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for MSW (Mock Service Worker).
Scores are editorial opinions as of 2026-03-06.