msw 0.0.0-fetch.rc-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018–present Artem Zakharchenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,243 @@
1
+ <br />
2
+
3
+ <p align="center">
4
+ <img src="media/msw-logo.svg" width="100" alt="Mock Service Worker logo" />
5
+ </p>
6
+
7
+ <h1 align="center">Mock Service Worker</h1>
8
+ <p align="center">Mock Service Worker (MSW) is a seamless REST/GraphQL API mocking library for browser and Node.js.</p>
9
+
10
+ <p align="center">
11
+ <a href="https://www.npmjs.com/package/msw" target="_blank">
12
+ <img src="https://img.shields.io/npm/v/msw.svg?style=for-the-badge&label=Latest&color=black" alt="Package version" />
13
+ </a>
14
+ <a href="https://www.npmjs.com/package/msw" target="_blank">
15
+ <img src="https://img.shields.io/npm/dm/msw?style=for-the-badge&color=black" alt="Downloads per month" />
16
+ </a>
17
+ <a href="https://kcd.im/discord" target="_blank">
18
+ <img src="https://img.shields.io/badge/chat-online-green?style=for-the-badge&color=black" alt="Discord server" />
19
+ </a>
20
+ </p>
21
+
22
+ <br />
23
+
24
+ ## Features
25
+
26
+ - **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.
27
+ - **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.
28
+ - **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.
29
+
30
+ ---
31
+
32
+ > "_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._"
33
+ >
34
+ > – [Kent C. Dodds](https://twitter.com/kentcdodds)
35
+
36
+ ## Documentation
37
+
38
+ 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.
39
+
40
+ - [Documentation](https://mswjs.io/docs)
41
+ - [**Getting started**](https://mswjs.io/docs/getting-started/install)
42
+ - [Recipes](https://mswjs.io/docs/recipes)
43
+ - [FAQ](https://mswjs.io/docs/faq)
44
+
45
+ ## Examples
46
+
47
+ - See the list of [**Usage examples**](https://github.com/mswjs/examples)
48
+
49
+ ## Browser
50
+
51
+ - [Learn more about using MSW in a browser](https://mswjs.io/docs/getting-started/integrate/browser)
52
+ - [`setupWorker` API](https://mswjs.io/docs/api/setup-worker)
53
+
54
+ ### How does it work?
55
+
56
+ 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.
57
+
58
+ **Take a look at this quick presentation on how Mock Service Worker functions in a browser:**
59
+
60
+ [![What is Mock Service Worker?](https://raw.githubusercontent.com/mswjs/msw/main/media/msw-video-thumbnail.jpg)](https://youtu.be/HcQCqboatZk)
61
+
62
+ ### How is it different?
63
+
64
+ - 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;
65
+ - 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;
66
+ - No more stubbing of `fetch`, `axios`, `react-query`, you-name-it;
67
+ - 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.
68
+
69
+ ### Usage example
70
+
71
+ ```js
72
+ // src/mocks.js
73
+ // 1. Import the library.
74
+ import { setupWorker, rest, HttpResponse } from 'msw'
75
+
76
+ // 2. Describe network behavior with request handlers.
77
+ const worker = setupWorker(
78
+ rest.get('https://github.com/octocat', ({ request, params, cookies }) => {
79
+ return HttpResponse.json(
80
+ {
81
+ message: 'Mocked response',
82
+ },
83
+ {
84
+ status: 202,
85
+ statusText: 'Mocked status',
86
+ },
87
+ )
88
+ }),
89
+ )
90
+
91
+ // 3. Start request interception by starting the Service Worker.
92
+ worker.start()
93
+ ```
94
+
95
+ Performing a `GET https://github.com/octocat` request in your application will result into a mocked response that you can inspect in your browser's "Network" tab:
96
+
97
+ ![Chrome DevTools Network screenshot with the request mocked](https://github.com/mswjs/msw/blob/main/media/msw-quick-look-network.png?raw=true)
98
+
99
+ > **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.
100
+
101
+ ## Node.js
102
+
103
+ - [Learn more about using MSW in Node.js](https://mswjs.io/docs/getting-started/integrate/node)
104
+ - [`setupServer` API](https://mswjs.io/docs/api/setup-server)
105
+
106
+ ### How does it work?
107
+
108
+ 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.
109
+
110
+ ### How is it different?
111
+
112
+ - Does not stub `fetch`, `axios`, etc. As a result, your tests know _nothing_ about mocking;
113
+ - 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.
114
+
115
+ ### Usage example
116
+
117
+ 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:
118
+
119
+ ```js
120
+ // test/Dashboard.test.js
121
+
122
+ import React from 'react'
123
+ import { rest, HttpResponse } from 'msw'
124
+ import { setupServer } from 'msw/node'
125
+ import { render, screen, waitFor } from '@testing-library/react'
126
+ import Dashboard from '../src/components/Dashboard'
127
+
128
+ const server = setupServer(
129
+ // Describe network behavior with request handlers.
130
+ // Tip: move the handlers into their own module and
131
+ // import it across your browser and Node.js setups!
132
+ rest.get('/posts', ({ request, params, cookies }) => {
133
+ return HttpResponse.json([
134
+ {
135
+ id: 'f8dd058f-9006-4174-8d49-e3086bc39c21',
136
+ title: `Avoid Nesting When You're Testing`,
137
+ },
138
+ {
139
+ id: '8ac96078-6434-4959-80ed-cc834e7fef61',
140
+ title: `How I Built A Modern Website In 2021`,
141
+ },
142
+ ])
143
+ }),
144
+ )
145
+
146
+ // Enable request interception.
147
+ beforeAll(() => server.listen())
148
+
149
+ // Reset handlers so that each test could alter them
150
+ // without affecting other, unrelated tests.
151
+ afterEach(() => server.resetHandlers())
152
+
153
+ // Don't forget to clean up afterwards.
154
+ afterAll(() => server.close())
155
+
156
+ it('displays the list of recent posts', async () => {
157
+ render(<Dashboard />)
158
+
159
+ // 🕗 Wait for the posts request to be finished.
160
+ await waitFor(() => {
161
+ expect(
162
+ screen.getByLabelText('Fetching latest posts...'),
163
+ ).not.toBeInTheDocument()
164
+ })
165
+
166
+ // ✅ Assert that the correct posts have loaded.
167
+ expect(
168
+ screen.getByRole('link', { name: /Avoid Nesting When You're Testing/ }),
169
+ ).toBeVisible()
170
+
171
+ expect(
172
+ screen.getByRole('link', { name: /How I Built A Modern Website In 2021/ }),
173
+ ).toBeVisible()
174
+ })
175
+ ```
176
+
177
+ > 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.
178
+
179
+ Despite the API being called `setupServer`, there are no actual servers involved! The name was chosen for familiarity, and the API was designed to resemble operating with an actual server.
180
+
181
+ ## Sponsors
182
+
183
+ Mock Service Worker is trusted by hundreds of thousands of engineers around the globe. It's used by companies like Google, Microsoft, Spotify, Amazon, and countless others. Despite that, this library remains a hobby project maintained in spare time and has no opportunity to financially support even a single full-time contributor.
184
+
185
+ **You can change that!** Consider [sponsoring the effort](https://github.com/sponsors/mswjs) behind one of the most innovative approaches around API mocking. Raise a topic of open source sponsorships with your boss and colleagues. Let's build sustainable open source together!
186
+
187
+ ### Golden Sponsors
188
+
189
+ > Become our first golden sponsor and get featured right here, enjoying other perks like issue prioritization and a personal consulting session with us.
190
+ >
191
+ > **Learn more on our [GitHub Sponsors profile](https://github.com/sponsors/mswjs)**.
192
+
193
+ <br />
194
+
195
+ <a href="https://www.github.com/" target="_blank">
196
+ <picture>
197
+ <source media="(prefers-color-scheme: dark)" srcset="media/sponsors/github-light.svg" />
198
+ <img src="media/sponsors/github.svg" alt="GitHub" width="75" />
199
+ </picture>
200
+ </a>
201
+
202
+ ### Silver Sponsors
203
+
204
+ > Become our _silver sponsor_ and get your profile image and link featured right here.
205
+ >
206
+ > **Learn more on our [GitHub Sponsors profile](https://github.com/sponsors/mswjs)**.
207
+
208
+ <br />
209
+
210
+ <a href="https://www.chromatic.com/" target="_blank">
211
+ <img src="media/sponsors/chromatic.svg" alt="Chromatic" width="75" />
212
+ </a>
213
+
214
+ ### Bronze Sponsors
215
+
216
+ > Become our first _bronze sponsor_ and get your profile image and link featured in this section.
217
+ >
218
+ > **Learn more on our [GitHub Sponsors profile](https://github.com/sponsors/mswjs)**.
219
+
220
+ ## Awards & Mentions
221
+
222
+ We've been extremely humbled to receive awards and mentions from the community for all the innovation and reach Mock Service Worker brings to the JavaScript ecosystem.
223
+
224
+ <table>
225
+ <tr valign="middle">
226
+ <td width="124">
227
+ <img src="https://raw.githubusercontent.com/mswjs/msw/main/media/tech-radar.png" width="124" alt="Technology Radar">
228
+ </td>
229
+ <td>
230
+ <h4>Solution Worth Pursuing</h4>
231
+ <p><em><a href="https://www.thoughtworks.com/radar/languages-and-frameworks/mock-service-worker">Technology Radar</a> (2020–2021)</em></p>
232
+ </td>
233
+ </tr>
234
+ <tr>
235
+ <td width="124">
236
+ <img src="https://raw.githubusercontent.com/mswjs/msw/main/media/os-awards.png" width="124" alt="Open Source Awards 2020">
237
+ </td>
238
+ <td>
239
+ <h4>The Most Exciting Use of Technology</h4>
240
+ <p><em><a href="https://osawards.com/javascript/2020">Open Source Awards</a> (2020)</em></p>
241
+ </td>
242
+ </tr>
243
+ </table>
package/cli/index.js ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env node
2
+ const yargs = require('yargs')
3
+
4
+ yargs
5
+ .usage('$0 <cmd> [args]')
6
+ .command(
7
+ 'init <publicDir>',
8
+ 'Initializes Mock Service Worker at the specified directory',
9
+ (yargs) => {
10
+ yargs
11
+ .positional('publicDir', {
12
+ type: 'string',
13
+ description: 'Relative path to the public directory',
14
+ required: true,
15
+ normalize: true,
16
+ })
17
+ .example('init', 'msw init public')
18
+ .option('save', {
19
+ type: 'boolean',
20
+ description: 'Save the worker directory in your package.json',
21
+ })
22
+ },
23
+ require('./init'),
24
+ )
25
+ .demandCommand()
26
+ .help().argv
package/cli/init.js ADDED
@@ -0,0 +1,144 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+ const chalk = require('chalk')
4
+ const { until } = require('@open-draft/until')
5
+ const inquirer = require('inquirer')
6
+ const invariant = require('./invariant')
7
+ const { SERVICE_WORKER_BUILD_PATH } = require('../config/constants')
8
+
9
+ const CWD = process.cwd()
10
+
11
+ module.exports = async function init(args) {
12
+ const { publicDir, save } = args
13
+
14
+ // When running as a part of "postinstall" script, "cwd" equals the library's directory.
15
+ // The "postinstall" script resolves the right absolute public directory path.
16
+ const absolutePublicDir = path.isAbsolute(publicDir)
17
+ ? publicDir
18
+ : path.resolve(CWD, publicDir)
19
+ const relativePublicDir = path.relative(CWD, absolutePublicDir)
20
+ const dirExists = fs.existsSync(absolutePublicDir)
21
+
22
+ if (!dirExists) {
23
+ // Try to create the directory if it doesn't exist
24
+ const [createDirectoryError] = await until(() =>
25
+ fs.promises.mkdir(absolutePublicDir, { recursive: true }),
26
+ )
27
+ invariant(
28
+ createDirectoryError == null,
29
+ 'Failed to create a Service Worker at "%s": directory does not exist and could not be created.\nMake sure to include a relative path to the root directory of your server.\n\nSee the original error below:\n%s',
30
+ absolutePublicDir,
31
+ createDirectoryError,
32
+ )
33
+ }
34
+
35
+ console.log(
36
+ 'Initializing the Mock Service Worker at "%s"...',
37
+ absolutePublicDir,
38
+ )
39
+
40
+ const serviceWorkerFilename = path.basename(SERVICE_WORKER_BUILD_PATH)
41
+ const swDestFilepath = path.resolve(absolutePublicDir, serviceWorkerFilename)
42
+
43
+ fs.copyFileSync(SERVICE_WORKER_BUILD_PATH, swDestFilepath)
44
+
45
+ console.log(`
46
+ ${chalk.green('Service Worker successfully created!')}
47
+ ${chalk.gray(swDestFilepath)}
48
+
49
+ Continue by creating a mocking definition module in your application:
50
+
51
+ ${chalk.cyan.bold('https://mswjs.io/docs/getting-started/mocks')}
52
+ `)
53
+
54
+ const packageJsonPath = path.resolve(CWD, 'package.json')
55
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
56
+ const { msw: mswConfig } = packageJson
57
+
58
+ if (mswConfig && mswConfig.workerDirectory === relativePublicDir) {
59
+ return
60
+ }
61
+
62
+ // When called `msw init --no-save` do not show the save suggestions.
63
+ if (save === false) {
64
+ return
65
+ }
66
+
67
+ if (!mswConfig) {
68
+ console.log(`\
69
+ ${chalk.cyan('INFO')} In order to ease the future updates to the worker script,
70
+ we recommend saving the path to the worker directory in your package.json.
71
+ `)
72
+
73
+ if (save) {
74
+ return saveWorkerDirectory(packageJsonPath, relativePublicDir)
75
+ }
76
+
77
+ return promptWorkerDirectoryUpdate(
78
+ `Do you wish to save "${relativePublicDir}" as the worker directory?`,
79
+ packageJsonPath,
80
+ relativePublicDir,
81
+ )
82
+ }
83
+
84
+ if (mswConfig.workerDirectory !== relativePublicDir) {
85
+ console.log(
86
+ `\
87
+ ${chalk.yellowBright(
88
+ 'WARN',
89
+ )} The "msw.workerDirectory" in your package.json ("%s")
90
+ is different from the worker directory used right now ("%s").
91
+ `,
92
+ mswConfig.workerDirectory,
93
+ relativePublicDir,
94
+ )
95
+
96
+ if (save) {
97
+ return saveWorkerDirectory(packageJsonPath, relativePublicDir)
98
+ }
99
+
100
+ return promptWorkerDirectoryUpdate(
101
+ `Do you wish to use "${relativePublicDir}" instead of "${mswConfig.workerDirectory}" as the worker directory?`,
102
+ packageJsonPath,
103
+ relativePublicDir,
104
+ )
105
+ }
106
+ }
107
+
108
+ function saveWorkerDirectory(packageJsonPath, publicDir) {
109
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
110
+
111
+ console.log(
112
+ chalk.gray('Writing "msw.workerDirectory" to "%s"...'),
113
+ packageJsonPath,
114
+ )
115
+
116
+ const nextPackageJson = Object.assign({}, packageJson, {
117
+ msw: {
118
+ workerDirectory: publicDir,
119
+ },
120
+ })
121
+
122
+ fs.writeFileSync(
123
+ packageJsonPath,
124
+ JSON.stringify(nextPackageJson, null, 2),
125
+ 'utf8',
126
+ )
127
+ }
128
+
129
+ function promptWorkerDirectoryUpdate(message, packageJsonPath, publicDir) {
130
+ return inquirer
131
+ .prompt({
132
+ type: 'confirm',
133
+ name: 'saveWorkerDirectory',
134
+ prefix: chalk.yellowBright('?'),
135
+ message,
136
+ })
137
+ .then((answers) => {
138
+ if (!answers.saveWorkerDirectory) {
139
+ return
140
+ }
141
+
142
+ saveWorkerDirectory(packageJsonPath, publicDir)
143
+ })
144
+ }
@@ -0,0 +1,8 @@
1
+ const chalk = require('chalk')
2
+
3
+ module.exports = function (predicate, message, ...args) {
4
+ if (!predicate) {
5
+ console.error(chalk.red(message), ...args)
6
+ process.exit(1)
7
+ }
8
+ }
@@ -0,0 +1,18 @@
1
+ const path = require('path')
2
+
3
+ const SERVICE_WORKER_SOURCE_PATH = path.resolve(
4
+ __dirname,
5
+ '../',
6
+ 'src/mockServiceWorker.js',
7
+ )
8
+
9
+ const SERVICE_WORKER_BUILD_PATH = path.resolve(
10
+ __dirname,
11
+ '../lib',
12
+ path.basename(SERVICE_WORKER_SOURCE_PATH),
13
+ )
14
+
15
+ module.exports = {
16
+ SERVICE_WORKER_SOURCE_PATH,
17
+ SERVICE_WORKER_BUILD_PATH,
18
+ }
@@ -0,0 +1,48 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+ const { execSync } = require('child_process')
4
+
5
+ // When executing the "postinstall" script, the "process.cwd" equals
6
+ // the package directory, not the parent project where the package is installed.
7
+ // NPM stores the parent project directory in the "INIT_CWD" env variable.
8
+ const parentPackageCwd = process.env.INIT_CWD
9
+
10
+ function postinstall() {
11
+ // 1. Check if "package.json" has "msw.workerDirectory" property set.
12
+ const packageJson = JSON.parse(
13
+ fs.readFileSync(path.resolve(parentPackageCwd, 'package.json'), 'utf8'),
14
+ )
15
+
16
+ if (!packageJson.msw || !packageJson.msw.workerDirectory) {
17
+ return
18
+ }
19
+
20
+ // 2. Check if the worker directory is an existing path.
21
+ const { workerDirectory } = packageJson.msw
22
+ const absoluteWorkerDirectory = path.resolve(
23
+ parentPackageCwd,
24
+ workerDirectory,
25
+ )
26
+
27
+ if (!fs.existsSync(absoluteWorkerDirectory)) {
28
+ return console.error(
29
+ `[MSW] Failed to automatically update the worker script at "%s": given path does not exist.`,
30
+ workerDirectory,
31
+ )
32
+ }
33
+
34
+ // 3. Update the worker script.
35
+ const cliExecutable = path.resolve(process.cwd(), 'cli/index.js')
36
+
37
+ try {
38
+ execSync(`node ${cliExecutable} init ${absoluteWorkerDirectory}`, {
39
+ cwd: parentPackageCwd,
40
+ })
41
+ } catch (error) {
42
+ console.error(
43
+ `[MSW] Failed to automatically update the worker script:\n${error}`,
44
+ )
45
+ }
46
+ }
47
+
48
+ postinstall()
@@ -0,0 +1,19 @@
1
+ import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
2
+ import { c as SetupApi, i as ServerLifecycleEventsMap, R as RequestHandler, S as SharedOptions } from './glossary-f5428b2e.js';
3
+
4
+ declare class SetupServerApi extends SetupApi<ServerLifecycleEventsMap> {
5
+ protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
6
+ private resolvedOptions;
7
+ constructor(interceptors: Array<{
8
+ new (): Interceptor<HttpRequestEventMap>;
9
+ }>, ...handlers: Array<RequestHandler>);
10
+ /**
11
+ * Subscribe to all requests that are using the interceptor object
12
+ */
13
+ private init;
14
+ listen(options?: Partial<SharedOptions>): void;
15
+ printHandlers(): void;
16
+ close(): void;
17
+ }
18
+
19
+ export { SetupServerApi as S };