32

Mock Service Worker

 3 years ago
source link: https://mswjs.io/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

MSW – Seamless API mocking library for browser and Node

Mock 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'
3const worker = setupWorker(
4 rest.post('/login', (req, res, ctx) => {
5 const { username } = req.body
7 return res(
8 ctx.json({
9 username,
10 firstName: 'John'
13 }),
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 do people fall in love with Mock Service Worker?

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

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 Sercive 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

rest.get('/books', (req, res, ctx) => {
// So the issue is when there's a single book!
return res(
ctx.json([
id: 'ea42ffcb-e729-4dd5-bfac-7a5b645cb1da',
title: 'The Lord of the Rings',
publishedAt: -486867600

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK