Support Ukraine πŸ‡ΊπŸ‡¦ Support Ukrainian Military to Win The War.
LOGOMock Service Worker

API mocking of the next generation

Mock by intercepting requests on the network level. Seamlessly reuse the same mock definition for testing, development, and debugging.

1import { setupWorker, rest } from 'msw'
2
3const worker = setupWorker(
4 rest.post('/login', async (req, res, ctx) => {
5 const { username } = await req.json()
6
7 return res(
8 ctx.json({
9 username,
10 firstName: 'John'
11 })
12 )
13 }),
14)
15
16worker.start()

Seamless

Dedicated layer of requests interception at your disposal. Keep your application's code and tests unaware whether something is mocked or not.

Deviation-free

Request the same production resources and test the actual behavior of your app. Augment an existing API, or design it as you go, when there is none.

Familiar & Powerful

Use Express-like routing syntax to capture outgoing requests. Parameters, wildcards, regular expressionsβ€”mocking has never been easier.


I found MSW and was thrilled that not only could I still see the mocked responses in my DevTools, but that the mocks didn't have to be written in a Service Worker and could instead live alongside the rest of my app. This made it silly easy to adopt. The fact that I can use it for testing as well makes MSW a huge productivity booster.

Kent C. Dodds

Kent C. Dodds

Teacher, Google Developer Expert

Why Mock Service Worker?

  • Interception on the network level
  • Composable functional syntax
  • Standardized Service Worker API
  • Client-side execution
  • Support of REST API and GraphQL
  • Native TypeScript support

Trusted by engineers at

illustration

Integrate anywhere

Perfect match for your setup.

API mocking that is available anywhere: during development, on any level of testing, and even debugging. Living in a dedicated layer, Mock Service Worker is agnostic of the frameworks, libraries, or setups you may use.

See usage example with your setup
eshop.com/product/1

given I wrote a test suite with MSW

treats API response as a pre-requisite
tests how a user actually interacts with my app
produces a maintainable and resilient test

Test suite using a GET /product/:productId mock.

Test with confidence

Write test suites that don't smell.

You don't expect your customers to mock fetch, do you? So don't expect your tests either. Target any state of your API while testing your application exactly how your users interact with it.

Learn about API mocking in tests with Kent C. Dodds

Develop incrementally

Modern solution for competitive development.

Present a fully functioning application or kick off the next successful startup without having any backend at all. RESTful API today, or GraphQL tomorrow? Experiment, combine, and find what suits your project best before committing to the ecosystem.

Explore REST API integration

Explore GraphQL API integration

1rest.get('/books', (req, res, ctx) => {
2 // So the issue is when there's a single book!
3 return res(
4 ctx.json([
5 {
6 id: 'ea42ffcb-e729-4dd5-bfac-7a5b645cb1da',
7 title: 'The Lord of the Rings',
8 publishedAt: -486867600
9 }
10 ])
11 )
12})

Debug like a pro

Mock the very response that crashes your app.

Triage, bisect, and eliminate API-related issues without resetting the state of your application. With the unique ability to intercept requests to any origins, finally feel like you are in charge on what happens with the traffic.

teachers-diagram

Teach what matters

API for your next lesson in a matter of minutes.

Let your students and yourself focus on the topic, while Mock Service Worker provides a reliable API substitution that you don't need to run, and that behaves the same between different OS.

Enroll in the "Epic React" course featuring MSW