Mock Service Worker
Seamless REST/GraphQL API mocking library for browser and Node.js.
README
Mock Service Worker
Mock Service Worker (MSW) is a seamless REST/GraphQL API mocking library for browser and Node.js.
Features
"_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._"
Documentation
Examples
Browser
How does it work?
How is it different?
Usage example
- ``` js
- // src/mocks.js
- // 1. Import the library.
- import { setupWorker, rest } from 'msw'
- // 2. Describe network behavior with request handlers.
- const worker = setupWorker(
- rest.get('https://github.com/octocat', (req, res, ctx) => {
- return res(
- ctx.delay(1500),
- ctx.status(202, 'Mocked status'),
- ctx.json({
- message: 'Mocked response JSON body',
- }),
- )
- }),
- )
- // 3. Start request interception by starting the Service Worker.
- worker.start()
- ```
Tip: Did you know that although Service Worker runs in a separate thread, your mock definition executes entirely on the client? This way you can use the same languages, like TypeScript, third-party libraries, and internal logic to create the mocks you need.
Node.js
How does it work?
How is it different?
Usage example
- ``` js
- // test/Dashboard.test.js
- import React from 'react'
- import { rest } from 'msw'
- import { setupServer } from 'msw/node'
- import { render, screen, waitFor } from '@testing-library/react'
- import Dashboard from '../src/components/Dashboard'
- const server = setupServer(
- // Describe network behavior with request handlers.
- // Tip: move the handlers into their own module and
- // import it across your browser and Node.js setups!
- rest.get('/posts', (req, res, ctx) => {
- return res(
- ctx.json([
- {
- id: 'f8dd058f-9006-4174-8d49-e3086bc39c21',
- title: `Avoid Nesting When You're Testing`,
- },
- {
- id: '8ac96078-6434-4959-80ed-cc834e7fef61',
- title: `How I Built A Modern Website In 2021`,
- },
- ]),
- )
- }),
- )
- // Enable request interception.
- beforeAll(() => server.listen())
- // Reset handlers so that each test could alter them
- // without affecting other, unrelated tests.
- afterEach(() => server.resetHandlers())
- // Don't forget to clean up afterwards.
- afterAll(() => server.close())
- it('displays the list of recent posts', async () => {
- render(<Dashboard />)
- // 🕗 Wait for the posts request to be finished.
- await waitFor(() => {
- expect(
- screen.getByLabelText('Fetching latest posts...'),
- ).not.toBeInTheDocument()
- })
- // ✅ Assert that the correct posts have loaded.
- expect(
- screen.getByRole('link', { name: /Avoid Nesting When You're Testing/ }),
- ).toBeVisible()
- expect(
- screen.getByRole('link', { name: /How I Built A Modern Website In 2021/ }),
- ).toBeVisible()
- })
- ```
Don't get overwhelmed! We've prepared a step-by-step [Getting started](https://mswjs.io/docs/getting-started/install) tutorial that you can follow to learn how to integrate Mock Service Worker into your project.
Sponsors
Golden Sponsors
Become our first golden sponsor and get featured right here, enjoying other perks like issue prioritization and a personal consulting session with us.
Learn more on our GitHub Sponsors profile.
Silver Sponsors
Become our _silver sponsor_ and get your profile image and link featured right here.
Learn more on our GitHub Sponsors profile.
Bronze Sponsors
Become our first _bronze sponsor_ and get your profile image and link featured in this section.
Learn more on our GitHub Sponsors profile.
Awards & Mentions
Solution Worth PursuingTechnology Radar (2020–2021) | |
The Most Exciting Use of TechnologyOpen Source Awards (2020) |