msw 2.0.0 → 2.0.2

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/README.md CHANGED
@@ -1,6 +1,12 @@
1
- > [!IMPORTANT]\
2
1
  > **MSW 2.0 is finally here! 🎉** Read the [Release notes](https://github.com/mswjs/msw/releases/tag/v2.0.0) and please follow the [**Migration guidelines**](https://mswjs.io/docs/migrations/1.x-to-2.x) to upgrade. If you're having any questions while upgrading, please reach out in our [Discord server](https://kettanaito.com/discord).
2
+ >
3
+ > We've also recorded the most comprehensive introduction to MSW ever. Learn how to mock APIs like a pro in our official video course:
4
+
5
+ <a href="https://egghead.io/courses/mock-rest-and-graphql-apis-with-mock-service-worker-8d471ece?af=8mci9b" target="_blank">
6
+ <img src="media/egghead-banner.png" alt="Mock REST and GraphQL APIs with Mock Service Worker" />
7
+ </a>
3
8
 
9
+ <br />
4
10
  <br />
5
11
 
6
12
  <p align="center">
@@ -17,13 +23,15 @@
17
23
  <a href="https://www.npmjs.com/package/msw" target="_blank">
18
24
  <img src="https://img.shields.io/npm/dm/msw?style=for-the-badge&color=black" alt="Downloads per month" />
19
25
  </a>
20
- <a href="https://kcd.im/discord" target="_blank">
26
+ <a href="https://kettanaito.com/discord" target="_blank">
21
27
  <img src="https://img.shields.io/badge/chat-online-green?style=for-the-badge&color=black" alt="Discord server" />
22
28
  </a>
23
29
  </p>
24
30
 
25
31
  <br />
26
32
 
33
+ <br />
34
+
27
35
  ## Features
28
36
 
29
37
  - **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.
@@ -117,7 +125,7 @@ There's no such thing as Service Workers in Node.js. Instead, MSW implements a [
117
125
 
118
126
  ### Usage example
119
127
 
120
- 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:
128
+ Take a look at the example of an integration test in Vitest that uses [React Testing Library](https://github.com/testing-library/react-testing-library) and Mock Service Worker:
121
129
 
122
130
  ```js
123
131
  // test/Dashboard.test.js
@@ -205,6 +213,11 @@ Mock Service Worker is trusted by hundreds of thousands of engineers around the
205
213
  </picture>
206
214
  </a>
207
215
  </td>
216
+ <td>
217
+ <a href="https://www.codacy.com/" target="_blank">
218
+ <img src="media/sponsors/codacy.svg" alt="Codacy" height="64" />
219
+ </a>
220
+ </td>
208
221
  </tr>
209
222
  </table>
210
223
 
@@ -261,6 +261,7 @@ var createRequestListener = (context, options) => {
261
261
  onMockedResponse(_0, _1) {
262
262
  return __async(this, arguments, function* (response, { handler, parsedResult }) {
263
263
  const responseClone = response.clone();
264
+ const responseCloneForLogs = response.clone();
264
265
  const responseInit = (0, import_toResponseInit.toResponseInit)(response);
265
266
  if (context.supports.readableStreamTransfer) {
266
267
  const responseStream = response.body;
@@ -278,10 +279,10 @@ var createRequestListener = (context, options) => {
278
279
  }));
279
280
  }
280
281
  if (!options.quiet) {
281
- context.emitter.once("response:mocked", ({ response: response2 }) => {
282
+ context.emitter.once("response:mocked", () => {
282
283
  handler.log({
283
284
  request: requestCloneForLogs,
284
- response: response2,
285
+ response: responseCloneForLogs,
285
286
  parsedResult
286
287
  });
287
288
  });
@@ -237,6 +237,7 @@ var createRequestListener = (context, options) => {
237
237
  onMockedResponse(_0, _1) {
238
238
  return __async(this, arguments, function* (response, { handler, parsedResult }) {
239
239
  const responseClone = response.clone();
240
+ const responseCloneForLogs = response.clone();
240
241
  const responseInit = toResponseInit(response);
241
242
  if (context.supports.readableStreamTransfer) {
242
243
  const responseStream = response.body;
@@ -254,10 +255,10 @@ var createRequestListener = (context, options) => {
254
255
  }));
255
256
  }
256
257
  if (!options.quiet) {
257
- context.emitter.once("response:mocked", ({ response: response2 }) => {
258
+ context.emitter.once("response:mocked", () => {
258
259
  handler.log({
259
260
  request: requestCloneForLogs,
260
- response: response2,
261
+ response: responseCloneForLogs,
261
262
  parsedResult
262
263
  });
263
264
  });
@@ -129,11 +129,10 @@ Consider naming this operation or using "graphql.operation()" request handler to
129
129
  const statusColor = (0, import_getStatusCodeColor.getStatusCodeColor)(loggedResponse.status);
130
130
  const requestInfo = ((_a = args.parsedResult) == null ? void 0 : _a.operationName) ? `${(_b = args.parsedResult) == null ? void 0 : _b.operationType} ${(_c = args.parsedResult) == null ? void 0 : _c.operationName}` : `anonymous ${(_d = args.parsedResult) == null ? void 0 : _d.operationType}`;
131
131
  console.groupCollapsed(
132
- import_devUtils.devUtils.formatMessage("%s %s (%c%s%c)"),
133
- (0, import_getTimestamp.getTimestamp)(),
134
- `${requestInfo}`,
132
+ import_devUtils.devUtils.formatMessage(
133
+ `${(0, import_getTimestamp.getTimestamp)()} ${requestInfo} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
134
+ ),
135
135
  `color:${statusColor}`,
136
- `${loggedResponse.status} ${loggedResponse.statusText}`,
137
136
  "color:inherit"
138
137
  );
139
138
  console.log("Request:", loggedRequest);
@@ -110,11 +110,10 @@ Consider naming this operation or using "graphql.operation()" request handler to
110
110
  const statusColor = getStatusCodeColor(loggedResponse.status);
111
111
  const requestInfo = ((_a = args.parsedResult) == null ? void 0 : _a.operationName) ? `${(_b = args.parsedResult) == null ? void 0 : _b.operationType} ${(_c = args.parsedResult) == null ? void 0 : _c.operationName}` : `anonymous ${(_d = args.parsedResult) == null ? void 0 : _d.operationType}`;
112
112
  console.groupCollapsed(
113
- devUtils.formatMessage("%s %s (%c%s%c)"),
114
- getTimestamp(),
115
- `${requestInfo}`,
113
+ devUtils.formatMessage(
114
+ `${getTimestamp()} ${requestInfo} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
115
+ ),
116
116
  `color:${statusColor}`,
117
- `${loggedResponse.status} ${loggedResponse.statusText}`,
118
117
  "color:inherit"
119
118
  );
120
119
  console.log("Request:", loggedRequest);
@@ -132,12 +132,10 @@ class HttpHandler extends import_RequestHandler.RequestHandler {
132
132
  const loggedResponse = yield (0, import_serializeResponse.serializeResponse)(args.response);
133
133
  const statusColor = (0, import_getStatusCodeColor.getStatusCodeColor)(loggedResponse.status);
134
134
  console.groupCollapsed(
135
- import_devUtils.devUtils.formatMessage("%s %s %s (%c%s%c)"),
136
- (0, import_getTimestamp.getTimestamp)(),
137
- args.request.method,
138
- publicUrl,
135
+ import_devUtils.devUtils.formatMessage(
136
+ `${(0, import_getTimestamp.getTimestamp)()} ${args.request.method} ${publicUrl} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
137
+ ),
139
138
  `color:${statusColor}`,
140
- `${loggedResponse.status} ${loggedResponse.statusText}`,
141
139
  "color:inherit"
142
140
  );
143
141
  console.log("Request", loggedRequest);
@@ -112,12 +112,10 @@ class HttpHandler extends RequestHandler {
112
112
  const loggedResponse = yield serializeResponse(args.response);
113
113
  const statusColor = getStatusCodeColor(loggedResponse.status);
114
114
  console.groupCollapsed(
115
- devUtils.formatMessage("%s %s %s (%c%s%c)"),
116
- getTimestamp(),
117
- args.request.method,
118
- publicUrl,
115
+ devUtils.formatMessage(
116
+ `${getTimestamp()} ${args.request.method} ${publicUrl} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
117
+ ),
119
118
  `color:${statusColor}`,
120
- `${loggedResponse.status} ${loggedResponse.statusText}`,
121
119
  "color:inherit"
122
120
  );
123
121
  console.log("Request", loggedRequest);
@@ -105,12 +105,11 @@ class RequestHandler {
105
105
  */
106
106
  run(args) {
107
107
  return __async(this, null, function* () {
108
- var _a;
108
+ var _a, _b;
109
109
  if (this.isUsed && ((_a = this.options) == null ? void 0 : _a.once)) {
110
110
  return null;
111
111
  }
112
112
  const mainRequestRef = args.request.clone();
113
- this.isUsed = true;
114
113
  const parsedResult = yield this.parse({
115
114
  request: args.request,
116
115
  resolutionContext: args.resolutionContext
@@ -123,6 +122,10 @@ class RequestHandler {
123
122
  if (!shouldInterceptRequest) {
124
123
  return null;
125
124
  }
125
+ if (this.isUsed && ((_b = this.options) == null ? void 0 : _b.once)) {
126
+ return null;
127
+ }
128
+ this.isUsed = true;
126
129
  const executeResolver = this.wrapResolver(this.resolver);
127
130
  const resolverExtras = this.extendResolverArgs({
128
131
  request: args.request,
@@ -84,12 +84,11 @@ class RequestHandler {
84
84
  */
85
85
  run(args) {
86
86
  return __async(this, null, function* () {
87
- var _a;
87
+ var _a, _b;
88
88
  if (this.isUsed && ((_a = this.options) == null ? void 0 : _a.once)) {
89
89
  return null;
90
90
  }
91
91
  const mainRequestRef = args.request.clone();
92
- this.isUsed = true;
93
92
  const parsedResult = yield this.parse({
94
93
  request: args.request,
95
94
  resolutionContext: args.resolutionContext
@@ -102,6 +101,10 @@ class RequestHandler {
102
101
  if (!shouldInterceptRequest) {
103
102
  return null;
104
103
  }
104
+ if (this.isUsed && ((_b = this.options) == null ? void 0 : _b.once)) {
105
+ return null;
106
+ }
107
+ this.isUsed = true;
105
108
  const executeResolver = this.wrapResolver(this.resolver);
106
109
  const resolverExtras = this.extendResolverArgs({
107
110
  request: args.request,
package/lib/iife/index.js CHANGED
@@ -547,12 +547,11 @@ var MockServiceWorker = (() => {
547
547
  */
548
548
  run(args) {
549
549
  return __async(this, null, function* () {
550
- var _a3;
550
+ var _a3, _b2;
551
551
  if (this.isUsed && ((_a3 = this.options) == null ? void 0 : _a3.once)) {
552
552
  return null;
553
553
  }
554
554
  const mainRequestRef = args.request.clone();
555
- this.isUsed = true;
556
555
  const parsedResult = yield this.parse({
557
556
  request: args.request,
558
557
  resolutionContext: args.resolutionContext
@@ -565,6 +564,10 @@ var MockServiceWorker = (() => {
565
564
  if (!shouldInterceptRequest) {
566
565
  return null;
567
566
  }
567
+ if (this.isUsed && ((_b2 = this.options) == null ? void 0 : _b2.once)) {
568
+ return null;
569
+ }
570
+ this.isUsed = true;
568
571
  const executeResolver = this.wrapResolver(this.resolver);
569
572
  const resolverExtras = this.extendResolverArgs({
570
573
  request: args.request,
@@ -2380,12 +2383,10 @@ Invalid value has been removed from localStorage to prevent subsequent failed pa
2380
2383
  const loggedResponse = yield serializeResponse(args.response);
2381
2384
  const statusColor = getStatusCodeColor(loggedResponse.status);
2382
2385
  console.groupCollapsed(
2383
- devUtils.formatMessage("%s %s %s (%c%s%c)"),
2384
- getTimestamp(),
2385
- args.request.method,
2386
- publicUrl,
2386
+ devUtils.formatMessage(
2387
+ `${getTimestamp()} ${args.request.method} ${publicUrl} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
2388
+ ),
2387
2389
  `color:${statusColor}`,
2388
- `${loggedResponse.status} ${loggedResponse.statusText}`,
2389
2390
  "color:inherit"
2390
2391
  );
2391
2392
  console.log("Request", loggedRequest);
@@ -5566,11 +5567,10 @@ Consider naming this operation or using "graphql.operation()" request handler to
5566
5567
  const statusColor = getStatusCodeColor(loggedResponse.status);
5567
5568
  const requestInfo = ((_a3 = args.parsedResult) == null ? void 0 : _a3.operationName) ? `${(_b2 = args.parsedResult) == null ? void 0 : _b2.operationType} ${(_c = args.parsedResult) == null ? void 0 : _c.operationName}` : `anonymous ${(_d = args.parsedResult) == null ? void 0 : _d.operationType}`;
5568
5569
  console.groupCollapsed(
5569
- devUtils.formatMessage("%s %s (%c%s%c)"),
5570
- getTimestamp(),
5571
- `${requestInfo}`,
5570
+ devUtils.formatMessage(
5571
+ `${getTimestamp()} ${requestInfo} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
5572
+ ),
5572
5573
  `color:${statusColor}`,
5573
- `${loggedResponse.status} ${loggedResponse.statusText}`,
5574
5574
  "color:inherit"
5575
5575
  );
5576
5576
  console.log("Request:", loggedRequest);
@@ -6366,6 +6366,7 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
6366
6366
  onMockedResponse(_0, _1) {
6367
6367
  return __async(this, arguments, function* (response, { handler, parsedResult }) {
6368
6368
  const responseClone = response.clone();
6369
+ const responseCloneForLogs = response.clone();
6369
6370
  const responseInit = toResponseInit(response);
6370
6371
  if (context.supports.readableStreamTransfer) {
6371
6372
  const responseStream = response.body;
@@ -6383,10 +6384,10 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
6383
6384
  }));
6384
6385
  }
6385
6386
  if (!options.quiet) {
6386
- context.emitter.once("response:mocked", ({ response: response2 }) => {
6387
+ context.emitter.once("response:mocked", () => {
6387
6388
  handler.log({
6388
6389
  request: requestCloneForLogs,
6389
- response: response2,
6390
+ response: responseCloneForLogs,
6390
6391
  parsedResult
6391
6392
  });
6392
6393
  });
@@ -2,7 +2,7 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (2.0.0).
5
+ * Mock Service Worker (2.0.2).
6
6
  * @see https://github.com/mswjs/msw
7
7
  * - Please do NOT modify this file.
8
8
  * - Please do NOT serve this file on production.
@@ -54,9 +54,9 @@ var import_mergeRight = require("../core/utils/internal/mergeRight.js");
54
54
  var import_handleRequest = require("../core/utils/handleRequest.js");
55
55
  var import_devUtils = require("../core/utils/internal/devUtils.js");
56
56
 
57
- // src/node/utils/isNodeException.ts
58
- function isNodeException(error) {
59
- return error instanceof Error && "code" in error;
57
+ // src/node/utils/isNodeExceptionLike.ts
58
+ function isNodeExceptionLike(error) {
59
+ return !!error && typeof error === "object" && "code" in error;
60
60
  }
61
61
 
62
62
  // src/node/SetupServerApi.ts
@@ -85,7 +85,7 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
85
85
  request.signal
86
86
  );
87
87
  } catch (error) {
88
- if (!(isNodeException(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
88
+ if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
89
89
  throw error;
90
90
  }
91
91
  }
@@ -34,9 +34,9 @@ import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
34
34
  import { handleRequest } from '../core/utils/handleRequest.mjs';
35
35
  import { devUtils } from '../core/utils/internal/devUtils.mjs';
36
36
 
37
- // src/node/utils/isNodeException.ts
38
- function isNodeException(error) {
39
- return error instanceof Error && "code" in error;
37
+ // src/node/utils/isNodeExceptionLike.ts
38
+ function isNodeExceptionLike(error) {
39
+ return !!error && typeof error === "object" && "code" in error;
40
40
  }
41
41
 
42
42
  // src/node/SetupServerApi.ts
@@ -65,7 +65,7 @@ var SetupServerApi = class extends SetupApi {
65
65
  request.signal
66
66
  );
67
67
  } catch (error) {
68
- if (!(isNodeException(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
68
+ if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
69
69
  throw error;
70
70
  }
71
71
  }
package/lib/node/index.js CHANGED
@@ -54,9 +54,9 @@ var import_mergeRight = require("../core/utils/internal/mergeRight.js");
54
54
  var import_handleRequest = require("../core/utils/handleRequest.js");
55
55
  var import_devUtils = require("../core/utils/internal/devUtils.js");
56
56
 
57
- // src/node/utils/isNodeException.ts
58
- function isNodeException(error) {
59
- return error instanceof Error && "code" in error;
57
+ // src/node/utils/isNodeExceptionLike.ts
58
+ function isNodeExceptionLike(error) {
59
+ return !!error && typeof error === "object" && "code" in error;
60
60
  }
61
61
 
62
62
  // src/node/SetupServerApi.ts
@@ -85,7 +85,7 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
85
85
  request.signal
86
86
  );
87
87
  } catch (error) {
88
- if (!(isNodeException(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
88
+ if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
89
89
  throw error;
90
90
  }
91
91
  }
@@ -31,9 +31,9 @@ import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
31
31
  import { handleRequest } from '../core/utils/handleRequest.mjs';
32
32
  import { devUtils } from '../core/utils/internal/devUtils.mjs';
33
33
 
34
- // src/node/utils/isNodeException.ts
35
- function isNodeException(error) {
36
- return error instanceof Error && "code" in error;
34
+ // src/node/utils/isNodeExceptionLike.ts
35
+ function isNodeExceptionLike(error) {
36
+ return !!error && typeof error === "object" && "code" in error;
37
37
  }
38
38
 
39
39
  // src/node/SetupServerApi.ts
@@ -62,7 +62,7 @@ var SetupServerApi = class extends SetupApi {
62
62
  request.signal
63
63
  );
64
64
  } catch (error) {
65
- if (!(isNodeException(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
65
+ if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
66
66
  throw error;
67
67
  }
68
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
5
5
  "main": "./lib/core/index.js",
6
6
  "module": "./lib/core/index.mjs",
@@ -123,11 +123,9 @@
123
123
  "@ossjs/release": "^0.8.0",
124
124
  "@playwright/test": "^1.30.0",
125
125
  "@swc/core": "^1.3.35",
126
- "@swc/jest": "^0.2.24",
127
126
  "@types/express": "^4.17.17",
128
127
  "@types/fs-extra": "^9.0.13",
129
128
  "@types/glob": "^8.1.0",
130
- "@types/jest": "^29.4.0",
131
129
  "@types/json-bigint": "^1.0.1",
132
130
  "@types/node": "18.x",
133
131
  "@types/node-fetch": "^2.5.11",
@@ -150,8 +148,7 @@
150
148
  "fs-extra": "^10.0.0",
151
149
  "fs-teardown": "^0.3.0",
152
150
  "glob": "^9.3.4",
153
- "jest": "^29.4.3",
154
- "jest-environment-jsdom": "^29.4.3",
151
+ "jsdom": "^22.1.0",
155
152
  "json-bigint": "^1.0.0",
156
153
  "lint-staged": "^13.0.3",
157
154
  "page-with": "^0.6.1",
@@ -164,6 +161,7 @@
164
161
  "typescript": "^5.0.2",
165
162
  "undici": "^5.20.0",
166
163
  "url-loader": "^4.1.1",
164
+ "vitest": "^0.34.6",
167
165
  "webpack": "^5.68.0",
168
166
  "webpack-dev-server": "^3.11.2",
169
167
  "webpack-http-server": "^0.5.0"
@@ -193,15 +191,14 @@
193
191
  "start": "tsup --watch",
194
192
  "clean": "rimraf ./lib",
195
193
  "lint": "eslint \"{cli,config,src,test}/**/*.ts\"",
196
- "prebuild": "rimraf ./lib",
197
194
  "build": "pnpm clean && cross-env NODE_ENV=production tsup && pnpm patch:dts",
198
195
  "patch:dts": "node \"./config/scripts/patch-ts.js\"",
199
196
  "check:exports": "node \"./config/scripts/validate-esm.js\"",
200
197
  "test": "pnpm test:unit && pnpm test:node && pnpm test:browser",
201
- "test:unit": "cross-env BABEL_ENV=test jest --maxWorkers=3",
202
- "test:node": "jest --config=./test/jest.config.js --forceExit",
198
+ "test:unit": "vitest",
199
+ "test:node": "vitest run --config=./test/node/vitest.config.ts",
203
200
  "test:browser": "playwright test -c ./test/browser/playwright.config.ts",
204
- "test:modules:node": "jest --config=./test/modules/node/jest.config.js",
201
+ "test:modules:node": "vitest run --config=./test/modules/node/vitest.config.ts",
205
202
  "test:modules:browser": "playwright test -c ./test/modules/browser/playwright.config.ts",
206
203
  "test:ts": "ts-node test/typings/run.ts",
207
204
  "release": "release publish",
@@ -1,2 +0,0 @@
1
-
2
- export { }
@@ -1,15 +0,0 @@
1
- "use strict";
2
- var import_http = require("./http.js");
3
- test("exports all REST API methods", () => {
4
- expect(import_http.http).toBeDefined();
5
- expect(Object.keys(import_http.http)).toEqual([
6
- "all",
7
- "head",
8
- "get",
9
- "post",
10
- "put",
11
- "delete",
12
- "patch",
13
- "options"
14
- ]);
15
- });
@@ -1,14 +0,0 @@
1
- import { http } from './http.mjs';
2
- test("exports all REST API methods", () => {
3
- expect(http).toBeDefined();
4
- expect(Object.keys(http)).toEqual([
5
- "all",
6
- "head",
7
- "get",
8
- "post",
9
- "put",
10
- "delete",
11
- "patch",
12
- "options"
13
- ]);
14
- });