Open-source API simulation and mocking tool that lets developers and testers replace flaky or slow API dependencies with reusable simulations.
mswjs/msw
A JavaScript API mocking library that intercepts network requests in browsers and Node.js, so app code runs unchanged while tests and development use mock responses.
- Stars
- 18,307
- Forks
- 642
- Open issues
- 16
- Last commit
- 8 days ago
What is it?
Mock Service Worker (MSW) mocks API requests at the network level. In the browser it uses the Service Worker API to respond to requests with your mock definitions, and in Node.js it uses a low-level interception layer. Because the application's own code, including fetch, axios and data-fetching libraries, is left untouched, the app behaves as it would in production.
Request handlers are written with an Express-like routing syntax that supports parameters, wildcards and regular expressions. Responses can set status codes, headers, cookies and delays, or be produced by fully custom resolvers. The same handlers can be reused across unit tests, integration tests, E2E tests and local development, and responses are visible in the browser's DevTools Network tab.
The project provides setupWorker for browser use and setupServer for Node.js, with a documentation site and example repositories for common setups.
Key features
- 01Intercepts requests at the network level, so application code runs unmodified.
- 02Works in browsers via Service Worker and in Node.js for tests and servers.
- 03Express-like route matching with parameters, wildcards and regular expressions.
- 04Custom status codes, headers, cookies, delays and resolver functions for responses.
- 05Mocked responses are visible in the browser DevTools Network tab.
Use cases
- Developing frontend features before the backend API is ready.
- Writing unit, integration and E2E tests that mock HTTP and GraphQL calls.
- Simulating slow or failing endpoints to test loading and error states.
- Running a local Express server against stubbed third-party APIs.
Quick start
- 1
Install the package as a dev dependency
npm i msw --save-dev - 2
Read the official quick start guide to set up request handlers
Things to know
- Browser mocking requires generating and shipping a Service Worker file with your app.
In collections
Topics
FAQ
- What is msw?
- A JavaScript API mocking library that intercepts network requests in browsers and Node.js, so app code runs unchanged while tests and development use mock responses. Mock Service Worker (MSW) mocks API requests at the network level. In the browser it uses the Service Worker API to respond to requests with your mock definitions, and in Node.js it uses a low-level interception layer. Because the application's own code, including fetch, axios and data-fetching libraries, is left untouched, the app behaves as it would in production.
- Is msw free to use? What license does it have?
- msw is open source under the MIT license. Check the license terms before using it in commercial products.
- Is msw still maintained?
- Actively maintained. The last commit to msw was on Oct 17, 2026.
- What can I use msw for?
- Developing frontend features before the backend API is ready. Writing unit, integration and E2E tests that mock HTTP and GraphQL calls. Simulating slow or failing endpoints to test loading and error states. Running a local Express server against stubbed third-party APIs.
- How do I install msw?
- Install the package as a dev dependency: npm i msw --save-dev Read the official quick start guide to set up request handlers
- Where can I get msw?
- The source code and installation instructions are on GitHub: https://github.com/mswjs/msw
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
Jest is a JavaScript testing framework with built-in assertions, mocking, snapshot testing and an interactive watch mode.
A Go testing toolkit providing assertions, mocks, and a suite structure that works with the standard go test runner.
A sandbox for developing and testing React UI components in isolation, outside of your full application.
Node.js test automation framework for end-to-end, unit and component testing in browsers and mobile apps via WebDriver, BiDi and Appium.
A local SMTP server that catches outgoing email during development and shows it in a web interface.