MSW (Mock Service Worker)
Seamless REST and GraphQL API mocking for browser and Node.js
Repository Health
Technical Analysis
MSW (Mock Service Worker) intercepts outgoing requests at the network level rather than patching fetch/XMLHttpRequest in your application code. In the browser it uses the Service Worker API to intercept requests before they leave the page; in Node.js it patches the underlying request-dispatching layer. Either way, your application code and tests remain unaware that responses are mocked — they see the same requests and responses they would in production.
Handlers are defined with an Express-like routing syntax (http.get('/users/:id', resolver), graphql.query(...)), supporting path parameters, wildcards, and regular expressions, with resolvers able to return custom status codes, headers, cookies, delays, or streaming responses. The same handler definitions work across unit tests, integration tests, Storybook, and live browser development, so mocks are written once and reused everywhere.
What You Get
setupWorker()for browser-based interception via a generated Service Worker script, andsetupServer()for Node.js test environments- Express-like
http.get/post/put/...andgraphql.query/mutationhandler syntax with path params, wildcards, and regex matching - A CLI (
msw init) to scaffold the Service Worker file into your public/static directory - Composable resolvers supporting custom status codes, headers, cookies, response delays, and streaming bodies
- Life-cycle events and request/response logging for debugging which handlers matched a given request
Common Use Cases
- Mocking backend REST/GraphQL APIs in unit and integration tests without patching
fetchor spinning up a real server - Running Storybook stories or local development against mocked API responses before the real backend endpoint exists
- Simulating error states, slow networks, or edge-case API responses that are hard to reproduce against a real backend
- Sharing one set of mock handlers between tests, Storybook, and browser-based manual QA to avoid mock drift
Under The Hood
Architecture - MSW’s src/core module holds the runtime-agnostic pieces (handler matching, resolver execution, life-cycle events), while src/browser implements Service-Worker-based interception (generating and registering mockServiceWorker.js) and src/node implements interception by patching Node’s request-dispatch internals via an interceptors library — both runtimes funnel matched requests through the same core resolver pipeline so handler code is 100% shared. Tech Stack - Written in TypeScript (97% of the codebase) with a dual CJS/ESM build produced by tsup, tests run under Vitest, and the package ships separate browser, node, native, and iife entry points via conditional exports in package.json for each target environment. Code Quality - The test/ directory mirrors src/ with dedicated suites per interception target plus a large integration-test surface, and knip.json/commitlint.config.js indicate enforced unused-export detection and commit-message conventions; the project has 181 contributors and consistent release cadence. API Design - The Express-like http.get(path, resolver) syntax gives JS/TS developers a near-zero learning curve, graphql.query/mutation mirrors the same pattern for GraphQL, and life-cycle events (server.events.on('request:start', ...)) provide debuggability without extra tooling.
Used by 57 apps in this directory
Cal.diy
Scheduling
The 100% MIT-licensed, community-driven scheduling platform — self-host your own booking infrastructure with no enterprise strings attached.
Coder
Code Editors · Developer Tools · Devops
Self-hosted cloud development environments and AI coding agents — defined in Terraform, connected via WireGuard, automatically shut down when idle.
CubeSandbox
AI Agents · Developer Tools · Security
Instant, concurrent, hardware-isolated MicroVM sandboxes for AI agents — E2B-API compatible, sub-60ms cold starts, and a built-in zero-trust egress proxy, all self-hostable at scale.
Fern
Developer Tools
Fern turns a single OpenAPI, AsyncAPI, or Protobuf definition into type-safe SDKs for nine languages and a hosted API documentation site, all from one CLI and one source of truth.
Filestash
File Storage
A self-hosted file management platform that unifies access to S3, SFTP, SMB, FTP, WebDAV, NFS, Git, SharePoint, and 20+ other storage backends through a single extensible web interface.
Ghost
Blogging · CMS
Open source headless Node.js CMS for professional publishing, paid memberships, and newsletters with a fully owned audience.
GitLab
Developer Tools · Devops
The complete DevOps platform that unifies Git hosting, CI/CD, issue tracking, and security scanning into a single self-hostable application.
Grafana
Analytics · Monitoring
The open-source observability platform that unifies metrics, logs, and traces from any data source into dynamic, queryable dashboards.
HyperDX
Analytics · Developer Tools · Monitoring
Open source observability platform that unifies logs, traces, metrics, and session replays on ClickHouse — now the core of ClickStack.