@r0hitsharma/http-client-msw 0.12.0-rohit-fork-ci.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 +251 -0
- package/dist/base-url.d.ts +46 -0
- package/dist/base-url.js +65 -0
- package/dist/browser.d.ts +50 -0
- package/dist/browser.js +59 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +20 -0
- package/dist/mock-api.d.ts +75 -0
- package/dist/mock-api.js +35 -0
- package/dist/mock-delay.d.ts +35 -0
- package/dist/mock-delay.js +50 -0
- package/dist/mock-store.d.ts +76 -0
- package/dist/mock-store.js +114 -0
- package/dist/node.d.ts +41 -0
- package/dist/node.js +41 -0
- package/dist/seeded-rng.d.ts +27 -0
- package/dist/seeded-rng.js +52 -0
- package/dist/setup.d.ts +54 -0
- package/dist/setup.js +26 -0
- package/dist/worker-options.d.ts +55 -0
- package/dist/worker-options.js +49 -0
- package/package.json +59 -0
- package/src/base-url.test.ts +177 -0
- package/src/base-url.ts +80 -0
- package/src/browser.ts +88 -0
- package/src/index.ts +53 -0
- package/src/mock-api.test.ts +148 -0
- package/src/mock-api.ts +96 -0
- package/src/mock-api.types.test.ts +119 -0
- package/src/mock-delay.test.ts +51 -0
- package/src/mock-delay.ts +68 -0
- package/src/mock-store.test.ts +263 -0
- package/src/mock-store.ts +180 -0
- package/src/node.ts +61 -0
- package/src/seeded-rng.test.ts +106 -0
- package/src/seeded-rng.ts +77 -0
- package/src/setup.test.ts +137 -0
- package/src/setup.ts +71 -0
- package/src/test-fixtures.ts +113 -0
- package/src/worker-options.test.ts +128 -0
- package/src/worker-options.ts +108 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { StartOptions } from 'msw/browser';
|
|
2
|
+
|
|
3
|
+
import { resolveWorkerScriptUrl } from './base-url.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The browser entry's decisions, factored out of `browser.ts` so they are
|
|
7
|
+
* testable without a service worker: a vitest run cannot register one, but it
|
|
8
|
+
* can assert the options that would be passed and the start-once behaviour.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export type MockWorkerOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* The app's **public base path** — `import.meta.env.BASE_URL` under Vite — used
|
|
14
|
+
* to locate the worker script for a subpath deployment. This is not the API
|
|
15
|
+
* base from `createMockApi`: the script is served by the app, the API is
|
|
16
|
+
* whatever the app talks to.
|
|
17
|
+
*
|
|
18
|
+
* @default '/'
|
|
19
|
+
*/
|
|
20
|
+
baseUrl?: string;
|
|
21
|
+
/**
|
|
22
|
+
* What msw does with a request no handler matched.
|
|
23
|
+
*
|
|
24
|
+
* Defaults to `'bypass'`, unlike msw's own `'warn'`: an app running against
|
|
25
|
+
* mocks still loads its own documents, modules, images, and fonts, and warning
|
|
26
|
+
* on every one of them buries the requests a developer is actually looking at.
|
|
27
|
+
* Use `'warn'` while filling gaps in a mock set.
|
|
28
|
+
*/
|
|
29
|
+
onUnhandledRequest?: StartOptions['onUnhandledRequest'];
|
|
30
|
+
/**
|
|
31
|
+
* Suppress msw's per-request console logging. Defaults to `true` — the request
|
|
32
|
+
* log is long enough to hide application logs, and the network panel already
|
|
33
|
+
* shows the same traffic.
|
|
34
|
+
*/
|
|
35
|
+
quiet?: boolean;
|
|
36
|
+
/** Escape hatch merged last into msw's own `worker.start()` options. */
|
|
37
|
+
start?: StartOptions;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export function buildWorkerStartOptions(
|
|
41
|
+
options: MockWorkerOptions = {},
|
|
42
|
+
): StartOptions {
|
|
43
|
+
const { baseUrl, onUnhandledRequest, quiet, start } = options;
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
...start,
|
|
47
|
+
// Resolved after the spread rather than defaulted before it: an options
|
|
48
|
+
// object forwarded with explicit `undefined` values would otherwise put
|
|
49
|
+
// those keys back and hand msw its own defaults instead of ours. `start`
|
|
50
|
+
// still wins over the named options where it sets a value.
|
|
51
|
+
onUnhandledRequest:
|
|
52
|
+
start?.onUnhandledRequest ?? onUnhandledRequest ?? 'bypass',
|
|
53
|
+
quiet: start?.quiet ?? quiet ?? true,
|
|
54
|
+
serviceWorker: {
|
|
55
|
+
...start?.serviceWorker,
|
|
56
|
+
// The same hole one level down: a `serviceWorker` object populated from
|
|
57
|
+
// another options bag carries an explicit `url: undefined`, which spread
|
|
58
|
+
// over the resolved path would erase it and send msw to its own default
|
|
59
|
+
// location — the wrong one for a subpath deployment.
|
|
60
|
+
url: start?.serviceWorker?.url ?? resolveWorkerScriptUrl(baseUrl),
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type IdempotentStart = {
|
|
66
|
+
/** Runs the wrapped function at most once, until `invalidate()`. */
|
|
67
|
+
start: () => Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Forgets a completed start so the next `start()` runs again. Whatever the
|
|
70
|
+
* start registered has to be torn down first — a memo pointing at a stopped
|
|
71
|
+
* worker resolves immediately and intercepts nothing.
|
|
72
|
+
*/
|
|
73
|
+
invalidate: () => void;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Wraps a start function so concurrent and repeated calls share one
|
|
78
|
+
* registration. An app entry, a hot reload, and a Playwright fixture can all
|
|
79
|
+
* call `start()`; registering the worker again mid-session would reset its
|
|
80
|
+
* handler list and drop any runtime overrides a test had installed.
|
|
81
|
+
*
|
|
82
|
+
* A rejected start invalidates itself, so a caller can retry after fixing
|
|
83
|
+
* whatever failed (a missing `mockServiceWorker.js`, most often).
|
|
84
|
+
*/
|
|
85
|
+
export function createIdempotentStart(
|
|
86
|
+
start: () => Promise<void>,
|
|
87
|
+
): IdempotentStart {
|
|
88
|
+
let pending: Promise<void> | undefined;
|
|
89
|
+
|
|
90
|
+
const invalidate = (): void => {
|
|
91
|
+
pending = undefined;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
start: () => {
|
|
96
|
+
// Called through an async wrapper so a synchronous throw from `start`
|
|
97
|
+
// rejects the returned promise instead of escaping as an exception the
|
|
98
|
+
// caller has no way to `.catch`.
|
|
99
|
+
pending ??= (async () => await start())().catch((error: unknown) => {
|
|
100
|
+
invalidate();
|
|
101
|
+
throw error;
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return pending;
|
|
105
|
+
},
|
|
106
|
+
invalidate,
|
|
107
|
+
};
|
|
108
|
+
}
|