Skip to content

MSW

A JavaScript library that intercepts API requests so your app can use mock responses.

Stars
18.3k
Forks
644
Licence
MIT
Language
TypeScript
Last update
20 days ago
Type
Library

What it is

Mock Service Worker (MSW) lets developers fake the responses of network requests made by their app. It works in the browser by using the Service Worker API, and in Node.js by intercepting requests at a low level, so the app's own code can run unchanged. Developers use it for local development, debugging, and automated tests.

  • Return a fake JSON response for a GET request while a backend endpoint is still being built
  • Reuse the same request handlers in tests and in local development
  • Use URL parameters and wildcards to match requests and return custom status codes, headers, or delays
  • Scope mocks to a single Express route with server.boundary()

Great for

  • Frontend developers who need to work before a backend API is ready
  • Teams that want the same mock definitions for unit, integration, and end-to-end tests
  • Developers who want mocked responses to show up in the browser DevTools Network tab
  • Node.js developers who want to mock outgoing requests from an Express server during development

Not ideal for

  • People who only need static fixture files and no request interception (the README does not describe such a use case, so a simpler tool may be enough)
  • Non-developers, since the library is configured with JavaScript or TypeScript code
Step-by-step guide: how to use MSWInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Node.js

    Clone the repository

    git clone https://github.com/mswjs/msw
  2. 2

    Run it

    • Read the Quick start guide at https://mswjs.io/docs/quick-start
    • Import http and HttpResponse from 'msw/http' and setupWorker from 'msw/browser' (or setupServer from 'msw/node' for Node.js)
    • Define request handlers with http.get() and return HttpResponse.json()
    • Call await worker.start() in the browser or server.listen() in Node.js
    • Make a request in your app and inspect the mocked response in the DevTools Network tab
  3. 3

    Get the most out of it

    • Use server.boundary() to limit interception to a specific closure in Node.js
    • Reuse one set of request handlers across unit, integration, and E2E tests
    • Use parameters like :id and wildcards to match families of URLs
    • Refer to the official docs at https://mswjs.io/docs for the full API, including setup-worker and setup-server

Downloads

Strengths

  • Intercepts requests at the network level, so the app code does not need to be changed for mocking
  • Uses Express-like routing syntax with parameters, wildcards, and regular expressions
  • The same request handlers can be used in the browser and in Node.js

Trade-offs

  • Requires understanding of Service Workers for browser setup, which adds some learning effort
  • Documentation and quick start are hosted on an external site, so the README alone gives limited setup detail
  • The README describes it as maintained in spare time, so project capacity may vary

How it compares

ProjectStarsTypeLevelUpdatedBest for
msw (this)18.3kLibraryintermediate20 days agoFrontend developers who need to work before a backend API is ready
nock13.1kLibraryintermediate7 days agoDevelopers writing automated tests for Node.js code that makes HTTP calls
mockito15.5kLibrarybeginner8 days agoJava developers writing unit tests for classes with external dependencies
json-server75.7kDev toolbeginner8 months agoFront-end developers who need a backend stub while the real API is not ready
workbox13kLibraryintermediate8 days agoWeb developers building Progressive Web Apps
fastapi105kLibrarybeginner4 days agoPython developers building JSON APIs or back-end services

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.