@vitest/browser 2.1.0-beta.5 → 2.1.0-beta.7

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.
@@ -0,0 +1,317 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ (function polyfill() {
5
+ const relList = document.createElement("link").relList;
6
+ if (relList && relList.supports && relList.supports("modulepreload")) {
7
+ return;
8
+ }
9
+ for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
10
+ processPreload(link);
11
+ }
12
+ new MutationObserver((mutations) => {
13
+ for (const mutation of mutations) {
14
+ if (mutation.type !== "childList") {
15
+ continue;
16
+ }
17
+ for (const node of mutation.addedNodes) {
18
+ if (node.tagName === "LINK" && node.rel === "modulepreload")
19
+ processPreload(node);
20
+ }
21
+ }
22
+ }).observe(document, { childList: true, subtree: true });
23
+ function getFetchOpts(link) {
24
+ const fetchOpts = {};
25
+ if (link.integrity) fetchOpts.integrity = link.integrity;
26
+ if (link.referrerPolicy) fetchOpts.referrerPolicy = link.referrerPolicy;
27
+ if (link.crossOrigin === "use-credentials")
28
+ fetchOpts.credentials = "include";
29
+ else if (link.crossOrigin === "anonymous") fetchOpts.credentials = "omit";
30
+ else fetchOpts.credentials = "same-origin";
31
+ return fetchOpts;
32
+ }
33
+ function processPreload(link) {
34
+ if (link.ep)
35
+ return;
36
+ link.ep = true;
37
+ const fetchOpts = getFetchOpts(link);
38
+ fetch(link.href, fetchOpts);
39
+ }
40
+ })();
41
+ async function importId(id) {
42
+ const name = `/@id/${id}`.replace(/\\/g, "/");
43
+ return (/* @__PURE__ */ getBrowserState()).wrapModule(() => import(
44
+ /* @vite-ignore */
45
+ name
46
+ ));
47
+ }
48
+ async function importFs(id) {
49
+ const name = `/@fs/${id}`.replace(/\\/g, "/");
50
+ return (/* @__PURE__ */ getBrowserState()).wrapModule(() => import(
51
+ /* @vite-ignore */
52
+ name
53
+ ));
54
+ }
55
+ const executor = {
56
+ isBrowser: true,
57
+ executeId: (id) => {
58
+ if (id[0] === "/" || id[1] === ":") {
59
+ return importFs(id);
60
+ }
61
+ return importId(id);
62
+ }
63
+ };
64
+ function getConfig() {
65
+ return (/* @__PURE__ */ getBrowserState()).config;
66
+ }
67
+ // @__NO_SIDE_EFFECTS__
68
+ function getBrowserState() {
69
+ return window.__vitest_browser_runner__;
70
+ }
71
+ // @__NO_SIDE_EFFECTS__
72
+ function getWorkerState() {
73
+ const state = window.__vitest_worker__;
74
+ if (!state) {
75
+ throw new Error("Worker state is not found. This is an issue with Vitest. Please, open an issue.");
76
+ }
77
+ return state;
78
+ }
79
+ class MockerRegistry {
80
+ constructor() {
81
+ __publicField(this, "registry", /* @__PURE__ */ new Map());
82
+ }
83
+ clear() {
84
+ this.registry.clear();
85
+ }
86
+ keys() {
87
+ return this.registry.keys();
88
+ }
89
+ add(mock) {
90
+ this.registry.set(mock.url, mock);
91
+ }
92
+ register(typeOrEvent, raw, url, factoryOrRedirect) {
93
+ const type = typeof typeOrEvent === "object" ? typeOrEvent.type : typeOrEvent;
94
+ if (typeof typeOrEvent === "object") {
95
+ const event = typeOrEvent;
96
+ if (event instanceof AutomockedModule || event instanceof AutospiedModule || event instanceof ManualMockedModule || event instanceof RedirectedModule) {
97
+ throw new TypeError(
98
+ `[vitest] Cannot register a mock that is already defined. Expected a JSON representation from \`MockedModule.toJSON\`, instead got "${event.type}". Use "registry.add()" to update a mock instead.`
99
+ );
100
+ }
101
+ if (event.type === "automock") {
102
+ const module = AutomockedModule.fromJSON(event);
103
+ this.add(module);
104
+ return module;
105
+ } else if (event.type === "autospy") {
106
+ const module = AutospiedModule.fromJSON(event);
107
+ this.add(module);
108
+ return module;
109
+ } else if (event.type === "redirect") {
110
+ const module = RedirectedModule.fromJSON(event);
111
+ this.add(module);
112
+ return module;
113
+ } else if (event.type === "manual") {
114
+ throw new Error(`Cannot set serialized manual mock. Define a factory function manually with \`ManualMockedModule.fromJSON()\`.`);
115
+ } else {
116
+ throw new Error(`Unknown mock type: ${event.type}`);
117
+ }
118
+ }
119
+ if (typeof raw !== "string") {
120
+ throw new TypeError("[vitest] Mocks require a raw string.");
121
+ }
122
+ if (typeof url !== "string") {
123
+ throw new TypeError("[vitest] Mocks require a url string.");
124
+ }
125
+ if (type === "manual") {
126
+ if (typeof factoryOrRedirect !== "function") {
127
+ throw new TypeError("[vitest] Manual mocks require a factory function.");
128
+ }
129
+ const mock = new ManualMockedModule(raw, url, factoryOrRedirect);
130
+ this.add(mock);
131
+ return mock;
132
+ } else if (type === "automock" || type === "autospy") {
133
+ const mock = type === "automock" ? new AutomockedModule(raw, url) : new AutospiedModule(raw, url);
134
+ this.add(mock);
135
+ return mock;
136
+ } else if (type === "redirect") {
137
+ if (typeof factoryOrRedirect !== "string") {
138
+ throw new TypeError("[vitest] Redirect mocks require a redirect string.");
139
+ }
140
+ const mock = new RedirectedModule(raw, url, factoryOrRedirect);
141
+ this.add(mock);
142
+ return mock;
143
+ } else {
144
+ throw new Error(`[vitest] Unknown mock type: ${type}`);
145
+ }
146
+ }
147
+ delete(id) {
148
+ this.registry.delete(id);
149
+ }
150
+ get(id) {
151
+ return this.registry.get(id);
152
+ }
153
+ has(id) {
154
+ return this.registry.has(id);
155
+ }
156
+ }
157
+ class AutomockedModule {
158
+ constructor(raw, url) {
159
+ __publicField(this, "type", "automock");
160
+ this.raw = raw;
161
+ this.url = url;
162
+ }
163
+ static fromJSON(data) {
164
+ return new AutospiedModule(data.raw, data.url);
165
+ }
166
+ toJSON() {
167
+ return {
168
+ type: this.type,
169
+ url: this.url,
170
+ raw: this.raw
171
+ };
172
+ }
173
+ }
174
+ class AutospiedModule {
175
+ constructor(raw, url) {
176
+ __publicField(this, "type", "autospy");
177
+ this.raw = raw;
178
+ this.url = url;
179
+ }
180
+ static fromJSON(data) {
181
+ return new AutospiedModule(data.raw, data.url);
182
+ }
183
+ toJSON() {
184
+ return {
185
+ type: this.type,
186
+ url: this.url,
187
+ raw: this.raw
188
+ };
189
+ }
190
+ }
191
+ class RedirectedModule {
192
+ constructor(raw, url, redirect) {
193
+ __publicField(this, "type", "redirect");
194
+ this.raw = raw;
195
+ this.url = url;
196
+ this.redirect = redirect;
197
+ }
198
+ static fromJSON(data) {
199
+ return new RedirectedModule(data.raw, data.url, data.redirect);
200
+ }
201
+ toJSON() {
202
+ return {
203
+ type: this.type,
204
+ url: this.url,
205
+ raw: this.raw,
206
+ redirect: this.redirect
207
+ };
208
+ }
209
+ }
210
+ class ManualMockedModule {
211
+ constructor(raw, url, factory) {
212
+ __publicField(this, "cache");
213
+ __publicField(this, "type", "manual");
214
+ this.raw = raw;
215
+ this.url = url;
216
+ this.factory = factory;
217
+ }
218
+ async resolve() {
219
+ if (this.cache) {
220
+ return this.cache;
221
+ }
222
+ let exports;
223
+ try {
224
+ exports = await this.factory();
225
+ } catch (err) {
226
+ const vitestError = new Error(
227
+ '[vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock'
228
+ );
229
+ vitestError.cause = err;
230
+ throw vitestError;
231
+ }
232
+ if (exports === null || typeof exports !== "object" || Array.isArray(exports)) {
233
+ throw new TypeError(
234
+ `[vitest] vi.mock("${this.raw}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`
235
+ );
236
+ }
237
+ return this.cache = exports;
238
+ }
239
+ static fromJSON(data, factory) {
240
+ return new ManualMockedModule(data.raw, data.url, factory);
241
+ }
242
+ toJSON() {
243
+ return {
244
+ type: this.type,
245
+ url: this.url,
246
+ raw: this.raw
247
+ };
248
+ }
249
+ }
250
+ const scriptRel = "modulepreload";
251
+ const assetsURL = function(dep) {
252
+ return "/" + dep;
253
+ };
254
+ const seen = {};
255
+ const __vitePreload = function preload(baseModule, deps, importerUrl) {
256
+ let promise = Promise.resolve();
257
+ if (deps && deps.length > 0) {
258
+ document.getElementsByTagName("link");
259
+ const cspNonceMeta = document.querySelector(
260
+ "meta[property=csp-nonce]"
261
+ );
262
+ const cspNonce = (cspNonceMeta == null ? void 0 : cspNonceMeta.nonce) || (cspNonceMeta == null ? void 0 : cspNonceMeta.getAttribute("nonce"));
263
+ promise = Promise.all(
264
+ deps.map((dep) => {
265
+ dep = assetsURL(dep);
266
+ if (dep in seen) return;
267
+ seen[dep] = true;
268
+ const isCss = dep.endsWith(".css");
269
+ const cssSelector = isCss ? '[rel="stylesheet"]' : "";
270
+ if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
271
+ return;
272
+ }
273
+ const link = document.createElement("link");
274
+ link.rel = isCss ? "stylesheet" : scriptRel;
275
+ if (!isCss) {
276
+ link.as = "script";
277
+ link.crossOrigin = "";
278
+ }
279
+ link.href = dep;
280
+ if (cspNonce) {
281
+ link.setAttribute("nonce", cspNonce);
282
+ }
283
+ document.head.appendChild(link);
284
+ if (isCss) {
285
+ return new Promise((res, rej) => {
286
+ link.addEventListener("load", res);
287
+ link.addEventListener(
288
+ "error",
289
+ () => rej(new Error(`Unable to preload CSS for ${dep}`))
290
+ );
291
+ });
292
+ }
293
+ })
294
+ );
295
+ }
296
+ return promise.then(() => baseModule()).catch((err) => {
297
+ const e = new Event("vite:preloadError", {
298
+ cancelable: true
299
+ });
300
+ e.payload = err;
301
+ window.dispatchEvent(e);
302
+ if (!e.defaultPrevented) {
303
+ throw err;
304
+ }
305
+ });
306
+ };
307
+ export {
308
+ AutomockedModule as A,
309
+ MockerRegistry as M,
310
+ RedirectedModule as R,
311
+ __vitePreload as _,
312
+ ManualMockedModule as a,
313
+ getConfig as b,
314
+ getWorkerState as c,
315
+ executor as e,
316
+ getBrowserState as g
317
+ };