vitest 3.2.0-beta.3 → 3.2.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/LICENSE.md +29 -0
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +2 -2
- package/dist/chunks/{base.D4119yLM.js → base.Cg0miDlQ.js} +10 -14
- package/dist/chunks/{benchmark.Cf_PACH1.js → benchmark.CYdenmiT.js} +4 -6
- package/dist/chunks/{cac.DWaWHIIE.js → cac.C8BzMmTW.js} +66 -136
- package/dist/chunks/{cli-api.CnmEXkxs.js → cli-api.DmupRhea.js} +1251 -1336
- package/dist/chunks/{console.Cwr-MFPV.js → console.CtFJOzRO.js} +24 -45
- package/dist/chunks/{coverage.0iPg4Wrz.js → coverage.DVF1vEu8.js} +4 -12
- package/dist/chunks/{coverage.C73DaDgS.js → coverage.EIiagJJP.js} +484 -1003
- package/dist/chunks/{creator.C8WKy2eW.js → creator.GK6I-cL4.js} +29 -76
- package/dist/chunks/{date.ByMsSlOr.js → date.Bq6ZW5rf.js} +3 -8
- package/dist/chunks/{defaults.DpVH7vbg.js → defaults.B7q_naMc.js} +1 -1
- package/dist/chunks/{env.Dq0hM4Xv.js → env.D4Lgay0q.js} +1 -1
- package/dist/chunks/{execute.B3q-2LPV.js → execute.B7h3T_Hc.js} +104 -220
- package/dist/chunks/{git.DXfdBEfR.js → git.BVQ8w_Sw.js} +1 -3
- package/dist/chunks/{global.d.BNLIi6yo.d.ts → global.d.MAmajcmJ.d.ts} +2 -0
- package/dist/chunks/{globals.CI21aWXF.js → globals.DEHgCU4V.js} +5 -5
- package/dist/chunks/{index.Bter3jj9.js → index.BZ0g1JD2.js} +366 -628
- package/dist/chunks/{index.CbT4iuwc.js → index.BbB8_kAK.js} +22 -24
- package/dist/chunks/{index.JOzufsrU.js → index.CIyJn3t1.js} +37 -82
- package/dist/chunks/{index.DNgLEKsQ.js → index.CdQS2e2Q.js} +2 -2
- package/dist/chunks/{index.2jgTs_Q5.js → index.CmSc2RE5.js} +69 -107
- package/dist/chunks/{inspector.BFsh5KO0.js → inspector.C914Efll.js} +1 -1
- package/dist/chunks/{node.Be-ntJnD.js → node.fjCdwEIl.js} +1 -1
- package/dist/chunks/{reporters.d.Bt4IGtsa.d.ts → reporters.d.C1ogPriE.d.ts} +24 -4
- package/dist/chunks/{rpc.BKExFSRG.js → rpc.Iovn4oWe.js} +9 -19
- package/dist/chunks/{runBaseTests.B_M1TTsK.js → runBaseTests.Dd85QTll.js} +18 -31
- package/dist/chunks/{setup-common.CF-O-dZX.js → setup-common.Dd054P77.js} +15 -42
- package/dist/chunks/{typechecker.BgzF-6iO.js → typechecker.DRKU1-1g.js} +106 -186
- package/dist/chunks/{utils.DPCq3gzW.js → utils.CAioKnHs.js} +6 -14
- package/dist/chunks/{utils.BlI4TC7Y.js → utils.XdZDrNZV.js} +5 -13
- package/dist/chunks/{vi.pkoYCV6A.js → vi.bdSIJ99Y.js} +118 -267
- package/dist/chunks/{vite.d.B-Kx3KCF.d.ts → vite.d.DqE4-hhK.d.ts} +1 -1
- package/dist/chunks/{vm.DPYem2so.js → vm.BThCzidc.js} +98 -214
- package/dist/chunks/{worker.d.Bl1O4kuf.d.ts → worker.d.DvqK5Vmu.d.ts} +1 -1
- package/dist/chunks/{worker.d.BKbBp2ga.d.ts → worker.d.tQu2eJQy.d.ts} +3 -1
- package/dist/cli.js +4 -4
- package/dist/config.cjs +1 -1
- package/dist/config.d.ts +4 -4
- package/dist/config.js +2 -2
- package/dist/coverage.d.ts +2 -2
- package/dist/coverage.js +5 -5
- package/dist/environments.js +1 -1
- package/dist/execute.d.ts +1 -1
- package/dist/execute.js +1 -1
- package/dist/index.d.ts +12 -11
- package/dist/index.js +5 -5
- package/dist/node.d.ts +7 -7
- package/dist/node.js +12 -14
- package/dist/reporters.d.ts +2 -2
- package/dist/reporters.js +4 -4
- package/dist/runners.d.ts +5 -2
- package/dist/runners.js +51 -80
- package/dist/snapshot.js +2 -2
- package/dist/suite.js +2 -2
- package/dist/worker.js +36 -42
- package/dist/workers/forks.js +4 -4
- package/dist/workers/runVmTests.js +15 -21
- package/dist/workers/threads.js +4 -4
- package/dist/workers/vmForks.js +6 -6
- package/dist/workers/vmThreads.js +6 -6
- package/dist/workers.d.ts +2 -2
- package/dist/workers.js +10 -10
- package/package.json +16 -14
|
@@ -13,9 +13,7 @@ import { highlight } from '@vitest/utils';
|
|
|
13
13
|
|
|
14
14
|
const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
15
15
|
function normalizeWindowsPath(input = "") {
|
|
16
|
-
if (!input)
|
|
17
|
-
return input;
|
|
18
|
-
}
|
|
16
|
+
if (!input) return input;
|
|
19
17
|
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
20
18
|
}
|
|
21
19
|
const _UNC_REGEX = /^[/\\]{2}/;
|
|
@@ -23,30 +21,20 @@ const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
|
23
21
|
const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
|
|
24
22
|
const _EXTNAME_RE = /.(\.[^./]+|\.)$/;
|
|
25
23
|
const normalize = function(path) {
|
|
26
|
-
if (path.length === 0)
|
|
27
|
-
return ".";
|
|
28
|
-
}
|
|
24
|
+
if (path.length === 0) return ".";
|
|
29
25
|
path = normalizeWindowsPath(path);
|
|
30
26
|
const isUNCPath = path.match(_UNC_REGEX);
|
|
31
27
|
const isPathAbsolute = isAbsolute(path);
|
|
32
28
|
const trailingSeparator = path[path.length - 1] === "/";
|
|
33
29
|
path = normalizeString(path, !isPathAbsolute);
|
|
34
30
|
if (path.length === 0) {
|
|
35
|
-
if (isPathAbsolute)
|
|
36
|
-
return "/";
|
|
37
|
-
}
|
|
31
|
+
if (isPathAbsolute) return "/";
|
|
38
32
|
return trailingSeparator ? "./" : ".";
|
|
39
33
|
}
|
|
40
|
-
if (trailingSeparator)
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
if (_DRIVE_LETTER_RE.test(path)) {
|
|
44
|
-
path += "/";
|
|
45
|
-
}
|
|
34
|
+
if (trailingSeparator) path += "/";
|
|
35
|
+
if (_DRIVE_LETTER_RE.test(path)) path += "/";
|
|
46
36
|
if (isUNCPath) {
|
|
47
|
-
if (!isPathAbsolute) {
|
|
48
|
-
return `//./${path}`;
|
|
49
|
-
}
|
|
37
|
+
if (!isPathAbsolute) return `//./${path}`;
|
|
50
38
|
return `//${path}`;
|
|
51
39
|
}
|
|
52
40
|
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
|
|
@@ -54,28 +42,19 @@ const normalize = function(path) {
|
|
|
54
42
|
const join = function(...segments) {
|
|
55
43
|
let path = "";
|
|
56
44
|
for (const seg of segments) {
|
|
57
|
-
if (!seg)
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
45
|
+
if (!seg) continue;
|
|
60
46
|
if (path.length > 0) {
|
|
61
47
|
const pathTrailing = path[path.length - 1] === "/";
|
|
62
48
|
const segLeading = seg[0] === "/";
|
|
63
49
|
const both = pathTrailing && segLeading;
|
|
64
|
-
if (both)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
path += pathTrailing || segLeading ? seg : `/${seg}`;
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
path += seg;
|
|
71
|
-
}
|
|
50
|
+
if (both) path += seg.slice(1);
|
|
51
|
+
else path += pathTrailing || segLeading ? seg : `/${seg}`;
|
|
52
|
+
} else path += seg;
|
|
72
53
|
}
|
|
73
54
|
return normalize(path);
|
|
74
55
|
};
|
|
75
56
|
function cwd() {
|
|
76
|
-
if (typeof process !== "undefined" && typeof process.cwd === "function")
|
|
77
|
-
return process.cwd().replace(/\\/g, "/");
|
|
78
|
-
}
|
|
57
|
+
if (typeof process !== "undefined" && typeof process.cwd === "function") return process.cwd().replace(/\\/g, "/");
|
|
79
58
|
return "/";
|
|
80
59
|
}
|
|
81
60
|
const resolve = function(...arguments_) {
|
|
@@ -84,16 +63,12 @@ const resolve = function(...arguments_) {
|
|
|
84
63
|
let resolvedAbsolute = false;
|
|
85
64
|
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
86
65
|
const path = index >= 0 ? arguments_[index] : cwd();
|
|
87
|
-
if (!path || path.length === 0)
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
66
|
+
if (!path || path.length === 0) continue;
|
|
90
67
|
resolvedPath = `${path}/${resolvedPath}`;
|
|
91
68
|
resolvedAbsolute = isAbsolute(path);
|
|
92
69
|
}
|
|
93
70
|
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
94
|
-
if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
|
|
95
|
-
return `/${resolvedPath}`;
|
|
96
|
-
}
|
|
71
|
+
if (resolvedAbsolute && !isAbsolute(resolvedPath)) return `/${resolvedPath}`;
|
|
97
72
|
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
98
73
|
};
|
|
99
74
|
function normalizeString(path, allowAboveRoot) {
|
|
@@ -103,13 +78,9 @@ function normalizeString(path, allowAboveRoot) {
|
|
|
103
78
|
let dots = 0;
|
|
104
79
|
let char = null;
|
|
105
80
|
for (let index = 0; index <= path.length; ++index) {
|
|
106
|
-
if (index < path.length)
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
break;
|
|
110
|
-
} else {
|
|
111
|
-
char = "/";
|
|
112
|
-
}
|
|
81
|
+
if (index < path.length) char = path[index];
|
|
82
|
+
else if (char === "/") break;
|
|
83
|
+
else char = "/";
|
|
113
84
|
if (char === "/") {
|
|
114
85
|
if (lastSlash === index - 1 || dots === 1);
|
|
115
86
|
else if (dots === 2) {
|
|
@@ -139,20 +110,14 @@ function normalizeString(path, allowAboveRoot) {
|
|
|
139
110
|
lastSegmentLength = 2;
|
|
140
111
|
}
|
|
141
112
|
} else {
|
|
142
|
-
if (res.length > 0) {
|
|
143
|
-
|
|
144
|
-
} else {
|
|
145
|
-
res = path.slice(lastSlash + 1, index);
|
|
146
|
-
}
|
|
113
|
+
if (res.length > 0) res += `/${path.slice(lastSlash + 1, index)}`;
|
|
114
|
+
else res = path.slice(lastSlash + 1, index);
|
|
147
115
|
lastSegmentLength = index - lastSlash - 1;
|
|
148
116
|
}
|
|
149
117
|
lastSlash = index;
|
|
150
118
|
dots = 0;
|
|
151
|
-
} else if (char === "." && dots !== -1)
|
|
152
|
-
|
|
153
|
-
} else {
|
|
154
|
-
dots = -1;
|
|
155
|
-
}
|
|
119
|
+
} else if (char === "." && dots !== -1) ++dots;
|
|
120
|
+
else dots = -1;
|
|
156
121
|
}
|
|
157
122
|
return res;
|
|
158
123
|
}
|
|
@@ -166,9 +131,7 @@ const extname = function(p) {
|
|
|
166
131
|
};
|
|
167
132
|
const dirname = function(p) {
|
|
168
133
|
const segments = normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1);
|
|
169
|
-
if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0]))
|
|
170
|
-
segments[0] += "/";
|
|
171
|
-
}
|
|
134
|
+
if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0])) segments[0] += "/";
|
|
172
135
|
return segments.join("/") || (isAbsolute(p) ? "/" : ".");
|
|
173
136
|
};
|
|
174
137
|
const basename = function(p, extension) {
|
|
@@ -192,9 +155,7 @@ function findMockRedirect(root, mockPath, external) {
|
|
|
192
155
|
if (external || isNodeBuiltin(mockPath) || !existsSync(mockPath)) {
|
|
193
156
|
const mockDirname = dirname(path);
|
|
194
157
|
const mockFolder = join(root, "__mocks__", mockDirname);
|
|
195
|
-
if (!existsSync(mockFolder))
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
158
|
+
if (!existsSync(mockFolder)) return null;
|
|
198
159
|
const baseOriginal = basename(path);
|
|
199
160
|
function findFile(mockFolder, baseOriginal) {
|
|
200
161
|
const files = readdirSync(mockFolder);
|
|
@@ -203,14 +164,11 @@ function findMockRedirect(root, mockPath, external) {
|
|
|
203
164
|
if (baseFile === baseOriginal) {
|
|
204
165
|
const path = resolve(mockFolder, file);
|
|
205
166
|
// if the same name, return the file
|
|
206
|
-
if (statSync(path).isFile())
|
|
207
|
-
|
|
208
|
-
} else {
|
|
167
|
+
if (statSync(path).isFile()) return path;
|
|
168
|
+
else {
|
|
209
169
|
// find folder/index.{js,ts}
|
|
210
170
|
const indexFile = findFile(path, "index");
|
|
211
|
-
if (indexFile)
|
|
212
|
-
return indexFile;
|
|
213
|
-
}
|
|
171
|
+
if (indexFile) return indexFile;
|
|
214
172
|
}
|
|
215
173
|
}
|
|
216
174
|
}
|
|
@@ -248,9 +206,7 @@ const prefixedBuiltins = new Set([
|
|
|
248
206
|
]);
|
|
249
207
|
const NODE_BUILTIN_NAMESPACE = "node:";
|
|
250
208
|
function isNodeBuiltin(id) {
|
|
251
|
-
if (prefixedBuiltins.has(id))
|
|
252
|
-
return true;
|
|
253
|
-
}
|
|
209
|
+
if (prefixedBuiltins.has(id)) return true;
|
|
254
210
|
return builtins.has(id.startsWith(NODE_BUILTIN_NAMESPACE) ? id.slice(NODE_BUILTIN_NAMESPACE.length) : id);
|
|
255
211
|
}
|
|
256
212
|
|
|
@@ -260,24 +216,21 @@ class VitestMocker {
|
|
|
260
216
|
spyModule;
|
|
261
217
|
primitives;
|
|
262
218
|
filterPublicKeys;
|
|
263
|
-
registries = new Map();
|
|
219
|
+
registries = /* @__PURE__ */ new Map();
|
|
264
220
|
mockContext = { callstack: null };
|
|
265
221
|
constructor(executor) {
|
|
266
222
|
this.executor = executor;
|
|
267
223
|
const context = this.executor.options.context;
|
|
268
|
-
if (context) {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
Map
|
|
279
|
-
};
|
|
280
|
-
}
|
|
224
|
+
if (context) this.primitives = vm.runInContext("({ Object, Error, Function, RegExp, Symbol, Array, Map })", context);
|
|
225
|
+
else this.primitives = {
|
|
226
|
+
Object,
|
|
227
|
+
Error,
|
|
228
|
+
Function,
|
|
229
|
+
RegExp,
|
|
230
|
+
Symbol: globalThis.Symbol,
|
|
231
|
+
Array,
|
|
232
|
+
Map
|
|
233
|
+
};
|
|
281
234
|
const Symbol = this.primitives.Symbol;
|
|
282
235
|
this.filterPublicKeys = [
|
|
283
236
|
"__esModule",
|
|
@@ -310,9 +263,7 @@ class VitestMocker {
|
|
|
310
263
|
}
|
|
311
264
|
getMockerRegistry() {
|
|
312
265
|
const suite = this.getSuiteFilepath();
|
|
313
|
-
if (!this.registries.has(suite))
|
|
314
|
-
this.registries.set(suite, new MockerRegistry());
|
|
315
|
-
}
|
|
266
|
+
if (!this.registries.has(suite)) this.registries.set(suite, new MockerRegistry());
|
|
316
267
|
return this.registries.get(suite);
|
|
317
268
|
}
|
|
318
269
|
reset() {
|
|
@@ -320,9 +271,7 @@ class VitestMocker {
|
|
|
320
271
|
}
|
|
321
272
|
deleteCachedItem(id) {
|
|
322
273
|
const mockId = this.getMockPath(id);
|
|
323
|
-
if (this.moduleCache.has(mockId))
|
|
324
|
-
this.moduleCache.delete(mockId);
|
|
325
|
-
}
|
|
274
|
+
if (this.moduleCache.has(mockId)) this.moduleCache.delete(mockId);
|
|
326
275
|
}
|
|
327
276
|
isModuleDirectory(path) {
|
|
328
277
|
return this.moduleDirectories.some((dir) => path.includes(dir));
|
|
@@ -347,9 +296,7 @@ class VitestMocker {
|
|
|
347
296
|
const { id: unresolvedId } = error[Symbol.for("vitest.error.not_found.data")];
|
|
348
297
|
id = unresolvedId;
|
|
349
298
|
fsPath = unresolvedId;
|
|
350
|
-
} else
|
|
351
|
-
throw error;
|
|
352
|
-
}
|
|
299
|
+
} else throw error;
|
|
353
300
|
}
|
|
354
301
|
// external is node_module or unresolved module
|
|
355
302
|
// for example, some people mock "vscode" and don't have it installed
|
|
@@ -361,38 +308,28 @@ class VitestMocker {
|
|
|
361
308
|
};
|
|
362
309
|
}
|
|
363
310
|
async resolveMocks() {
|
|
364
|
-
if (!VitestMocker.pendingIds.length)
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
311
|
+
if (!VitestMocker.pendingIds.length) return;
|
|
367
312
|
await Promise.all(VitestMocker.pendingIds.map(async (mock) => {
|
|
368
313
|
const { fsPath, external } = await this.resolvePath(mock.id, mock.importer);
|
|
369
|
-
if (mock.action === "unmock")
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
if (mock.action === "mock") {
|
|
373
|
-
this.mockPath(mock.id, fsPath, external, mock.type, mock.factory);
|
|
374
|
-
}
|
|
314
|
+
if (mock.action === "unmock") this.unmockPath(fsPath);
|
|
315
|
+
if (mock.action === "mock") this.mockPath(mock.id, fsPath, external, mock.type, mock.factory);
|
|
375
316
|
}));
|
|
376
317
|
VitestMocker.pendingIds = [];
|
|
377
318
|
}
|
|
378
319
|
async callFunctionMock(dep, mock) {
|
|
379
320
|
const cached = this.moduleCache.get(dep)?.exports;
|
|
380
|
-
if (cached)
|
|
381
|
-
return cached;
|
|
382
|
-
}
|
|
321
|
+
if (cached) return cached;
|
|
383
322
|
const exports = await mock.resolve();
|
|
384
323
|
const moduleExports = new Proxy(exports, { get: (target, prop) => {
|
|
385
324
|
const val = target[prop];
|
|
386
325
|
// 'then' can exist on non-Promise objects, need nested instanceof check for logic to work
|
|
387
326
|
if (prop === "then") {
|
|
388
|
-
if (target instanceof Promise)
|
|
389
|
-
return target.then.bind(target);
|
|
390
|
-
}
|
|
327
|
+
if (target instanceof Promise) return target.then.bind(target);
|
|
391
328
|
} else if (!(prop in target)) {
|
|
392
|
-
if (this.filterPublicKeys.includes(prop))
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
329
|
+
if (this.filterPublicKeys.includes(prop)) return void 0;
|
|
330
|
+
throw this.createError(`[vitest] No "${String(prop)}" export is defined on the "${mock.raw}" mock. Did you forget to return it from "vi.mock"?
|
|
331
|
+
If you need to partially mock a module, you can use "importOriginal" helper inside:
|
|
332
|
+
`, highlight(`vi.mock(import("${mock.raw}"), async (importOriginal) => {
|
|
396
333
|
const actual = await importOriginal()
|
|
397
334
|
return {
|
|
398
335
|
...actual,
|
|
@@ -425,9 +362,7 @@ class VitestMocker {
|
|
|
425
362
|
}
|
|
426
363
|
mockObject(object, mockExports = {}, behavior = "automock") {
|
|
427
364
|
const spyOn = this.spyModule?.spyOn;
|
|
428
|
-
if (!spyOn)
|
|
429
|
-
throw this.createError("[vitest] `spyModule` is not defined. This is a Vitest error. Please open a new issue with reproduction.");
|
|
430
|
-
}
|
|
365
|
+
if (!spyOn) throw this.createError("[vitest] `spyModule` is not defined. This is a Vitest error. Please open a new issue with reproduction.");
|
|
431
366
|
return mockObject({
|
|
432
367
|
globalConstructors: this.primitives,
|
|
433
368
|
spyOn,
|
|
@@ -443,17 +378,12 @@ class VitestMocker {
|
|
|
443
378
|
mockPath(originalId, path, external, mockType, factory) {
|
|
444
379
|
const registry = this.getMockerRegistry();
|
|
445
380
|
const id = this.normalizePath(path);
|
|
446
|
-
if (mockType === "manual")
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
registry.register("autospy", originalId, id, id);
|
|
450
|
-
} else {
|
|
381
|
+
if (mockType === "manual") registry.register("manual", originalId, id, id, factory);
|
|
382
|
+
else if (mockType === "autospy") registry.register("autospy", originalId, id, id);
|
|
383
|
+
else {
|
|
451
384
|
const redirect = this.resolveMockPath(id, external);
|
|
452
|
-
if (redirect)
|
|
453
|
-
|
|
454
|
-
} else {
|
|
455
|
-
registry.register("automock", originalId, id, id);
|
|
456
|
-
}
|
|
385
|
+
if (redirect) registry.register("redirect", originalId, id, id, redirect);
|
|
386
|
+
else registry.register("automock", originalId, id, id);
|
|
457
387
|
}
|
|
458
388
|
// every time the mock is registered, we remove the previous one from the cache
|
|
459
389
|
this.deleteCachedItem(id);
|
|
@@ -469,33 +399,24 @@ class VitestMocker {
|
|
|
469
399
|
let mock = this.getDependencyMock(normalizedId);
|
|
470
400
|
if (!mock) {
|
|
471
401
|
const redirect = this.resolveMockPath(normalizedId, external);
|
|
472
|
-
if (redirect)
|
|
473
|
-
|
|
474
|
-
} else {
|
|
475
|
-
mock = new AutomockedModule(rawId, normalizedId, normalizedId);
|
|
476
|
-
}
|
|
402
|
+
if (redirect) mock = new RedirectedModule(rawId, normalizedId, normalizedId, redirect);
|
|
403
|
+
else mock = new AutomockedModule(rawId, normalizedId, normalizedId);
|
|
477
404
|
}
|
|
478
405
|
if (mock.type === "automock" || mock.type === "autospy") {
|
|
479
406
|
const mod = await this.executor.cachedRequest(id, fsPath, [importee]);
|
|
480
407
|
return this.mockObject(mod, {}, mock.type);
|
|
481
408
|
}
|
|
482
|
-
if (mock.type === "manual")
|
|
483
|
-
return this.callFunctionMock(fsPath, mock);
|
|
484
|
-
}
|
|
409
|
+
if (mock.type === "manual") return this.callFunctionMock(fsPath, mock);
|
|
485
410
|
return this.executor.dependencyRequest(mock.redirect, mock.redirect, [importee]);
|
|
486
411
|
}
|
|
487
412
|
async requestWithMock(url, callstack) {
|
|
488
413
|
const id = this.normalizePath(url);
|
|
489
414
|
const mock = this.getDependencyMock(id);
|
|
490
|
-
if (!mock)
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
415
|
+
if (!mock) return;
|
|
493
416
|
const mockPath = this.getMockPath(id);
|
|
494
417
|
if (mock.type === "automock" || mock.type === "autospy") {
|
|
495
418
|
const cache = this.moduleCache.get(mockPath);
|
|
496
|
-
if (cache.exports)
|
|
497
|
-
return cache.exports;
|
|
498
|
-
}
|
|
419
|
+
if (cache.exports) return cache.exports;
|
|
499
420
|
const exports = {};
|
|
500
421
|
// Assign the empty exports object early to allow for cycles to work. The object will be filled by mockObject()
|
|
501
422
|
this.moduleCache.set(mockPath, { exports });
|
|
@@ -503,22 +424,19 @@ class VitestMocker {
|
|
|
503
424
|
this.mockObject(mod, exports, mock.type);
|
|
504
425
|
return exports;
|
|
505
426
|
}
|
|
506
|
-
if (mock.type === "manual" && !callstack.includes(mockPath) && !callstack.includes(url)) {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
callstack.splice(indexMock, 1);
|
|
518
|
-
}
|
|
519
|
-
} else if (mock.type === "redirect" && !callstack.includes(mock.redirect)) {
|
|
520
|
-
return mock.redirect;
|
|
427
|
+
if (mock.type === "manual" && !callstack.includes(mockPath) && !callstack.includes(url)) try {
|
|
428
|
+
callstack.push(mockPath);
|
|
429
|
+
// this will not work if user does Promise.all(import(), import())
|
|
430
|
+
// we can also use AsyncLocalStorage to store callstack, but this won't work in the browser
|
|
431
|
+
// maybe we should improve mock API in the future?
|
|
432
|
+
this.mockContext.callstack = callstack;
|
|
433
|
+
return await this.callFunctionMock(mockPath, mock);
|
|
434
|
+
} finally {
|
|
435
|
+
this.mockContext.callstack = null;
|
|
436
|
+
const indexMock = callstack.indexOf(mockPath);
|
|
437
|
+
callstack.splice(indexMock, 1);
|
|
521
438
|
}
|
|
439
|
+
else if (mock.type === "redirect" && !callstack.includes(mock.redirect)) return mock.redirect;
|
|
522
440
|
}
|
|
523
441
|
queueMock(id, importer, factoryOrOptions) {
|
|
524
442
|
const mockType = getMockType(factoryOrOptions);
|
|
@@ -526,7 +444,7 @@ class VitestMocker {
|
|
|
526
444
|
action: "mock",
|
|
527
445
|
id,
|
|
528
446
|
importer,
|
|
529
|
-
factory: typeof factoryOrOptions === "function" ? factoryOrOptions :
|
|
447
|
+
factory: typeof factoryOrOptions === "function" ? factoryOrOptions : void 0,
|
|
530
448
|
type: mockType
|
|
531
449
|
});
|
|
532
450
|
}
|
|
@@ -539,12 +457,8 @@ class VitestMocker {
|
|
|
539
457
|
}
|
|
540
458
|
}
|
|
541
459
|
function getMockType(factoryOrOptions) {
|
|
542
|
-
if (!factoryOrOptions)
|
|
543
|
-
|
|
544
|
-
}
|
|
545
|
-
if (typeof factoryOrOptions === "function") {
|
|
546
|
-
return "manual";
|
|
547
|
-
}
|
|
460
|
+
if (!factoryOrOptions) return "automock";
|
|
461
|
+
if (typeof factoryOrOptions === "function") return "manual";
|
|
548
462
|
return factoryOrOptions.spy ? "autospy" : "automock";
|
|
549
463
|
}
|
|
550
464
|
|
|
@@ -556,7 +470,7 @@ async function createVitestExecutor(options) {
|
|
|
556
470
|
await runner.mocker.initializeSpyModule();
|
|
557
471
|
return runner;
|
|
558
472
|
}
|
|
559
|
-
const externalizeMap = new Map();
|
|
473
|
+
const externalizeMap = /* @__PURE__ */ new Map();
|
|
560
474
|
const bareVitestRegexp = /^@?vitest(?:\/|$)/;
|
|
561
475
|
const dispose = [];
|
|
562
476
|
function listenForErrors(state) {
|
|
@@ -567,15 +481,11 @@ function listenForErrors(state) {
|
|
|
567
481
|
const listeners = process.listeners(event);
|
|
568
482
|
// if there is another listener, assume that it's handled by user code
|
|
569
483
|
// one is Vitest's own listener
|
|
570
|
-
if (listeners.length > 1)
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
484
|
+
if (listeners.length > 1) return;
|
|
573
485
|
const error = processError(err);
|
|
574
486
|
if (!isPrimitive(error)) {
|
|
575
|
-
error.VITEST_TEST_NAME = worker.current?.type === "test" ? worker.current.name :
|
|
576
|
-
if (worker.filepath)
|
|
577
|
-
error.VITEST_TEST_PATH = worker.filepath;
|
|
578
|
-
}
|
|
487
|
+
error.VITEST_TEST_NAME = worker.current?.type === "test" ? worker.current.name : void 0;
|
|
488
|
+
if (worker.filepath) error.VITEST_TEST_PATH = worker.filepath;
|
|
579
489
|
error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun;
|
|
580
490
|
}
|
|
581
491
|
state().rpc.onUnhandledError(error, type);
|
|
@@ -591,9 +501,7 @@ function listenForErrors(state) {
|
|
|
591
501
|
}
|
|
592
502
|
const relativeIds = {};
|
|
593
503
|
function getVitestImport(id, state) {
|
|
594
|
-
if (externalizeMap.has(id)) {
|
|
595
|
-
return { externalize: externalizeMap.get(id) };
|
|
596
|
-
}
|
|
504
|
+
if (externalizeMap.has(id)) return { externalize: externalizeMap.get(id) };
|
|
597
505
|
// always externalize Vitest because we import from there before running tests
|
|
598
506
|
// so we already have it cached by Node.js
|
|
599
507
|
const root = state().config.root;
|
|
@@ -623,9 +531,7 @@ async function startVitestExecutor(options) {
|
|
|
623
531
|
return await createVitestExecutor({
|
|
624
532
|
async fetchModule(id) {
|
|
625
533
|
const vitest = getVitestImport(id, state);
|
|
626
|
-
if (vitest)
|
|
627
|
-
return vitest;
|
|
628
|
-
}
|
|
534
|
+
if (vitest) return vitest;
|
|
629
535
|
const result = await rpc().fetch(id, getTransformMode());
|
|
630
536
|
if (result.id && !result.externalize) {
|
|
631
537
|
const code = readFileSync(result.id, "utf-8");
|
|
@@ -658,9 +564,7 @@ async function startVitestExecutor(options) {
|
|
|
658
564
|
});
|
|
659
565
|
}
|
|
660
566
|
function updateStyle(id, css) {
|
|
661
|
-
if (typeof document === "undefined")
|
|
662
|
-
return;
|
|
663
|
-
}
|
|
567
|
+
if (typeof document === "undefined") return;
|
|
664
568
|
const element = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
665
569
|
if (element) {
|
|
666
570
|
element.textContent = css;
|
|
@@ -674,13 +578,9 @@ function updateStyle(id, css) {
|
|
|
674
578
|
head?.appendChild(style);
|
|
675
579
|
}
|
|
676
580
|
function removeStyle(id) {
|
|
677
|
-
if (typeof document === "undefined")
|
|
678
|
-
return;
|
|
679
|
-
}
|
|
581
|
+
if (typeof document === "undefined") return;
|
|
680
582
|
const sheet = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
681
|
-
if (sheet)
|
|
682
|
-
document.head.removeChild(sheet);
|
|
683
|
-
}
|
|
583
|
+
if (sheet) document.head.removeChild(sheet);
|
|
684
584
|
}
|
|
685
585
|
function getDefaultRequestStubs(context) {
|
|
686
586
|
if (!context) {
|
|
@@ -725,9 +625,7 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
725
625
|
} else if (options.externalModulesExecutor) {
|
|
726
626
|
this.primitives = vm.runInContext("({ Object, Reflect, Symbol })", options.context);
|
|
727
627
|
this.externalModules = options.externalModulesExecutor;
|
|
728
|
-
} else
|
|
729
|
-
throw new Error("When context is provided, externalModulesExecutor must be provided as well.");
|
|
730
|
-
}
|
|
628
|
+
} else throw new Error("When context is provided, externalModulesExecutor must be provided as well.");
|
|
731
629
|
}
|
|
732
630
|
getContextPrimitives() {
|
|
733
631
|
return this.primitives;
|
|
@@ -740,9 +638,7 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
740
638
|
return this.options.moduleExecutionInfo;
|
|
741
639
|
}
|
|
742
640
|
shouldResolveId(id, _importee) {
|
|
743
|
-
if (isInternalRequest(id) || id.startsWith("data:"))
|
|
744
|
-
return false;
|
|
745
|
-
}
|
|
641
|
+
if (isInternalRequest(id) || id.startsWith("data:")) return false;
|
|
746
642
|
const transformMode = this.state.environment?.transformMode ?? "ssr";
|
|
747
643
|
// do not try and resolve node builtins in Node
|
|
748
644
|
// import('url') returns Node internal even if 'url' package is installed
|
|
@@ -752,12 +648,8 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
752
648
|
return super.resolveUrl(id, importer);
|
|
753
649
|
}
|
|
754
650
|
async resolveUrl(id, importer) {
|
|
755
|
-
if (VitestMocker.pendingIds.length)
|
|
756
|
-
|
|
757
|
-
}
|
|
758
|
-
if (importer && importer.startsWith("mock:")) {
|
|
759
|
-
importer = importer.slice(5);
|
|
760
|
-
}
|
|
651
|
+
if (VitestMocker.pendingIds.length) await this.mocker.resolveMocks();
|
|
652
|
+
if (importer && importer.startsWith("mock:")) importer = importer.slice(5);
|
|
761
653
|
try {
|
|
762
654
|
return await super.resolveUrl(id, importer);
|
|
763
655
|
} catch (error) {
|
|
@@ -765,18 +657,14 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
765
657
|
const { id } = error[Symbol.for("vitest.error.not_found.data")];
|
|
766
658
|
const path = this.mocker.normalizePath(id);
|
|
767
659
|
const mock = this.mocker.getDependencyMock(path);
|
|
768
|
-
if (mock !==
|
|
769
|
-
return [id, id];
|
|
770
|
-
}
|
|
660
|
+
if (mock !== void 0) return [id, id];
|
|
771
661
|
}
|
|
772
662
|
throw error;
|
|
773
663
|
}
|
|
774
664
|
}
|
|
775
665
|
async runModule(context, transformed) {
|
|
776
666
|
const vmContext = this.options.context;
|
|
777
|
-
if (!vmContext || !this.externalModules)
|
|
778
|
-
return super.runModule(context, transformed);
|
|
779
|
-
}
|
|
667
|
+
if (!vmContext || !this.externalModules) return super.runModule(context, transformed);
|
|
780
668
|
// add 'use strict' since ESM enables it by default
|
|
781
669
|
const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
|
|
782
670
|
const code = `${codeDefinition}${transformed}\n}}`;
|
|
@@ -785,27 +673,25 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
785
673
|
lineOffset: 0,
|
|
786
674
|
columnOffset: -codeDefinition.length
|
|
787
675
|
};
|
|
788
|
-
this.
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
676
|
+
const finishModuleExecutionInfo = this.startCalculateModuleExecutionInfo(options.filename, codeDefinition.length);
|
|
677
|
+
try {
|
|
678
|
+
const fn = vm.runInContext(code, vmContext, {
|
|
679
|
+
...options,
|
|
680
|
+
importModuleDynamically: this.externalModules.importModuleDynamically
|
|
681
|
+
});
|
|
682
|
+
await fn(...Object.values(context));
|
|
683
|
+
} finally {
|
|
684
|
+
this.options.moduleExecutionInfo?.set(options.filename, finishModuleExecutionInfo());
|
|
685
|
+
}
|
|
794
686
|
}
|
|
795
687
|
async importExternalModule(path) {
|
|
796
|
-
if (this.externalModules)
|
|
797
|
-
return this.externalModules.import(path);
|
|
798
|
-
}
|
|
688
|
+
if (this.externalModules) return this.externalModules.import(path);
|
|
799
689
|
return super.importExternalModule(path);
|
|
800
690
|
}
|
|
801
691
|
async dependencyRequest(id, fsPath, callstack) {
|
|
802
692
|
const mocked = await this.mocker.requestWithMock(fsPath, callstack);
|
|
803
|
-
if (typeof mocked === "string")
|
|
804
|
-
|
|
805
|
-
}
|
|
806
|
-
if (mocked && typeof mocked === "object") {
|
|
807
|
-
return mocked;
|
|
808
|
-
}
|
|
693
|
+
if (typeof mocked === "string") return super.dependencyRequest(mocked, mocked, callstack);
|
|
694
|
+
if (mocked && typeof mocked === "object") return mocked;
|
|
809
695
|
return super.dependencyRequest(id, fsPath, callstack);
|
|
810
696
|
}
|
|
811
697
|
prepareContext(context) {
|
|
@@ -814,9 +700,7 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
814
700
|
const globalNamespace = this.options.context || globalThis;
|
|
815
701
|
Object.defineProperty(context.__vite_ssr_import_meta__, "vitest", { get: () => globalNamespace.__vitest_index__ });
|
|
816
702
|
}
|
|
817
|
-
if (this.options.context && this.externalModules)
|
|
818
|
-
context.require = this.externalModules.createRequire(context.__filename);
|
|
819
|
-
}
|
|
703
|
+
if (this.options.context && this.externalModules) context.require = this.externalModules.createRequire(context.__filename);
|
|
820
704
|
return context;
|
|
821
705
|
}
|
|
822
706
|
}
|
|
@@ -18,9 +18,7 @@ class VitestGit {
|
|
|
18
18
|
}
|
|
19
19
|
async findChangedFiles(options) {
|
|
20
20
|
const root = await this.getRoot(this.cwd);
|
|
21
|
-
if (!root)
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
21
|
+
if (!root) return null;
|
|
24
22
|
this.root = root;
|
|
25
23
|
const changedSince = options.changedSince;
|
|
26
24
|
if (typeof changedSince === "string") {
|
|
@@ -113,6 +113,8 @@ declare module "@vitest/runner" {
|
|
|
113
113
|
* This API is useful for running snapshot tests concurrently because global expect cannot track them.
|
|
114
114
|
*/
|
|
115
115
|
readonly expect: ExpectStatic;
|
|
116
|
+
/** @internal */
|
|
117
|
+
_local: boolean;
|
|
116
118
|
}
|
|
117
119
|
interface TaskMeta {
|
|
118
120
|
typecheck?: boolean;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { g as globalApis } from './constants.DnKduX2e.js';
|
|
2
|
-
import { V as VitestIndex } from './index.
|
|
3
|
-
import './vi.
|
|
2
|
+
import { V as VitestIndex } from './index.CdQS2e2Q.js';
|
|
3
|
+
import './vi.bdSIJ99Y.js';
|
|
4
4
|
import '@vitest/expect';
|
|
5
5
|
import '@vitest/runner';
|
|
6
6
|
import '@vitest/runner/utils';
|
|
7
7
|
import 'chai';
|
|
8
|
-
import './utils.
|
|
8
|
+
import './utils.XdZDrNZV.js';
|
|
9
9
|
import '@vitest/utils';
|
|
10
10
|
import './_commonjsHelpers.BFTU3MAI.js';
|
|
11
11
|
import '@vitest/snapshot';
|
|
12
12
|
import '@vitest/utils/error';
|
|
13
13
|
import '@vitest/spy';
|
|
14
14
|
import '@vitest/utils/source-map';
|
|
15
|
-
import './date.
|
|
16
|
-
import './benchmark.
|
|
15
|
+
import './date.Bq6ZW5rf.js';
|
|
16
|
+
import './benchmark.CYdenmiT.js';
|
|
17
17
|
import 'expect-type';
|
|
18
18
|
|
|
19
19
|
function registerApiGlobally() {
|