msw-fetch-mock 0.3.0 → 0.3.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 +72 -6
- package/dist/browser.cjs +43 -0
- package/dist/browser.d.cts +15 -0
- package/dist/browser.d.ts +15 -9
- package/dist/browser.js +42 -7
- package/dist/chunk-43CWIVXO.cjs +540 -0
- package/dist/chunk-A56K5DPD.cjs +58 -0
- package/dist/chunk-ATXIIOZA.js +540 -0
- package/dist/chunk-D26SJTTI.js +27 -0
- package/dist/chunk-HYRDB2FH.cjs +27 -0
- package/dist/chunk-KSDUQENI.js +58 -0
- package/dist/fetch-mock-BNke4Oik.d.cts +199 -0
- package/dist/fetch-mock-BNke4Oik.d.ts +199 -0
- package/dist/index.cjs +19 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +19 -1
- package/dist/legacy.cjs +71 -0
- package/dist/legacy.d.cts +38 -0
- package/dist/legacy.d.ts +38 -0
- package/dist/legacy.js +71 -0
- package/dist/node.cjs +19 -0
- package/dist/node.d.cts +18 -0
- package/dist/node.d.ts +17 -10
- package/dist/node.js +19 -12
- package/docs/api.md +121 -13
- package/docs/msw-v1-legacy.md +94 -0
- package/package.json +34 -10
- package/dist/browser-adapter.d.ts +0 -10
- package/dist/browser-adapter.d.ts.map +0 -1
- package/dist/browser-adapter.js +0 -20
- package/dist/browser.d.ts.map +0 -1
- package/dist/fetch-mock.d.ts +0 -31
- package/dist/fetch-mock.d.ts.map +0 -1
- package/dist/fetch-mock.js +0 -347
- package/dist/index.d.ts.map +0 -1
- package/dist/mock-call-history.d.ts +0 -65
- package/dist/mock-call-history.d.ts.map +0 -1
- package/dist/mock-call-history.js +0 -140
- package/dist/node-adapter.d.ts +0 -11
- package/dist/node-adapter.d.ts.map +0 -1
- package/dist/node-adapter.js +0 -34
- package/dist/node.d.ts.map +0 -1
- package/dist/types.d.ts +0 -74
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -1
package/dist/node.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkA56K5DPDcjs = require('./chunk-A56K5DPD.cjs');
|
|
6
|
+
require('./chunk-HYRDB2FH.cjs');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
var _chunk43CWIVXOcjs = require('./chunk-43CWIVXO.cjs');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
exports.FetchMock = _chunk43CWIVXOcjs.FetchMock; exports.MockCallHistory = _chunk43CWIVXOcjs.MockCallHistory; exports.MockCallHistoryLog = _chunk43CWIVXOcjs.MockCallHistoryLog; exports.NodeMswAdapter = _chunkA56K5DPDcjs.NodeMswAdapter; exports.createFetchMock = _chunkA56K5DPDcjs.createFetchMock; exports.fetchMock = _chunkA56K5DPDcjs.fetchMock;
|
package/dist/node.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { M as MswAdapter, i as SetupServerLike, R as ResolvedActivateOptions, F as FetchMock } from './fetch-mock-BNke4Oik.cjs';
|
|
2
|
+
export { A as ActivateOptions, C as CallHistoryFilterCriteria, H as HandlerFactory, I as InterceptOptions, a as MockCallHistory, b as MockCallHistoryLog, c as MockCallHistoryLogData, d as MockInterceptor, e as MockPool, f as MockReplyChain, O as OnUnhandledRequest, P as PendingInterceptor, j as ReplyCallback, g as ReplyOptions, k as SingleReplyCallback, l as SingleReplyResult } from './fetch-mock-BNke4Oik.cjs';
|
|
3
|
+
|
|
4
|
+
declare class NodeMswAdapter implements MswAdapter {
|
|
5
|
+
private server;
|
|
6
|
+
private readonly ownsServer;
|
|
7
|
+
constructor(externalServer?: SetupServerLike);
|
|
8
|
+
use(...handlers: Array<unknown>): void;
|
|
9
|
+
resetHandlers(...handlers: Array<unknown>): void;
|
|
10
|
+
activate(options: ResolvedActivateOptions): void;
|
|
11
|
+
deactivate(): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function createFetchMock(server?: SetupServerLike): FetchMock;
|
|
15
|
+
/** Pre-built singleton for quick standalone use (Cloudflare migration compatible). */
|
|
16
|
+
declare const fetchMock: FetchMock;
|
|
17
|
+
|
|
18
|
+
export { FetchMock, MswAdapter, NodeMswAdapter, SetupServerLike, createFetchMock, fetchMock };
|
package/dist/node.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import { FetchMock } from './fetch-mock';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { M as MswAdapter, i as SetupServerLike, R as ResolvedActivateOptions, F as FetchMock } from './fetch-mock-BNke4Oik.js';
|
|
2
|
+
export { A as ActivateOptions, C as CallHistoryFilterCriteria, H as HandlerFactory, I as InterceptOptions, a as MockCallHistory, b as MockCallHistoryLog, c as MockCallHistoryLogData, d as MockInterceptor, e as MockPool, f as MockReplyChain, O as OnUnhandledRequest, P as PendingInterceptor, j as ReplyCallback, g as ReplyOptions, k as SingleReplyCallback, l as SingleReplyResult } from './fetch-mock-BNke4Oik.js';
|
|
3
|
+
|
|
4
|
+
declare class NodeMswAdapter implements MswAdapter {
|
|
5
|
+
private server;
|
|
6
|
+
private readonly ownsServer;
|
|
7
|
+
constructor(externalServer?: SetupServerLike);
|
|
8
|
+
use(...handlers: Array<unknown>): void;
|
|
9
|
+
resetHandlers(...handlers: Array<unknown>): void;
|
|
10
|
+
activate(options: ResolvedActivateOptions): void;
|
|
11
|
+
deactivate(): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function createFetchMock(server?: SetupServerLike): FetchMock;
|
|
6
15
|
/** Pre-built singleton for quick standalone use (Cloudflare migration compatible). */
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export {
|
|
10
|
-
export type { MockCallHistoryLogData, CallHistoryFilterCriteria } from './mock-call-history';
|
|
11
|
-
//# sourceMappingURL=node.d.ts.map
|
|
16
|
+
declare const fetchMock: FetchMock;
|
|
17
|
+
|
|
18
|
+
export { FetchMock, MswAdapter, NodeMswAdapter, SetupServerLike, createFetchMock, fetchMock };
|
package/dist/node.js
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export {
|
|
1
|
+
import {
|
|
2
|
+
NodeMswAdapter,
|
|
3
|
+
createFetchMock,
|
|
4
|
+
fetchMock
|
|
5
|
+
} from "./chunk-KSDUQENI.js";
|
|
6
|
+
import "./chunk-D26SJTTI.js";
|
|
7
|
+
import {
|
|
8
|
+
FetchMock,
|
|
9
|
+
MockCallHistory,
|
|
10
|
+
MockCallHistoryLog
|
|
11
|
+
} from "./chunk-ATXIIOZA.js";
|
|
12
|
+
export {
|
|
13
|
+
FetchMock,
|
|
14
|
+
MockCallHistory,
|
|
15
|
+
MockCallHistoryLog,
|
|
16
|
+
NodeMswAdapter,
|
|
17
|
+
createFetchMock,
|
|
18
|
+
fetchMock
|
|
19
|
+
};
|
package/docs/api.md
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## Import Paths
|
|
4
4
|
|
|
5
|
-
| Path | Environment | MSW
|
|
6
|
-
| ------------------------ | ---------------------------- |
|
|
7
|
-
| `msw-fetch-mock` | Node.js (re-exports `/node`) |
|
|
8
|
-
| `msw-fetch-mock/node` | Node.js |
|
|
9
|
-
| `msw-fetch-mock/browser` | Browser |
|
|
5
|
+
| Path | Environment | MSW version |
|
|
6
|
+
| ------------------------ | ---------------------------- | ----------- |
|
|
7
|
+
| `msw-fetch-mock` | Node.js (re-exports `/node`) | v2 |
|
|
8
|
+
| `msw-fetch-mock/node` | Node.js | v2 |
|
|
9
|
+
| `msw-fetch-mock/browser` | Browser | v2 |
|
|
10
|
+
| `msw-fetch-mock/legacy` | Node.js (MSW v1) | v1 |
|
|
10
11
|
|
|
11
12
|
## `fetchMock` (singleton)
|
|
12
13
|
|
|
@@ -57,6 +58,19 @@ beforeAll(async () => {
|
|
|
57
58
|
});
|
|
58
59
|
```
|
|
59
60
|
|
|
61
|
+
## `createFetchMock(rest, server?)` (Legacy)
|
|
62
|
+
|
|
63
|
+
Creates a `FetchMock` for MSW v1 environments. See [MSW v1 Legacy Guide](msw-v1-legacy.md).
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { rest } from 'msw';
|
|
67
|
+
import { setupServer } from 'msw/node';
|
|
68
|
+
import { createFetchMock } from 'msw-fetch-mock/legacy';
|
|
69
|
+
|
|
70
|
+
const server = setupServer();
|
|
71
|
+
const fetchMock = createFetchMock(rest, server);
|
|
72
|
+
```
|
|
73
|
+
|
|
60
74
|
## `new FetchMock(adapter?)`
|
|
61
75
|
|
|
62
76
|
Creates a `FetchMock` instance with an explicit `MswAdapter`.
|
|
@@ -142,12 +156,23 @@ fetchMock.calls.clear();
|
|
|
142
156
|
|
|
143
157
|
### `fetchMock.get(origin)`
|
|
144
158
|
|
|
145
|
-
Returns a `MockPool` scoped to the given origin.
|
|
159
|
+
Returns a `MockPool` scoped to the given origin. The `origin` parameter accepts three forms:
|
|
146
160
|
|
|
147
161
|
```typescript
|
|
162
|
+
// String — exact origin match
|
|
148
163
|
const pool = fetchMock.get('https://api.example.com');
|
|
164
|
+
|
|
165
|
+
// RegExp — match against URL origin
|
|
166
|
+
const pool = fetchMock.get(/\.example\.com$/);
|
|
167
|
+
|
|
168
|
+
// Function — custom origin predicate
|
|
169
|
+
const pool = fetchMock.get((origin) => origin.startsWith('https://'));
|
|
149
170
|
```
|
|
150
171
|
|
|
172
|
+
| Parameter | Type | Required | Description |
|
|
173
|
+
| --------- | ------------------------------------------------- | -------- | -------------- |
|
|
174
|
+
| `origin` | `string \| RegExp \| (origin: string) => boolean` | Yes | Origin matcher |
|
|
175
|
+
|
|
151
176
|
### `fetchMock.disableNetConnect()`
|
|
152
177
|
|
|
153
178
|
Prevents any real network requests. Unmatched requests will throw.
|
|
@@ -174,6 +199,44 @@ fetchMock.enableNetConnect((host) => host.endsWith('.test'));
|
|
|
174
199
|
| --------- | ----------------------------------------------- | -------- | ------------------------------------ |
|
|
175
200
|
| `matcher` | `string \| RegExp \| (host: string) => boolean` | No | Host matcher. Allows all if omitted. |
|
|
176
201
|
|
|
202
|
+
### `fetchMock.defaultReplyHeaders(headers)`
|
|
203
|
+
|
|
204
|
+
Sets default headers that are included in every response. Per-reply headers merge with and override defaults.
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
fetchMock.defaultReplyHeaders({
|
|
208
|
+
'x-request-id': 'test-123',
|
|
209
|
+
'cache-control': 'no-store',
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// This reply will have both x-request-id and content-type
|
|
213
|
+
fetchMock
|
|
214
|
+
.get('https://api.example.com')
|
|
215
|
+
.intercept({ path: '/data' })
|
|
216
|
+
.reply(200, { ok: true }, { headers: { 'content-type': 'application/json' } });
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
> `reset()` clears default reply headers.
|
|
220
|
+
|
|
221
|
+
| Parameter | Type | Required | Description |
|
|
222
|
+
| --------- | ------------------------ | -------- | ------------------------------------ |
|
|
223
|
+
| `headers` | `Record<string, string>` | Yes | Headers to include in every response |
|
|
224
|
+
|
|
225
|
+
### `fetchMock.enableCallHistory()`
|
|
226
|
+
|
|
227
|
+
Enables call history recording. This is the default state.
|
|
228
|
+
|
|
229
|
+
### `fetchMock.disableCallHistory()`
|
|
230
|
+
|
|
231
|
+
Disables call history recording. Requests are still intercepted and replied to, but not recorded. Useful when you want to reduce overhead in performance-sensitive tests.
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
fetchMock.disableCallHistory();
|
|
235
|
+
// ... requests are intercepted but not recorded
|
|
236
|
+
fetchMock.enableCallHistory();
|
|
237
|
+
// ... requests are now recorded again
|
|
238
|
+
```
|
|
239
|
+
|
|
177
240
|
### `fetchMock.getCallHistory()`
|
|
178
241
|
|
|
179
242
|
Returns the `MockCallHistory` instance. Cloudflare-compatible alias for `fetchMock.calls`.
|
|
@@ -182,6 +245,10 @@ Returns the `MockCallHistory` instance. Cloudflare-compatible alias for `fetchMo
|
|
|
182
245
|
|
|
183
246
|
Clears all recorded calls. Cloudflare-compatible alias for `fetchMock.calls.clear()`.
|
|
184
247
|
|
|
248
|
+
### `fetchMock.clearAllCallHistory()`
|
|
249
|
+
|
|
250
|
+
Alias for `clearCallHistory()`.
|
|
251
|
+
|
|
185
252
|
### `fetchMock.assertNoPendingInterceptors()`
|
|
186
253
|
|
|
187
254
|
Throws an error if any registered interceptor has not been consumed. This is a **pure assertion** — it does not clear call history, interceptors, or handlers. Use `reset()` to clean up state.
|
|
@@ -195,7 +262,7 @@ afterEach(() => {
|
|
|
195
262
|
|
|
196
263
|
### `fetchMock.reset()`
|
|
197
264
|
|
|
198
|
-
Clears all interceptors, call history, and MSW handlers. Resets the instance to a clean state without stopping the server. Use in `afterEach` after asserting no pending interceptors.
|
|
265
|
+
Clears all interceptors, call history, default reply headers, and MSW handlers. Resets the instance to a clean state without stopping the server. Use in `afterEach` after asserting no pending interceptors.
|
|
199
266
|
|
|
200
267
|
```typescript
|
|
201
268
|
afterEach(() => {
|
|
@@ -302,7 +369,7 @@ Returns: `MockInterceptor`
|
|
|
302
369
|
|
|
303
370
|
### `interceptor.reply(status, body?, options?)`
|
|
304
371
|
|
|
305
|
-
Defines the mock response.
|
|
372
|
+
Defines the mock response with a static body.
|
|
306
373
|
|
|
307
374
|
```typescript
|
|
308
375
|
// Static body
|
|
@@ -310,7 +377,13 @@ Defines the mock response.
|
|
|
310
377
|
|
|
311
378
|
// With response headers
|
|
312
379
|
.reply(200, { users: [] }, { headers: { 'x-request-id': '123' } })
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### `interceptor.reply(status, callback)`
|
|
313
383
|
|
|
384
|
+
Defines the mock response with a dynamic body callback.
|
|
385
|
+
|
|
386
|
+
```typescript
|
|
314
387
|
// Callback (receives request info)
|
|
315
388
|
.reply(200, (req) => {
|
|
316
389
|
const input = JSON.parse(req.body!);
|
|
@@ -318,13 +391,39 @@ Defines the mock response.
|
|
|
318
391
|
})
|
|
319
392
|
```
|
|
320
393
|
|
|
394
|
+
### `interceptor.reply(callback)`
|
|
395
|
+
|
|
396
|
+
Single callback form — full control over status code, body, and response options.
|
|
397
|
+
|
|
398
|
+
```typescript
|
|
399
|
+
.reply((req) => {
|
|
400
|
+
const data = JSON.parse(req.body!);
|
|
401
|
+
return {
|
|
402
|
+
statusCode: 201,
|
|
403
|
+
data: { id: '1', ...data },
|
|
404
|
+
responseOptions: { headers: { 'x-created': 'true' } },
|
|
405
|
+
};
|
|
406
|
+
})
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
The callback receives `{ body: string | null }` and must return (or resolve to):
|
|
410
|
+
|
|
411
|
+
```typescript
|
|
412
|
+
interface SingleReplyResult {
|
|
413
|
+
statusCode: number;
|
|
414
|
+
data: unknown;
|
|
415
|
+
responseOptions?: { headers?: Record<string, string> };
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
|
|
321
419
|
Returns: `MockReplyChain`
|
|
322
420
|
|
|
323
|
-
| Parameter
|
|
324
|
-
|
|
|
325
|
-
| `status`
|
|
326
|
-
| `body`
|
|
327
|
-
| `
|
|
421
|
+
| Parameter | Type | Description |
|
|
422
|
+
| ---------- | -------------------------------------- | ------------------------- |
|
|
423
|
+
| `status` | `number` | HTTP status code |
|
|
424
|
+
| `body` | `unknown \| (req) => unknown` | Response body or callback |
|
|
425
|
+
| `callback` | `SingleReplyCallback` | Full-control callback |
|
|
426
|
+
| `options` | `{ headers?: Record<string, string> }` | Response headers |
|
|
328
427
|
|
|
329
428
|
### `interceptor.replyWithError(error)`
|
|
330
429
|
|
|
@@ -368,6 +467,15 @@ Adds a delay before the response is sent.
|
|
|
368
467
|
.reply(200, { ok: true }).delay(500)
|
|
369
468
|
```
|
|
370
469
|
|
|
470
|
+
### `chain.replyContentLength()`
|
|
471
|
+
|
|
472
|
+
Automatically adds a `Content-Length` header to the response based on the JSON-serialized body size.
|
|
473
|
+
|
|
474
|
+
```typescript
|
|
475
|
+
.reply(200, { ok: true }).replyContentLength()
|
|
476
|
+
// Response will include Content-Length: 13
|
|
477
|
+
```
|
|
478
|
+
|
|
371
479
|
---
|
|
372
480
|
|
|
373
481
|
## `MockCallHistory`
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# MSW v1 Legacy Guide
|
|
2
|
+
|
|
3
|
+
If you're using MSW v1 (`msw@^1.x`), use the `msw-fetch-mock/legacy` subpath export. The API is the same as the v2 version — only the setup differs.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -D msw-fetch-mock msw@^1
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { rest } from 'msw';
|
|
15
|
+
import { setupServer } from 'msw/node';
|
|
16
|
+
import { createFetchMock } from 'msw-fetch-mock/legacy';
|
|
17
|
+
|
|
18
|
+
const server = setupServer();
|
|
19
|
+
const fetchMock = createFetchMock(rest, server);
|
|
20
|
+
|
|
21
|
+
beforeAll(() => fetchMock.activate());
|
|
22
|
+
afterAll(() => fetchMock.deactivate());
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
fetchMock.assertNoPendingInterceptors();
|
|
25
|
+
fetchMock.reset();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('mocks a GET request', async () => {
|
|
29
|
+
fetchMock
|
|
30
|
+
.get('https://api.example.com')
|
|
31
|
+
.intercept({ path: '/users', method: 'GET' })
|
|
32
|
+
.reply(200, { users: [{ id: '1', name: 'Alice' }] });
|
|
33
|
+
|
|
34
|
+
const res = await fetch('https://api.example.com/users');
|
|
35
|
+
const data = await res.json();
|
|
36
|
+
|
|
37
|
+
expect(data.users).toHaveLength(1);
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API Comparison
|
|
42
|
+
|
|
43
|
+
| MSW v2 (`msw-fetch-mock`) | MSW v1 (`msw-fetch-mock/legacy`) |
|
|
44
|
+
| -------------------------------------------- | ----------------------------------------------- |
|
|
45
|
+
| `import { fetchMock } from 'msw-fetch-mock'` | N/A (no singleton — must use `createFetchMock`) |
|
|
46
|
+
| `import { createFetchMock } from '.../node'` | `import { createFetchMock } from '.../legacy'` |
|
|
47
|
+
| `createFetchMock(server?)` | `createFetchMock(rest, server?)` |
|
|
48
|
+
| `import { http } from 'msw'` | `import { rest } from 'msw'` |
|
|
49
|
+
|
|
50
|
+
## Key Differences
|
|
51
|
+
|
|
52
|
+
| Aspect | MSW v2 | MSW v1 (Legacy) |
|
|
53
|
+
| ----------------- | ----------------------------------------- | ------------------------------------- |
|
|
54
|
+
| Import | `msw-fetch-mock` or `msw-fetch-mock/node` | `msw-fetch-mock/legacy` |
|
|
55
|
+
| Factory | `createFetchMock(server?)` | `createFetchMock(rest, server?)` |
|
|
56
|
+
| Singleton | `fetchMock` pre-built instance | Not available — use `createFetchMock` |
|
|
57
|
+
| Handler internals | Uses `http.*` + `HttpResponse` | Uses `rest.*` + `(req, res, ctx)` |
|
|
58
|
+
| MSW version | `msw@^2.12.7` | `msw@^1.0.0` |
|
|
59
|
+
|
|
60
|
+
## Exports
|
|
61
|
+
|
|
62
|
+
The `msw-fetch-mock/legacy` subpath exports:
|
|
63
|
+
|
|
64
|
+
| Export | Type | Description |
|
|
65
|
+
| -------------------------------- | -------- | ------------------------------------------------- |
|
|
66
|
+
| `createFetchMock(rest, server?)` | Function | Create a FetchMock instance for MSW v1 |
|
|
67
|
+
| `FetchMock` | Class | Core mock class (same as v2) |
|
|
68
|
+
| `createLegacyHandlerFactory` | Function | Low-level: create a v1-compatible handler factory |
|
|
69
|
+
| `LegacyRestApi` | Type | Type for MSW v1's `rest` object |
|
|
70
|
+
|
|
71
|
+
## Migrating from MSW v1 to v2
|
|
72
|
+
|
|
73
|
+
When you upgrade MSW from v1 to v2, update your imports:
|
|
74
|
+
|
|
75
|
+
```diff
|
|
76
|
+
- import { rest } from 'msw';
|
|
77
|
+
- import { createFetchMock } from 'msw-fetch-mock/legacy';
|
|
78
|
+
- const fetchMock = createFetchMock(rest, server);
|
|
79
|
+
+ import { createFetchMock } from 'msw-fetch-mock/node';
|
|
80
|
+
+ const fetchMock = createFetchMock(server);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Or use the singleton for standalone mode:
|
|
84
|
+
|
|
85
|
+
```diff
|
|
86
|
+
- import { rest } from 'msw';
|
|
87
|
+
- import { setupServer } from 'msw/node';
|
|
88
|
+
- import { createFetchMock } from 'msw-fetch-mock/legacy';
|
|
89
|
+
- const server = setupServer();
|
|
90
|
+
- const fetchMock = createFetchMock(rest, server);
|
|
91
|
+
+ import { fetchMock } from 'msw-fetch-mock';
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The rest of the API (`get()`, `intercept()`, `reply()`, `calls`, etc.) stays the same.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msw-fetch-mock",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Undici-style fetch mock API built on MSW (Mock Service Worker)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,18 +24,39 @@
|
|
|
24
24
|
"exports": {
|
|
25
25
|
".": {
|
|
26
26
|
"source": "./src/index.ts",
|
|
27
|
-
"types":
|
|
28
|
-
|
|
27
|
+
"types": {
|
|
28
|
+
"import": "./dist/index.d.ts",
|
|
29
|
+
"require": "./dist/index.d.cts"
|
|
30
|
+
},
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.cjs"
|
|
29
33
|
},
|
|
30
34
|
"./node": {
|
|
31
35
|
"source": "./src/node.ts",
|
|
32
|
-
"types":
|
|
33
|
-
|
|
36
|
+
"types": {
|
|
37
|
+
"import": "./dist/node.d.ts",
|
|
38
|
+
"require": "./dist/node.d.cts"
|
|
39
|
+
},
|
|
40
|
+
"import": "./dist/node.js",
|
|
41
|
+
"require": "./dist/node.cjs"
|
|
34
42
|
},
|
|
35
43
|
"./browser": {
|
|
36
44
|
"source": "./src/browser.ts",
|
|
37
|
-
"types":
|
|
38
|
-
|
|
45
|
+
"types": {
|
|
46
|
+
"import": "./dist/browser.d.ts",
|
|
47
|
+
"require": "./dist/browser.d.cts"
|
|
48
|
+
},
|
|
49
|
+
"import": "./dist/browser.js",
|
|
50
|
+
"require": "./dist/browser.cjs"
|
|
51
|
+
},
|
|
52
|
+
"./legacy": {
|
|
53
|
+
"source": "./src/legacy.ts",
|
|
54
|
+
"types": {
|
|
55
|
+
"import": "./dist/legacy.d.ts",
|
|
56
|
+
"require": "./dist/legacy.d.cts"
|
|
57
|
+
},
|
|
58
|
+
"import": "./dist/legacy.js",
|
|
59
|
+
"require": "./dist/legacy.cjs"
|
|
39
60
|
}
|
|
40
61
|
},
|
|
41
62
|
"files": [
|
|
@@ -46,17 +67,19 @@
|
|
|
46
67
|
],
|
|
47
68
|
"scripts": {
|
|
48
69
|
"prepare": "lefthook install",
|
|
49
|
-
"build": "
|
|
70
|
+
"build": "tsup",
|
|
71
|
+
"typecheck": "tsc --noEmit",
|
|
50
72
|
"test": "vitest",
|
|
51
73
|
"test:run": "vitest run",
|
|
52
74
|
"lint": "eslint src",
|
|
53
75
|
"lint:fix": "eslint src --fix",
|
|
54
76
|
"format": "prettier --write .",
|
|
55
77
|
"format:check": "prettier --check .",
|
|
56
|
-
"prepublishOnly": "
|
|
78
|
+
"prepublishOnly": "tsup",
|
|
79
|
+
"test:e2e": "bash scripts/test-e2e.sh"
|
|
57
80
|
},
|
|
58
81
|
"peerDependencies": {
|
|
59
|
-
"msw": "^2.12.7"
|
|
82
|
+
"msw": "^1.0.0 || ^2.12.7"
|
|
60
83
|
},
|
|
61
84
|
"devDependencies": {
|
|
62
85
|
"@eslint/js": "^9.39.2",
|
|
@@ -64,6 +87,7 @@
|
|
|
64
87
|
"lefthook": "^2.0.15",
|
|
65
88
|
"msw": "^2.12.7",
|
|
66
89
|
"prettier": "^3.8.1",
|
|
90
|
+
"tsup": "^8.5.1",
|
|
67
91
|
"typescript": "^5.9.3",
|
|
68
92
|
"typescript-eslint": "^8.53.1",
|
|
69
93
|
"vitest": "^4.0.17"
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { MswAdapter, ResolvedActivateOptions, SetupWorkerLike } from './types';
|
|
2
|
-
export declare class BrowserMswAdapter implements MswAdapter {
|
|
3
|
-
private readonly worker;
|
|
4
|
-
constructor(worker: SetupWorkerLike);
|
|
5
|
-
use(...handlers: Array<unknown>): void;
|
|
6
|
-
resetHandlers(...handlers: Array<unknown>): void;
|
|
7
|
-
activate(options: ResolvedActivateOptions): Promise<void>;
|
|
8
|
-
deactivate(): void;
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=browser-adapter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"browser-adapter.d.ts","sourceRoot":"","sources":["../src/browser-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,uBAAuB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAEpF,qBAAa,iBAAkB,YAAW,UAAU;IAClD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;gBAE7B,MAAM,EAAE,eAAe;IAInC,GAAG,CAAC,GAAG,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,GAAG,IAAI;IAItC,aAAa,CAAC,GAAG,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,GAAG,IAAI;IAI1C,QAAQ,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;IAM/D,UAAU,IAAI,IAAI;CAGnB"}
|
package/dist/browser-adapter.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export class BrowserMswAdapter {
|
|
2
|
-
worker;
|
|
3
|
-
constructor(worker) {
|
|
4
|
-
this.worker = worker;
|
|
5
|
-
}
|
|
6
|
-
use(...handlers) {
|
|
7
|
-
this.worker.use(...handlers);
|
|
8
|
-
}
|
|
9
|
-
resetHandlers(...handlers) {
|
|
10
|
-
this.worker.resetHandlers(...handlers);
|
|
11
|
-
}
|
|
12
|
-
async activate(options) {
|
|
13
|
-
await this.worker.start({
|
|
14
|
-
onUnhandledRequest: options.onUnhandledRequest,
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
deactivate() {
|
|
18
|
-
this.worker.stop();
|
|
19
|
-
}
|
|
20
|
-
}
|
package/dist/browser.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,wBAAgB,eAAe,CAAC,MAAM,EAAE,eAAe,GAAG,SAAS,CAElE;AAED,YAAY,EACV,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,QAAQ,EACR,eAAe,EACf,cAAc,EACd,YAAY,EACZ,kBAAkB,EAClB,UAAU,EACV,eAAe,GAChB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC1E,YAAY,EAAE,sBAAsB,EAAE,yBAAyB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/fetch-mock.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { MockCallHistory } from './mock-call-history';
|
|
2
|
-
import type { MockPool, PendingInterceptor, ActivateOptions, MswAdapter, SetupServerLike, SetupWorkerLike } from './types';
|
|
3
|
-
export type { InterceptOptions, ReplyOptions, MockReplyChain, MockInterceptor, MockPool, PendingInterceptor, OnUnhandledRequest, ActivateOptions, } from './types';
|
|
4
|
-
export declare class FetchMock {
|
|
5
|
-
/** @internal */
|
|
6
|
-
static _defaultAdapterFactory?: () => MswAdapter;
|
|
7
|
-
private readonly _calls;
|
|
8
|
-
private adapter;
|
|
9
|
-
private interceptors;
|
|
10
|
-
private netConnectAllowed;
|
|
11
|
-
private mswHandlers;
|
|
12
|
-
get calls(): MockCallHistory;
|
|
13
|
-
constructor(input?: SetupServerLike | SetupWorkerLike | MswAdapter);
|
|
14
|
-
activate(options?: ActivateOptions): Promise<void>;
|
|
15
|
-
disableNetConnect(): void;
|
|
16
|
-
enableNetConnect(matcher?: string | RegExp | ((host: string) => boolean)): void;
|
|
17
|
-
private isNetConnectAllowed;
|
|
18
|
-
/**
|
|
19
|
-
* Remove consumed MSW handlers so future requests to those URLs
|
|
20
|
-
* go through MSW's onUnhandledRequest instead of silently passing through.
|
|
21
|
-
*/
|
|
22
|
-
private syncMswHandlers;
|
|
23
|
-
getCallHistory(): MockCallHistory;
|
|
24
|
-
clearCallHistory(): void;
|
|
25
|
-
deactivate(): void;
|
|
26
|
-
reset(): void;
|
|
27
|
-
assertNoPendingInterceptors(): void;
|
|
28
|
-
pendingInterceptors(): PendingInterceptor[];
|
|
29
|
-
get(origin: string): MockPool;
|
|
30
|
-
}
|
|
31
|
-
//# sourceMappingURL=fetch-mock.d.ts.map
|
package/dist/fetch-mock.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fetch-mock.d.ts","sourceRoot":"","sources":["../src/fetch-mock.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAUV,QAAQ,EACR,kBAAkB,EAElB,eAAe,EACf,UAAU,EAEV,eAAe,EACf,eAAe,EAChB,MAAM,SAAS,CAAC;AAEjB,YAAY,EACV,gBAAgB,EAChB,YAAY,EACZ,cAAc,EACd,eAAe,EACf,QAAQ,EACR,kBAAkB,EAClB,kBAAkB,EAClB,eAAe,GAChB,MAAM,SAAS,CAAC;AA2KjB,qBAAa,SAAS;IACpB,gBAAgB;IAChB,MAAM,CAAC,sBAAsB,CAAC,EAAE,MAAM,UAAU,CAAC;IAEjD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyB;IAChD,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,YAAY,CAA4B;IAChD,OAAO,CAAC,iBAAiB,CAA4B;IAErD,OAAO,CAAC,WAAW,CAA2C;IAE9D,IAAI,KAAK,IAAI,eAAe,CAE3B;gBAEW,KAAK,CAAC,EAAE,eAAe,GAAG,eAAe,GAAG,UAAU;IAI5D,QAAQ,CAAC,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBxD,iBAAiB,IAAI,IAAI;IAIzB,gBAAgB,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,IAAI;IAI/E,OAAO,CAAC,mBAAmB;IAS3B;;;OAGG;IACH,OAAO,CAAC,eAAe;IAOvB,cAAc,IAAI,eAAe;IAIjC,gBAAgB,IAAI,IAAI;IAIxB,UAAU,IAAI,IAAI;IAOlB,KAAK,IAAI,IAAI;IAOb,2BAA2B,IAAI,IAAI;IAQnC,mBAAmB,IAAI,kBAAkB,EAAE;IAI3C,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ;CAqH9B"}
|