From 80704757472a52b3df103f61e368ca0c707ae6a8 Mon Sep 17 00:00:00 2001
From: Artem Zakharchenko
Mock Service Worker (MSW) is an API mocking library for browser and Node.js.
+Mock Service Worker (MSW) is a seamless REST/GraphQL API mocking library for browser and Node.js.
-
-
-
@@ -27,8 +24,8 @@
## Features
- **Seamless**. A dedicated layer of requests interception at your disposal. Keep your application's code and tests unaware of 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](https://github.com/expressjs/express)-like routing syntax to capture outgoing requests. Use parameters, wildcards, and regular expressions to match requests, and respond with necessary status codes, headers, cookies, delays, or completely custom resolvers.
+- **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](https://github.com/expressjs/express)-like routing syntax to capture requests. Use parameters, wildcards, and regular expressions to match requests, and respond with necessary status codes, headers, cookies, delays, or completely custom resolvers.
---
@@ -38,6 +35,8 @@
## Documentation
+This README will give you a brief overview on the library but there's no better place to start with Mock Service Worker than its official documentation.
+
- [Documentation](https://mswjs.io/docs)
- [**Getting started**](https://mswjs.io/docs/getting-started/install)
- [Recipes](https://mswjs.io/docs/recipes)
@@ -54,27 +53,27 @@
### How does it work?
-Browser usage is what sets Mock Service Worker apart from other tools. Utilizing the [Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API), which can intercept requests for the purpose of caching, Mock Service Worker responds to captured requests with your mock definition on the network level. This way your application knows nothing about the mocking.
+In-browser usage is what sets Mock Service Worker apart from other tools. Utilizing the [Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API), which can intercept requests for the purpose of caching, Mock Service Worker responds to captured requests with your mock definition on the network level. This way your application knows nothing about the mocking.
-**Watch a 30 seconds explanation on how Mock Service Worker works in a browser:**
+**Take a look at this quick presentation on how Mock Service Worker functions in a browser:**
[![What is Mock Service Worker?](https://raw.githubusercontent.com/mswjs/msw/main/media/msw-video-thumbnail.jpg)](https://youtu.be/HcQCqboatZk)
### How is it different?
-- Intercepts requests on the network level, not the application level.
-- If you think of your application as a box, Mock Service Worker lives in its own box next to yours, instead of opening and altering it for the purpose of mocking.
-- Agnostic of request-issuing libraries, so you can use it with `fetch`, `axios`, `react-query`, you-name-it.
-- The same mock definition can be reused for unit, integration, E2E testing, and debugging.
+- This library intercepts requests on the network level, which means _after_ they have been performed and "left" your application. As a result, the entirety of your code runs, giving you more confidence when mocking;
+- Imagine your application as a box. Every API mocking library out there opens your box and removes the part that does the request, placing a blackbox in its stead. Mock Service Worker leaves your box intact, 1-1 as it is in production. Instead, MSW lives in a separate box next to yours;
+- No more stubbing of `fetch`, `axios`, `react-query`, you-name-it;
+- You can reuse the same mock definition for the unit, integration, and E2E testing. Did we mention local development and debugging? Yep. All running against the same network description without the need for adapters of bloated configurations.
### Usage example
```js
// src/mocks.js
-// 1. Import mocking utils.
+// 1. Import the library.
import { setupWorker, rest } from 'msw'
-// 2. Define request handlers and response resolvers.
+// 2. Describe network behavior with request handlers.
const worker = setupWorker(
rest.get('https://github.com/octocat', (req, res, ctx) => {
return res(
@@ -87,7 +86,7 @@ const worker = setupWorker(
}),
)
-// 3. Start the Service Worker.
+// 3. Start request interception by starting the Service Worker.
worker.start()
```
@@ -95,103 +94,96 @@ Performing a `GET https://github.com/octocat` request in your application will r
![Chrome DevTools Network screenshot with the request mocked](https://github.com/mswjs/msw/blob/main/media/msw-quick-look-network.png?raw=true)
-> **Tip:** Did you know that although Service Worker runs in a separate thread, your mock definition executes on the client-side? That way you can use the same languages (i.e. TypeScript), third-party libraries, and internal logic in mocks.
+> **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
+## Node.js
- [Learn more about using MSW in Node.js](https://mswjs.io/docs/getting-started/integrate/node)
- [`setupServer` API](https://mswjs.io/docs/api/setup-server)
### How does it work?
-Although Service Worker is a browser-specific API, this library allows reusing of the same mock definition to have API mocking in Node.js through augmenting native request issuing modules.
+There's no such thing as Service Workers in Node.js. Instead, MSW implements a [low-level interception algorithm](https://github.com/mswjs/interceptors) that can utilize the very same request handlers you have for the browser. This blends the boundary between environments, allowing you to focus on your network behaviors.
### How is it different?
-- Prevents from stubbing `fetch`/`axios`/etc. as a part of your test, allowing you to treat API mocking as a pre-requisite and focus on what actually matters during testing.
-- The same mock definition you use for local development can be reused for testing.
+- Does not stub `fetch`, `axios`, etc. As a result, your tests know _nothing_ about mocking;
+- You can reuse the same request handlers for local development and debugging, as well as for testing. Truly a single source of truth for your network behavior across all environments and all tools.
### Usage example
-Here's an example of an actual integration test in Jest that uses [React Testing Library](https://github.com/testing-library/react-testing-library) and Mock Service Worker:
+Take a look at the example of an integration test in Jest that uses [React Testing Library](https://github.com/testing-library/react-testing-library) and Mock Service Worker:
```js
-// test/LoginForm.test.js
-import '@testing-library/jest-dom'
+// test/Dashboard.test.js
+
import React from 'react'
import { rest } from 'msw'
import { setupServer } from 'msw/node'
-import { render, screen } from '@testing-library/react'
-import userEvent from '@testing-library/user-event'
-import Login from '../src/components/Login'
+import { render, screen, waitFor } from '@testing-library/react'
+import Dashboard from '../src/components/Dashboard'
const server = setupServer(
- rest.post('/login', (req, res, ctx) => {
- // Respond with a mocked user token that gets persisted
- // in the `sessionStorage` by the `Login` component.
- return res(ctx.json({ token: 'mocked_user_token' }))
+ // 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 API mocking before tests.
+// Enable request interception.
beforeAll(() => server.listen())
-// Reset any runtime request handlers we may add during the tests.
+// Reset handlers so that each test could alter them
+// without affecting other, unrelated tests.
afterEach(() => server.resetHandlers())
-// Disable API mocking after the tests are done.
+// Don't forget to clean up afterwards.
afterAll(() => server.close())
-test('allows the user to log in', async () => {
- render(
From 4e40f419ee755218b1355896fbbf09aa47b0f67c Mon Sep 17 00:00:00 2001
From: Artem Zakharchenko |