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'23const worker = setupWorker(4 rest.post('/login', async (req, res, ctx) => {5 const { username } = await req.json()67 return res(8 ctx.json({9 username,10 firstName: 'John'11 })12 )13 }),14)1516worker.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
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
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 setupgiven I wrote a test suite with MSW
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.
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.
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: -4868676009 }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.
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