msw 0.47.0 → 0.47.1
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 +70 -76
- package/lib/{glossary-c690f512.d.ts → glossary-3d04462e.d.ts} +1 -1
- package/lib/iife/index.js +75 -23
- package/lib/iife/index.js.map +1 -1
- package/lib/index.d.ts +4 -9
- package/lib/index.js +69 -17
- package/lib/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.ts +1 -1
- package/lib/native/index.js +3 -7
- package/lib/native/index.mjs +3 -7
- package/lib/node/index.d.ts +2 -2
- package/lib/node/index.js +3 -7
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +3 -7
- package/lib/node/index.mjs.map +1 -1
- package/package.json +14 -6
package/README.md
CHANGED
|
@@ -5,15 +5,12 @@
|
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
<h1 align="center">Mock Service Worker</h1>
|
|
8
|
-
<p align="center">Mock Service Worker (MSW) is
|
|
8
|
+
<p align="center">Mock Service Worker (MSW) is a seamless REST/GraphQL API mocking library for browser and Node.js.</p>
|
|
9
9
|
|
|
10
10
|
<p align="center">
|
|
11
11
|
<a href="https://www.npmjs.com/package/msw" target="_blank">
|
|
12
12
|
<img src="https://img.shields.io/npm/v/msw.svg?style=for-the-badge&label=Latest&color=black" alt="Package version" />
|
|
13
13
|
</a>
|
|
14
|
-
<a href="https://circleci.com/gh/mswjs/msw" target="_blank">
|
|
15
|
-
<img src="https://img.shields.io/circleci/project/github/mswjs/msw/master.svg?style=for-the-badge&color=black" alt="Build status" />
|
|
16
|
-
</a>
|
|
17
14
|
<a href="https://www.npmjs.com/package/msw" target="_blank">
|
|
18
15
|
<img src="https://img.shields.io/npm/dm/msw?style=for-the-badge&color=black" alt="Downloads per month" />
|
|
19
16
|
</a>
|
|
@@ -27,8 +24,8 @@
|
|
|
27
24
|
## Features
|
|
28
25
|
|
|
29
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.
|
|
30
|
-
- **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
|
|
31
|
-
- **Familiar & Powerful**. Use [Express](https://github.com/expressjs/express)-like routing syntax to capture
|
|
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.
|
|
32
29
|
|
|
33
30
|
---
|
|
34
31
|
|
|
@@ -38,6 +35,8 @@
|
|
|
38
35
|
|
|
39
36
|
## Documentation
|
|
40
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
|
+
|
|
41
40
|
- [Documentation](https://mswjs.io/docs)
|
|
42
41
|
- [**Getting started**](https://mswjs.io/docs/getting-started/install)
|
|
43
42
|
- [Recipes](https://mswjs.io/docs/recipes)
|
|
@@ -54,27 +53,27 @@
|
|
|
54
53
|
|
|
55
54
|
### How does it work?
|
|
56
55
|
|
|
57
|
-
|
|
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.
|
|
58
57
|
|
|
59
|
-
**
|
|
58
|
+
**Take a look at this quick presentation on how Mock Service Worker functions in a browser:**
|
|
60
59
|
|
|
61
60
|
[](https://youtu.be/HcQCqboatZk)
|
|
62
61
|
|
|
63
62
|
### How is it different?
|
|
64
63
|
|
|
65
|
-
-
|
|
66
|
-
-
|
|
67
|
-
-
|
|
68
|
-
-
|
|
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.
|
|
69
68
|
|
|
70
69
|
### Usage example
|
|
71
70
|
|
|
72
71
|
```js
|
|
73
72
|
// src/mocks.js
|
|
74
|
-
// 1. Import
|
|
73
|
+
// 1. Import the library.
|
|
75
74
|
import { setupWorker, rest } from 'msw'
|
|
76
75
|
|
|
77
|
-
// 2.
|
|
76
|
+
// 2. Describe network behavior with request handlers.
|
|
78
77
|
const worker = setupWorker(
|
|
79
78
|
rest.get('https://github.com/octocat', (req, res, ctx) => {
|
|
80
79
|
return res(
|
|
@@ -87,7 +86,7 @@ const worker = setupWorker(
|
|
|
87
86
|
}),
|
|
88
87
|
)
|
|
89
88
|
|
|
90
|
-
// 3. Start the Service Worker.
|
|
89
|
+
// 3. Start request interception by starting the Service Worker.
|
|
91
90
|
worker.start()
|
|
92
91
|
```
|
|
93
92
|
|
|
@@ -95,103 +94,96 @@ Performing a `GET https://github.com/octocat` request in your application will r
|
|
|
95
94
|
|
|
96
95
|

|
|
97
96
|
|
|
98
|
-
> **Tip:** Did you know that although Service Worker runs in a separate thread, your mock definition executes on the client
|
|
97
|
+
> **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.
|
|
99
98
|
|
|
100
|
-
## Node
|
|
99
|
+
## Node.js
|
|
101
100
|
|
|
102
101
|
- [Learn more about using MSW in Node.js](https://mswjs.io/docs/getting-started/integrate/node)
|
|
103
102
|
- [`setupServer` API](https://mswjs.io/docs/api/setup-server)
|
|
104
103
|
|
|
105
104
|
### How does it work?
|
|
106
105
|
|
|
107
|
-
|
|
106
|
+
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.
|
|
108
107
|
|
|
109
108
|
### How is it different?
|
|
110
109
|
|
|
111
|
-
-
|
|
112
|
-
-
|
|
110
|
+
- Does not stub `fetch`, `axios`, etc. As a result, your tests know _nothing_ about mocking;
|
|
111
|
+
- 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.
|
|
113
112
|
|
|
114
113
|
### Usage example
|
|
115
114
|
|
|
116
|
-
|
|
115
|
+
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:
|
|
117
116
|
|
|
118
117
|
```js
|
|
119
|
-
// test/
|
|
120
|
-
|
|
118
|
+
// test/Dashboard.test.js
|
|
119
|
+
|
|
121
120
|
import React from 'react'
|
|
122
121
|
import { rest } from 'msw'
|
|
123
122
|
import { setupServer } from 'msw/node'
|
|
124
|
-
import { render, screen } from '@testing-library/react'
|
|
125
|
-
import
|
|
126
|
-
import Login from '../src/components/Login'
|
|
123
|
+
import { render, screen, waitFor } from '@testing-library/react'
|
|
124
|
+
import Dashboard from '../src/components/Dashboard'
|
|
127
125
|
|
|
128
126
|
const server = setupServer(
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
127
|
+
// Describe network behavior with request handlers.
|
|
128
|
+
// Tip: move the handlers into their own module and
|
|
129
|
+
// import it across your browser and Node.js setups!
|
|
130
|
+
rest.get('/posts', (req, res, ctx) => {
|
|
131
|
+
return res(
|
|
132
|
+
ctx.json([
|
|
133
|
+
{
|
|
134
|
+
id: 'f8dd058f-9006-4174-8d49-e3086bc39c21',
|
|
135
|
+
title: `Avoid Nesting When You're Testing`,
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
id: '8ac96078-6434-4959-80ed-cc834e7fef61',
|
|
139
|
+
title: `How I Built A Modern Website In 2021`,
|
|
140
|
+
},
|
|
141
|
+
]),
|
|
142
|
+
)
|
|
133
143
|
}),
|
|
134
144
|
)
|
|
135
145
|
|
|
136
|
-
// Enable
|
|
146
|
+
// Enable request interception.
|
|
137
147
|
beforeAll(() => server.listen())
|
|
138
148
|
|
|
139
|
-
// Reset
|
|
149
|
+
// Reset handlers so that each test could alter them
|
|
150
|
+
// without affecting other, unrelated tests.
|
|
140
151
|
afterEach(() => server.resetHandlers())
|
|
141
152
|
|
|
142
|
-
//
|
|
153
|
+
// Don't forget to clean up afterwards.
|
|
143
154
|
afterAll(() => server.close())
|
|
144
155
|
|
|
145
|
-
|
|
146
|
-
render(<
|
|
147
|
-
userEvent.type(
|
|
148
|
-
screen.getByRole('textbox', { name: /username/i }),
|
|
149
|
-
'john.maverick',
|
|
150
|
-
)
|
|
151
|
-
userEvent.type(
|
|
152
|
-
screen.getByRole('textbox', { name: /password/i }),
|
|
153
|
-
'super-secret',
|
|
154
|
-
)
|
|
155
|
-
userEvent.click(screen.getByText(/submit/i))
|
|
156
|
-
const alert = await screen.findByRole('alert')
|
|
157
|
-
|
|
158
|
-
// Assert successful login state
|
|
159
|
-
expect(alert).toHaveTextContent(/welcome/i)
|
|
160
|
-
expect(window.sessionStorage.getItem('token')).toEqual(fakeUserResponse.token)
|
|
161
|
-
})
|
|
156
|
+
it('displays the list of recent posts', async () => {
|
|
157
|
+
render(<Dashboard />)
|
|
162
158
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
screen.getByRole('
|
|
177
|
-
|
|
178
|
-
)
|
|
179
|
-
userEvent.type(
|
|
180
|
-
screen.getByRole('textbox', { name: /password/i }),
|
|
181
|
-
'super-secret',
|
|
182
|
-
)
|
|
183
|
-
userEvent.click(screen.getByText(/submit/i))
|
|
184
|
-
|
|
185
|
-
// Assert meaningful error message shown to the user
|
|
186
|
-
expect(alert).toHaveTextContent(/sorry, something went wrong/i)
|
|
187
|
-
expect(window.sessionStorage.getItem('token')).toBeNull()
|
|
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()
|
|
188
174
|
})
|
|
189
175
|
```
|
|
190
176
|
|
|
191
|
-
>
|
|
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.
|
|
192
180
|
|
|
193
181
|
## Sponsors
|
|
194
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
|
+
|
|
195
187
|
### Golden Sponsors
|
|
196
188
|
|
|
197
189
|
> Become our first golden sponsor and get featured right here, enjoying other perks like issue prioritization and a personal consulting session with us.
|
|
@@ -227,6 +219,8 @@ test('handles login exception', () => {
|
|
|
227
219
|
|
|
228
220
|
## Awards & Mentions
|
|
229
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
|
+
|
|
230
224
|
<table>
|
|
231
225
|
<tr valign="middle">
|
|
232
226
|
<td width="124">
|
|
@@ -253,7 +253,7 @@ declare abstract class RequestHandler<HandlerInfo extends RequestHandlerDefaultI
|
|
|
253
253
|
/**
|
|
254
254
|
* Print out the successfully handled request.
|
|
255
255
|
*/
|
|
256
|
-
abstract log(request: Request, response: SerializedResponse<any>,
|
|
256
|
+
abstract log(request: Request, response: SerializedResponse<any>, parsedResult: ParsedResult): void;
|
|
257
257
|
/**
|
|
258
258
|
* Parse the captured request to extract additional information from it.
|
|
259
259
|
* Parsed result is then exposed to other methods of this request handler.
|
package/lib/iife/index.js
CHANGED
|
@@ -467,7 +467,7 @@ var MockServiceWorker = (() => {
|
|
|
467
467
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
468
468
|
exports.headersToObject = void 0;
|
|
469
469
|
var singleValueHeaders = ["user-agent"];
|
|
470
|
-
function
|
|
470
|
+
function headersToObject2(headers) {
|
|
471
471
|
var headersObject = {};
|
|
472
472
|
headers.forEach(function(value, name) {
|
|
473
473
|
var isMultiValue = !singleValueHeaders.includes(name.toLowerCase()) && value.includes(",");
|
|
@@ -477,7 +477,7 @@ var MockServiceWorker = (() => {
|
|
|
477
477
|
});
|
|
478
478
|
return headersObject;
|
|
479
479
|
}
|
|
480
|
-
exports.headersToObject =
|
|
480
|
+
exports.headersToObject = headersToObject2;
|
|
481
481
|
}
|
|
482
482
|
});
|
|
483
483
|
|
|
@@ -628,13 +628,13 @@ var MockServiceWorker = (() => {
|
|
|
628
628
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
629
629
|
exports.flattenHeadersObject = void 0;
|
|
630
630
|
var reduceHeadersObject_1 = require_reduceHeadersObject();
|
|
631
|
-
function
|
|
631
|
+
function flattenHeadersObject2(headersObject) {
|
|
632
632
|
return reduceHeadersObject_1.reduceHeadersObject(headersObject, function(headers, name, value) {
|
|
633
633
|
headers[name] = [].concat(value).join("; ");
|
|
634
634
|
return headers;
|
|
635
635
|
}, {});
|
|
636
636
|
}
|
|
637
|
-
exports.flattenHeadersObject =
|
|
637
|
+
exports.flattenHeadersObject = flattenHeadersObject2;
|
|
638
638
|
}
|
|
639
639
|
});
|
|
640
640
|
|
|
@@ -5318,12 +5318,12 @@ var MockServiceWorker = (() => {
|
|
|
5318
5318
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5319
5319
|
exports.getArrayBuffer = exports.decodeBuffer = exports.encodeBuffer = void 0;
|
|
5320
5320
|
var web_encoding_1 = require_lib8();
|
|
5321
|
-
function
|
|
5321
|
+
function encodeBuffer3(text2) {
|
|
5322
5322
|
var encoder = new web_encoding_1.TextEncoder();
|
|
5323
5323
|
var encoded = encoder.encode(text2);
|
|
5324
5324
|
return getArrayBuffer(encoded);
|
|
5325
5325
|
}
|
|
5326
|
-
exports.encodeBuffer =
|
|
5326
|
+
exports.encodeBuffer = encodeBuffer3;
|
|
5327
5327
|
function decodeBuffer2(buffer, encoding) {
|
|
5328
5328
|
var decoder = new web_encoding_1.TextDecoder(encoding);
|
|
5329
5329
|
return decoder.decode(buffer);
|
|
@@ -12728,10 +12728,7 @@ spurious results.`);
|
|
|
12728
12728
|
const statusColor = getStatusCodeColor(response2.status);
|
|
12729
12729
|
console.groupCollapsed(devUtils.formatMessage("%s %s %s (%c%s%c)"), getTimestamp(), request.method, publicUrl, `color:${statusColor}`, `${response2.status} ${response2.statusText}`, "color:inherit");
|
|
12730
12730
|
console.log("Request", loggedRequest);
|
|
12731
|
-
console.log("Handler:",
|
|
12732
|
-
mask: this.info.path,
|
|
12733
|
-
resolver: this.resolver
|
|
12734
|
-
});
|
|
12731
|
+
console.log("Handler:", this);
|
|
12735
12732
|
console.log("Response", loggedResponse);
|
|
12736
12733
|
console.groupEnd();
|
|
12737
12734
|
}
|
|
@@ -12833,7 +12830,7 @@ Consider naming this operation or using "graphql.operation" request handler to i
|
|
|
12833
12830
|
const hasMatchingOperationName = this.info.operationName instanceof RegExp ? this.info.operationName.test(parsedResult.operationName || "") : parsedResult.operationName === this.info.operationName;
|
|
12834
12831
|
return hasMatchingUrl.matches && hasMatchingOperationType && hasMatchingOperationName;
|
|
12835
12832
|
}
|
|
12836
|
-
log(request, response2,
|
|
12833
|
+
log(request, response2, parsedRequest) {
|
|
12837
12834
|
const loggedRequest = prepareRequest(request);
|
|
12838
12835
|
const loggedResponse = prepareResponse(response2);
|
|
12839
12836
|
const statusColor = getStatusCodeColor(response2.status);
|
|
@@ -12964,7 +12961,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
|
12964
12961
|
|
|
12965
12962
|
// src/utils/handleRequest.ts
|
|
12966
12963
|
async function handleRequest(request, handlers, options, emitter, handleRequestOptions) {
|
|
12967
|
-
var _a, _b, _c, _d, _e, _f
|
|
12964
|
+
var _a, _b, _c, _d, _e, _f;
|
|
12968
12965
|
emitter.emit("request:start", request);
|
|
12969
12966
|
if (request.headers.get("x-msw-bypass") === "true") {
|
|
12970
12967
|
emitter.emit("request:end", request);
|
|
@@ -13005,11 +13002,21 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
|
13005
13002
|
const requiredLookupResult = lookupResult;
|
|
13006
13003
|
const transformedResponse = ((_e = handleRequestOptions == null ? void 0 : handleRequestOptions.transformResponse) == null ? void 0 : _e.call(handleRequestOptions, response2)) || response2;
|
|
13007
13004
|
(_f = handleRequestOptions == null ? void 0 : handleRequestOptions.onMockedResponse) == null ? void 0 : _f.call(handleRequestOptions, transformedResponse, requiredLookupResult);
|
|
13008
|
-
(_g = handleRequestOptions == null ? void 0 : handleRequestOptions.onMockedResponseSent) == null ? void 0 : _g.call(handleRequestOptions, transformedResponse, requiredLookupResult);
|
|
13009
13005
|
emitter.emit("request:end", request);
|
|
13010
13006
|
return transformedResponse;
|
|
13011
13007
|
}
|
|
13012
13008
|
|
|
13009
|
+
// src/utils/logging/serializeResponse.ts
|
|
13010
|
+
var import_headers_polyfill8 = __toESM(require_lib());
|
|
13011
|
+
function serializeResponse(source) {
|
|
13012
|
+
return {
|
|
13013
|
+
status: source.status,
|
|
13014
|
+
statusText: source.statusText,
|
|
13015
|
+
headers: (0, import_headers_polyfill8.flattenHeadersObject)((0, import_headers_polyfill8.headersToObject)(source.headers)),
|
|
13016
|
+
body: source.body
|
|
13017
|
+
};
|
|
13018
|
+
}
|
|
13019
|
+
|
|
13013
13020
|
// src/setupWorker/start/createRequestListener.ts
|
|
13014
13021
|
var createRequestListener = (context, options) => {
|
|
13015
13022
|
return async (event, message) => {
|
|
@@ -13021,7 +13028,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
|
13021
13028
|
onPassthroughResponse() {
|
|
13022
13029
|
messageChannel.postMessage("NOT_FOUND");
|
|
13023
13030
|
},
|
|
13024
|
-
async onMockedResponse(response2) {
|
|
13031
|
+
async onMockedResponse(response2, { handler, publicRequest, parsedRequest }) {
|
|
13025
13032
|
if (response2.body instanceof ReadableStream) {
|
|
13026
13033
|
throw new Error(devUtils.formatMessage('Failed to construct a mocked response with a "ReadableStream" body: mocked streams are not supported. Follow https://github.com/mswjs/msw/issues/1336 for more details.'));
|
|
13027
13034
|
}
|
|
@@ -13031,12 +13038,11 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
|
13031
13038
|
messageChannel.postMessage("MOCK_RESPONSE", __spreadProps(__spreadValues({}, response2), {
|
|
13032
13039
|
body: responseBody
|
|
13033
13040
|
}), [responseBodyBuffer]);
|
|
13034
|
-
|
|
13035
|
-
|
|
13036
|
-
|
|
13037
|
-
|
|
13041
|
+
if (!options.quiet) {
|
|
13042
|
+
context.emitter.once("response:mocked", (response3) => {
|
|
13043
|
+
handler.log(publicRequest, serializeResponse(response3), parsedRequest);
|
|
13044
|
+
});
|
|
13038
13045
|
}
|
|
13039
|
-
handler.log(publicRequest, response2, handler, parsedRequest);
|
|
13040
13046
|
}
|
|
13041
13047
|
});
|
|
13042
13048
|
} catch (error2) {
|
|
@@ -13262,11 +13268,44 @@ If this message still persists after updating, please report an issue: https://g
|
|
|
13262
13268
|
}
|
|
13263
13269
|
|
|
13264
13270
|
// src/setupWorker/start/createFallbackRequestListener.ts
|
|
13265
|
-
var
|
|
13271
|
+
var import_interceptors4 = __toESM(require_lib9());
|
|
13266
13272
|
var import_fetch3 = __toESM(require_fetch());
|
|
13267
13273
|
var import_XMLHttpRequest = __toESM(require_XMLHttpRequest());
|
|
13274
|
+
|
|
13275
|
+
// src/utils/request/createResponseFromIsomorphicResponse.ts
|
|
13276
|
+
var import_interceptors3 = __toESM(require_lib9());
|
|
13277
|
+
var noop = () => {
|
|
13278
|
+
throw new Error("Not implemented");
|
|
13279
|
+
};
|
|
13280
|
+
function createResponseFromIsomorphicResponse(response2) {
|
|
13281
|
+
return __spreadProps(__spreadValues({}, response2), {
|
|
13282
|
+
ok: response2.status >= 200 && response2.status < 300,
|
|
13283
|
+
url: "",
|
|
13284
|
+
type: "default",
|
|
13285
|
+
status: response2.status,
|
|
13286
|
+
statusText: response2.statusText,
|
|
13287
|
+
headers: response2.headers,
|
|
13288
|
+
body: new ReadableStream(),
|
|
13289
|
+
redirected: response2.headers.get("Location") != null,
|
|
13290
|
+
async text() {
|
|
13291
|
+
return response2.body || "";
|
|
13292
|
+
},
|
|
13293
|
+
async json() {
|
|
13294
|
+
return JSON.parse(response2.body || "");
|
|
13295
|
+
},
|
|
13296
|
+
async arrayBuffer() {
|
|
13297
|
+
return (0, import_interceptors3.encodeBuffer)(response2.body || "");
|
|
13298
|
+
},
|
|
13299
|
+
bodyUsed: false,
|
|
13300
|
+
formData: noop,
|
|
13301
|
+
blob: noop,
|
|
13302
|
+
clone: noop
|
|
13303
|
+
});
|
|
13304
|
+
}
|
|
13305
|
+
|
|
13306
|
+
// src/setupWorker/start/createFallbackRequestListener.ts
|
|
13268
13307
|
function createFallbackRequestListener(context, options) {
|
|
13269
|
-
const interceptor = new
|
|
13308
|
+
const interceptor = new import_interceptors4.BatchInterceptor({
|
|
13270
13309
|
name: "fallback",
|
|
13271
13310
|
interceptors: [new import_fetch3.FetchInterceptor(), new import_XMLHttpRequest.XMLHttpRequestInterceptor()]
|
|
13272
13311
|
});
|
|
@@ -13284,9 +13323,11 @@ If this message still persists after updating, please report an issue: https://g
|
|
|
13284
13323
|
delay: response3.delay
|
|
13285
13324
|
};
|
|
13286
13325
|
},
|
|
13287
|
-
|
|
13326
|
+
onMockedResponse(_, { handler, publicRequest, parsedRequest }) {
|
|
13288
13327
|
if (!options.quiet) {
|
|
13289
|
-
|
|
13328
|
+
context.emitter.once("response:mocked", (response3) => {
|
|
13329
|
+
handler.log(publicRequest, serializeResponse(response3), parsedRequest);
|
|
13330
|
+
});
|
|
13290
13331
|
}
|
|
13291
13332
|
}
|
|
13292
13333
|
});
|
|
@@ -13294,6 +13335,17 @@ If this message still persists after updating, please report an issue: https://g
|
|
|
13294
13335
|
request.respondWith(response2);
|
|
13295
13336
|
}
|
|
13296
13337
|
});
|
|
13338
|
+
interceptor.on("response", (request, response2) => {
|
|
13339
|
+
if (!request.id) {
|
|
13340
|
+
return;
|
|
13341
|
+
}
|
|
13342
|
+
const browserResponse = createResponseFromIsomorphicResponse(response2);
|
|
13343
|
+
if (response2.headers.get("x-powered-by") === "msw") {
|
|
13344
|
+
context.emitter.emit("response:mocked", browserResponse, request.id);
|
|
13345
|
+
} else {
|
|
13346
|
+
context.emitter.emit("response:bypass", browserResponse, request.id);
|
|
13347
|
+
}
|
|
13348
|
+
});
|
|
13297
13349
|
interceptor.apply();
|
|
13298
13350
|
return interceptor;
|
|
13299
13351
|
}
|