vitest 3.1.0-beta.1 → 3.1.0
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.js +4 -4
- package/dist/chunks/base.bV8rwssx.js +41 -0
- package/dist/chunks/benchmark.BKUatJGy.js +39 -0
- package/dist/chunks/cac.1WcTh-zl.js +1529 -0
- package/dist/chunks/{cli-api.BwkkJsRe.js → cli-api.2yb7XCwB.js} +4640 -5072
- package/dist/chunks/console.D6t261w0.js +173 -0
- package/dist/chunks/constants.BZZyIeIE.js +43 -0
- package/dist/chunks/coverage.0iPg4Wrz.js +33 -0
- package/dist/chunks/{coverage.gV8doR2Y.js → coverage.SfnlalVs.js} +2424 -2482
- package/dist/chunks/creator.CuL7xDWI.js +705 -0
- package/dist/chunks/date.CDOsz-HY.js +53 -0
- package/dist/chunks/defaults.DmfNPoe5.js +114 -0
- package/dist/chunks/{env.D4Lgay0q.js → env.Dq0hM4Xv.js} +1 -1
- package/dist/chunks/execute.CwmnH2oH.js +791 -0
- package/dist/chunks/git.DXfdBEfR.js +74 -0
- package/dist/chunks/{globals.BEpDe-k3.js → globals.DCbUWjip.js} +10 -10
- package/dist/chunks/{index.D7Ny8f_s.js → index.BDobFbcz.js} +6 -7
- package/dist/chunks/index.CwHmn5H5.js +2422 -0
- package/dist/chunks/index.DFXFpH3w.js +607 -0
- package/dist/chunks/index.VfYQ6MXY.js +104 -0
- package/dist/chunks/inspector.DbDkSkFn.js +54 -0
- package/dist/chunks/node.IqGoMrm4.js +15 -0
- package/dist/chunks/{reporters.d.r7poTZjA.d.ts → reporters.d.CfRkRKN2.d.ts} +52 -20
- package/dist/chunks/rpc.DGgL5dw7.js +92 -0
- package/dist/chunks/run-once.I7PpBOk1.js +47 -0
- package/dist/chunks/runBaseTests.CqmKSG99.js +134 -0
- package/dist/chunks/setup-common.DEGDGBiA.js +88 -0
- package/dist/chunks/{typechecker.BlF3eHsb.js → typechecker.CG0zmr19.js} +620 -622
- package/dist/chunks/utils.BfxieIyZ.js +66 -0
- package/dist/chunks/utils.CtocqOoE.js +72 -0
- package/dist/chunks/utils.Lot3J_8U.js +194 -0
- package/dist/chunks/{vi.nSCvwQ7l.js → vi.B-PuvDzu.js} +878 -1019
- package/dist/chunks/vite.d.4pkSbgmp.d.ts +23 -0
- package/dist/chunks/vm.Lp7mPCVW.js +796 -0
- package/dist/cli.js +2 -2
- package/dist/config.cjs +97 -103
- package/dist/config.d.ts +6 -4
- package/dist/config.js +6 -6
- package/dist/coverage.d.ts +2 -1
- package/dist/coverage.js +7 -7
- package/dist/environments.js +1 -1
- package/dist/execute.d.ts +0 -2
- package/dist/execute.js +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.js +6 -6
- package/dist/node.d.ts +17 -6
- package/dist/node.js +36 -45
- package/dist/path.js +1 -4
- package/dist/reporters.d.ts +2 -1
- package/dist/reporters.js +4 -4
- package/dist/runners.js +231 -267
- package/dist/snapshot.js +2 -2
- package/dist/suite.js +2 -2
- package/dist/worker.js +98 -114
- package/dist/workers/forks.js +22 -22
- package/dist/workers/runVmTests.js +61 -66
- package/dist/workers/threads.js +13 -13
- package/dist/workers/vmForks.js +24 -24
- package/dist/workers/vmThreads.js +15 -15
- package/dist/workers.d.ts +2 -1
- package/dist/workers.js +10 -10
- package/package.json +17 -17
- package/dist/chunks/base.DV59CbtV.js +0 -45
- package/dist/chunks/benchmark.DL72EVN-.js +0 -40
- package/dist/chunks/cac.BjmXy7OV.js +0 -1664
- package/dist/chunks/console.CN7AiMGV.js +0 -179
- package/dist/chunks/constants.DTYd6dNH.js +0 -46
- package/dist/chunks/coverage.A3sS5-Wm.js +0 -40
- package/dist/chunks/creator.BsBnpTzI.js +0 -670
- package/dist/chunks/date.W2xKR2qe.js +0 -53
- package/dist/chunks/defaults.C2Ndd9wx.js +0 -119
- package/dist/chunks/execute.eDH0aFFd.js +0 -839
- package/dist/chunks/git.B5SDxu-n.js +0 -69
- package/dist/chunks/index.DOyx6FYJ.js +0 -2551
- package/dist/chunks/index.K90BXFOx.js +0 -658
- package/dist/chunks/index.uXkkC4xl.js +0 -111
- package/dist/chunks/inspector.DKLceBVD.js +0 -54
- package/dist/chunks/node.AKq966Jp.js +0 -15
- package/dist/chunks/rpc.TVf73xOu.js +0 -102
- package/dist/chunks/run-once.2ogXb3JV.js +0 -28
- package/dist/chunks/runBaseTests.BVrL_ow3.js +0 -142
- package/dist/chunks/setup-common.CPvtqi8q.js +0 -96
- package/dist/chunks/utils.C8RiOc4B.js +0 -77
- package/dist/chunks/utils.Cn0zI1t3.js +0 -68
- package/dist/chunks/utils.bLM2atbD.js +0 -198
- package/dist/chunks/vite.d.Fvq-NZoa.d.ts +0 -11
- package/dist/chunks/vm.jEFQDlX_.js +0 -852
|
@@ -0,0 +1,791 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import vm from 'node:vm';
|
|
4
|
+
import { processError } from '@vitest/utils/error';
|
|
5
|
+
import { normalize as normalize$1 } from 'pathe';
|
|
6
|
+
import { ViteNodeRunner, DEFAULT_REQUEST_STUBS } from 'vite-node/client';
|
|
7
|
+
import { isInternalRequest, isNodeBuiltin as isNodeBuiltin$1, isPrimitive, toFilePath } from 'vite-node/utils';
|
|
8
|
+
import { distDir } from '../path.js';
|
|
9
|
+
import { resolve as resolve$1, isAbsolute as isAbsolute$1 } from 'node:path';
|
|
10
|
+
import { MockerRegistry, mockObject, RedirectedModule, AutomockedModule } from '@vitest/mocker';
|
|
11
|
+
import { builtinModules } from 'node:module';
|
|
12
|
+
import { highlight } from '@vitest/utils';
|
|
13
|
+
|
|
14
|
+
const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
15
|
+
function normalizeWindowsPath(input = "") {
|
|
16
|
+
if (!input) {
|
|
17
|
+
return input;
|
|
18
|
+
}
|
|
19
|
+
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
20
|
+
}
|
|
21
|
+
const _UNC_REGEX = /^[/\\]{2}/;
|
|
22
|
+
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
23
|
+
const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
|
|
24
|
+
const _EXTNAME_RE = /.(\.[^./]+|\.)$/;
|
|
25
|
+
const normalize = function(path) {
|
|
26
|
+
if (path.length === 0) {
|
|
27
|
+
return ".";
|
|
28
|
+
}
|
|
29
|
+
path = normalizeWindowsPath(path);
|
|
30
|
+
const isUNCPath = path.match(_UNC_REGEX);
|
|
31
|
+
const isPathAbsolute = isAbsolute(path);
|
|
32
|
+
const trailingSeparator = path[path.length - 1] === "/";
|
|
33
|
+
path = normalizeString(path, !isPathAbsolute);
|
|
34
|
+
if (path.length === 0) {
|
|
35
|
+
if (isPathAbsolute) {
|
|
36
|
+
return "/";
|
|
37
|
+
}
|
|
38
|
+
return trailingSeparator ? "./" : ".";
|
|
39
|
+
}
|
|
40
|
+
if (trailingSeparator) {
|
|
41
|
+
path += "/";
|
|
42
|
+
}
|
|
43
|
+
if (_DRIVE_LETTER_RE.test(path)) {
|
|
44
|
+
path += "/";
|
|
45
|
+
}
|
|
46
|
+
if (isUNCPath) {
|
|
47
|
+
if (!isPathAbsolute) {
|
|
48
|
+
return `//./${path}`;
|
|
49
|
+
}
|
|
50
|
+
return `//${path}`;
|
|
51
|
+
}
|
|
52
|
+
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
|
|
53
|
+
};
|
|
54
|
+
const join = function(...segments) {
|
|
55
|
+
let path = "";
|
|
56
|
+
for (const seg of segments) {
|
|
57
|
+
if (!seg) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (path.length > 0) {
|
|
61
|
+
const pathTrailing = path[path.length - 1] === "/";
|
|
62
|
+
const segLeading = seg[0] === "/";
|
|
63
|
+
const both = pathTrailing && segLeading;
|
|
64
|
+
if (both) {
|
|
65
|
+
path += seg.slice(1);
|
|
66
|
+
} else {
|
|
67
|
+
path += pathTrailing || segLeading ? seg : `/${seg}`;
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
path += seg;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return normalize(path);
|
|
74
|
+
};
|
|
75
|
+
function cwd() {
|
|
76
|
+
if (typeof process !== "undefined" && typeof process.cwd === "function") {
|
|
77
|
+
return process.cwd().replace(/\\/g, "/");
|
|
78
|
+
}
|
|
79
|
+
return "/";
|
|
80
|
+
}
|
|
81
|
+
const resolve = function(...arguments_) {
|
|
82
|
+
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
|
|
83
|
+
let resolvedPath = "";
|
|
84
|
+
let resolvedAbsolute = false;
|
|
85
|
+
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
86
|
+
const path = index >= 0 ? arguments_[index] : cwd();
|
|
87
|
+
if (!path || path.length === 0) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
91
|
+
resolvedAbsolute = isAbsolute(path);
|
|
92
|
+
}
|
|
93
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
94
|
+
if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
|
|
95
|
+
return `/${resolvedPath}`;
|
|
96
|
+
}
|
|
97
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
98
|
+
};
|
|
99
|
+
function normalizeString(path, allowAboveRoot) {
|
|
100
|
+
let res = "";
|
|
101
|
+
let lastSegmentLength = 0;
|
|
102
|
+
let lastSlash = -1;
|
|
103
|
+
let dots = 0;
|
|
104
|
+
let char = null;
|
|
105
|
+
for (let index = 0; index <= path.length; ++index) {
|
|
106
|
+
if (index < path.length) {
|
|
107
|
+
char = path[index];
|
|
108
|
+
} else if (char === "/") {
|
|
109
|
+
break;
|
|
110
|
+
} else {
|
|
111
|
+
char = "/";
|
|
112
|
+
}
|
|
113
|
+
if (char === "/") {
|
|
114
|
+
if (lastSlash === index - 1 || dots === 1);
|
|
115
|
+
else if (dots === 2) {
|
|
116
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
117
|
+
if (res.length > 2) {
|
|
118
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
119
|
+
if (lastSlashIndex === -1) {
|
|
120
|
+
res = "";
|
|
121
|
+
lastSegmentLength = 0;
|
|
122
|
+
} else {
|
|
123
|
+
res = res.slice(0, lastSlashIndex);
|
|
124
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
125
|
+
}
|
|
126
|
+
lastSlash = index;
|
|
127
|
+
dots = 0;
|
|
128
|
+
continue;
|
|
129
|
+
} else if (res.length > 0) {
|
|
130
|
+
res = "";
|
|
131
|
+
lastSegmentLength = 0;
|
|
132
|
+
lastSlash = index;
|
|
133
|
+
dots = 0;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (allowAboveRoot) {
|
|
138
|
+
res += res.length > 0 ? "/.." : "..";
|
|
139
|
+
lastSegmentLength = 2;
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
if (res.length > 0) {
|
|
143
|
+
res += `/${path.slice(lastSlash + 1, index)}`;
|
|
144
|
+
} else {
|
|
145
|
+
res = path.slice(lastSlash + 1, index);
|
|
146
|
+
}
|
|
147
|
+
lastSegmentLength = index - lastSlash - 1;
|
|
148
|
+
}
|
|
149
|
+
lastSlash = index;
|
|
150
|
+
dots = 0;
|
|
151
|
+
} else if (char === "." && dots !== -1) {
|
|
152
|
+
++dots;
|
|
153
|
+
} else {
|
|
154
|
+
dots = -1;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return res;
|
|
158
|
+
}
|
|
159
|
+
const isAbsolute = function(p) {
|
|
160
|
+
return _IS_ABSOLUTE_RE.test(p);
|
|
161
|
+
};
|
|
162
|
+
const extname = function(p) {
|
|
163
|
+
if (p === "..") return "";
|
|
164
|
+
const match = _EXTNAME_RE.exec(normalizeWindowsPath(p));
|
|
165
|
+
return match && match[1] || "";
|
|
166
|
+
};
|
|
167
|
+
const dirname = function(p) {
|
|
168
|
+
const segments = normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1);
|
|
169
|
+
if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0])) {
|
|
170
|
+
segments[0] += "/";
|
|
171
|
+
}
|
|
172
|
+
return segments.join("/") || (isAbsolute(p) ? "/" : ".");
|
|
173
|
+
};
|
|
174
|
+
const basename = function(p, extension) {
|
|
175
|
+
const segments = normalizeWindowsPath(p).split("/");
|
|
176
|
+
let lastSegment = "";
|
|
177
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
178
|
+
const val = segments[i];
|
|
179
|
+
if (val) {
|
|
180
|
+
lastSegment = val;
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return extension && lastSegment.endsWith(extension) ? lastSegment.slice(0, -extension.length) : lastSegment;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const { existsSync, readdirSync, statSync } = fs;
|
|
188
|
+
function findMockRedirect(root, mockPath, external) {
|
|
189
|
+
const path = external || mockPath;
|
|
190
|
+
if (external || isNodeBuiltin(mockPath) || !existsSync(mockPath)) {
|
|
191
|
+
const mockDirname = dirname(path);
|
|
192
|
+
const mockFolder = join(root, "__mocks__", mockDirname);
|
|
193
|
+
if (!existsSync(mockFolder)) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
const baseOriginal = basename(path);
|
|
197
|
+
function findFile(mockFolder, baseOriginal) {
|
|
198
|
+
const files = readdirSync(mockFolder);
|
|
199
|
+
for (const file of files) {
|
|
200
|
+
const baseFile = basename(file, extname(file));
|
|
201
|
+
if (baseFile === baseOriginal) {
|
|
202
|
+
const path = resolve(mockFolder, file);
|
|
203
|
+
if (statSync(path).isFile()) {
|
|
204
|
+
return path;
|
|
205
|
+
} else {
|
|
206
|
+
const indexFile = findFile(path, "index");
|
|
207
|
+
if (indexFile) {
|
|
208
|
+
return indexFile;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
return findFile(mockFolder, baseOriginal);
|
|
216
|
+
}
|
|
217
|
+
const dir = dirname(path);
|
|
218
|
+
const baseId = basename(path);
|
|
219
|
+
const fullPath = resolve(dir, "__mocks__", baseId);
|
|
220
|
+
return existsSync(fullPath) ? fullPath : null;
|
|
221
|
+
}
|
|
222
|
+
const builtins = new Set([
|
|
223
|
+
...builtinModules,
|
|
224
|
+
"assert/strict",
|
|
225
|
+
"diagnostics_channel",
|
|
226
|
+
"dns/promises",
|
|
227
|
+
"fs/promises",
|
|
228
|
+
"path/posix",
|
|
229
|
+
"path/win32",
|
|
230
|
+
"readline/promises",
|
|
231
|
+
"stream/consumers",
|
|
232
|
+
"stream/promises",
|
|
233
|
+
"stream/web",
|
|
234
|
+
"timers/promises",
|
|
235
|
+
"util/types",
|
|
236
|
+
"wasi"
|
|
237
|
+
]);
|
|
238
|
+
const prefixedBuiltins = new Set([
|
|
239
|
+
"node:sea",
|
|
240
|
+
"node:sqlite",
|
|
241
|
+
"node:test",
|
|
242
|
+
"node:test/reporters"
|
|
243
|
+
]);
|
|
244
|
+
const NODE_BUILTIN_NAMESPACE = "node:";
|
|
245
|
+
function isNodeBuiltin(id) {
|
|
246
|
+
if (prefixedBuiltins.has(id)) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
return builtins.has(id.startsWith(NODE_BUILTIN_NAMESPACE) ? id.slice(NODE_BUILTIN_NAMESPACE.length) : id);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const spyModulePath = resolve$1(distDir, "spy.js");
|
|
253
|
+
class VitestMocker {
|
|
254
|
+
static pendingIds = [];
|
|
255
|
+
spyModule;
|
|
256
|
+
primitives;
|
|
257
|
+
filterPublicKeys;
|
|
258
|
+
registries = new Map();
|
|
259
|
+
mockContext = { callstack: null };
|
|
260
|
+
constructor(executor) {
|
|
261
|
+
this.executor = executor;
|
|
262
|
+
const context = this.executor.options.context;
|
|
263
|
+
if (context) {
|
|
264
|
+
this.primitives = vm.runInContext("({ Object, Error, Function, RegExp, Symbol, Array, Map })", context);
|
|
265
|
+
} else {
|
|
266
|
+
this.primitives = {
|
|
267
|
+
Object,
|
|
268
|
+
Error,
|
|
269
|
+
Function,
|
|
270
|
+
RegExp,
|
|
271
|
+
Symbol: globalThis.Symbol,
|
|
272
|
+
Array,
|
|
273
|
+
Map
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
const Symbol = this.primitives.Symbol;
|
|
277
|
+
this.filterPublicKeys = [
|
|
278
|
+
"__esModule",
|
|
279
|
+
Symbol.asyncIterator,
|
|
280
|
+
Symbol.hasInstance,
|
|
281
|
+
Symbol.isConcatSpreadable,
|
|
282
|
+
Symbol.iterator,
|
|
283
|
+
Symbol.match,
|
|
284
|
+
Symbol.matchAll,
|
|
285
|
+
Symbol.replace,
|
|
286
|
+
Symbol.search,
|
|
287
|
+
Symbol.split,
|
|
288
|
+
Symbol.species,
|
|
289
|
+
Symbol.toPrimitive,
|
|
290
|
+
Symbol.toStringTag,
|
|
291
|
+
Symbol.unscopables
|
|
292
|
+
];
|
|
293
|
+
}
|
|
294
|
+
get root() {
|
|
295
|
+
return this.executor.options.root;
|
|
296
|
+
}
|
|
297
|
+
get moduleCache() {
|
|
298
|
+
return this.executor.moduleCache;
|
|
299
|
+
}
|
|
300
|
+
get moduleDirectories() {
|
|
301
|
+
return this.executor.options.moduleDirectories || [];
|
|
302
|
+
}
|
|
303
|
+
async initializeSpyModule() {
|
|
304
|
+
this.spyModule = await this.executor.executeId(spyModulePath);
|
|
305
|
+
}
|
|
306
|
+
getMockerRegistry() {
|
|
307
|
+
const suite = this.getSuiteFilepath();
|
|
308
|
+
if (!this.registries.has(suite)) {
|
|
309
|
+
this.registries.set(suite, new MockerRegistry());
|
|
310
|
+
}
|
|
311
|
+
return this.registries.get(suite);
|
|
312
|
+
}
|
|
313
|
+
reset() {
|
|
314
|
+
this.registries.clear();
|
|
315
|
+
}
|
|
316
|
+
deleteCachedItem(id) {
|
|
317
|
+
const mockId = this.getMockPath(id);
|
|
318
|
+
if (this.moduleCache.has(mockId)) {
|
|
319
|
+
this.moduleCache.delete(mockId);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
isModuleDirectory(path) {
|
|
323
|
+
return this.moduleDirectories.some((dir) => path.includes(dir));
|
|
324
|
+
}
|
|
325
|
+
getSuiteFilepath() {
|
|
326
|
+
return this.executor.state.filepath || "global";
|
|
327
|
+
}
|
|
328
|
+
createError(message, codeFrame) {
|
|
329
|
+
const Error = this.primitives.Error;
|
|
330
|
+
const error = new Error(message);
|
|
331
|
+
Object.assign(error, { codeFrame });
|
|
332
|
+
return error;
|
|
333
|
+
}
|
|
334
|
+
async resolvePath(rawId, importer) {
|
|
335
|
+
let id;
|
|
336
|
+
let fsPath;
|
|
337
|
+
try {
|
|
338
|
+
[id, fsPath] = await this.executor.originalResolveUrl(rawId, importer);
|
|
339
|
+
} catch (error) {
|
|
340
|
+
if (error.code === "ERR_MODULE_NOT_FOUND") {
|
|
341
|
+
const { id: unresolvedId } = error[Symbol.for("vitest.error.not_found.data")];
|
|
342
|
+
id = unresolvedId;
|
|
343
|
+
fsPath = unresolvedId;
|
|
344
|
+
} else {
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const external = !isAbsolute$1(fsPath) || this.isModuleDirectory(fsPath) ? rawId : null;
|
|
349
|
+
return {
|
|
350
|
+
id,
|
|
351
|
+
fsPath,
|
|
352
|
+
external: external ? this.normalizePath(external) : external
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
async resolveMocks() {
|
|
356
|
+
if (!VitestMocker.pendingIds.length) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
await Promise.all(VitestMocker.pendingIds.map(async (mock) => {
|
|
360
|
+
const { fsPath, external } = await this.resolvePath(mock.id, mock.importer);
|
|
361
|
+
if (mock.action === "unmock") {
|
|
362
|
+
this.unmockPath(fsPath);
|
|
363
|
+
}
|
|
364
|
+
if (mock.action === "mock") {
|
|
365
|
+
this.mockPath(mock.id, fsPath, external, mock.type, mock.factory);
|
|
366
|
+
}
|
|
367
|
+
}));
|
|
368
|
+
VitestMocker.pendingIds = [];
|
|
369
|
+
}
|
|
370
|
+
async callFunctionMock(dep, mock) {
|
|
371
|
+
const cached = this.moduleCache.get(dep)?.exports;
|
|
372
|
+
if (cached) {
|
|
373
|
+
return cached;
|
|
374
|
+
}
|
|
375
|
+
const exports = await mock.resolve();
|
|
376
|
+
const moduleExports = new Proxy(exports, { get: (target, prop) => {
|
|
377
|
+
const val = target[prop];
|
|
378
|
+
if (prop === "then") {
|
|
379
|
+
if (target instanceof Promise) {
|
|
380
|
+
return target.then.bind(target);
|
|
381
|
+
}
|
|
382
|
+
} else if (!(prop in target)) {
|
|
383
|
+
if (this.filterPublicKeys.includes(prop)) {
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
throw this.createError(`[vitest] No "${String(prop)}" export is defined on the "${mock.raw}" mock. ` + "Did you forget to return it from \"vi.mock\"?" + "\nIf you need to partially mock a module, you can use \"importOriginal\" helper inside:\n", highlight(`vi.mock(import("${mock.raw}"), async (importOriginal) => {
|
|
387
|
+
const actual = await importOriginal()
|
|
388
|
+
return {
|
|
389
|
+
...actual,
|
|
390
|
+
// your mocked methods
|
|
391
|
+
}
|
|
392
|
+
})`));
|
|
393
|
+
}
|
|
394
|
+
return val;
|
|
395
|
+
} });
|
|
396
|
+
this.moduleCache.set(dep, { exports: moduleExports });
|
|
397
|
+
return moduleExports;
|
|
398
|
+
}
|
|
399
|
+
getMockContext() {
|
|
400
|
+
return this.mockContext;
|
|
401
|
+
}
|
|
402
|
+
getMockPath(dep) {
|
|
403
|
+
return `mock:${dep}`;
|
|
404
|
+
}
|
|
405
|
+
getDependencyMock(id) {
|
|
406
|
+
const registry = this.getMockerRegistry();
|
|
407
|
+
return registry.get(id);
|
|
408
|
+
}
|
|
409
|
+
normalizePath(path) {
|
|
410
|
+
return this.moduleCache.normalizePath(path);
|
|
411
|
+
}
|
|
412
|
+
resolveMockPath(mockPath, external) {
|
|
413
|
+
return findMockRedirect(this.root, mockPath, external);
|
|
414
|
+
}
|
|
415
|
+
mockObject(object, mockExports = {}, behavior = "automock") {
|
|
416
|
+
const spyOn = this.spyModule?.spyOn;
|
|
417
|
+
if (!spyOn) {
|
|
418
|
+
throw this.createError("[vitest] `spyModule` is not defined. This is a Vitest error. Please open a new issue with reproduction.");
|
|
419
|
+
}
|
|
420
|
+
return mockObject({
|
|
421
|
+
globalConstructors: this.primitives,
|
|
422
|
+
spyOn,
|
|
423
|
+
type: behavior
|
|
424
|
+
}, object, mockExports);
|
|
425
|
+
}
|
|
426
|
+
unmockPath(path) {
|
|
427
|
+
const registry = this.getMockerRegistry();
|
|
428
|
+
const id = this.normalizePath(path);
|
|
429
|
+
registry.delete(id);
|
|
430
|
+
this.deleteCachedItem(id);
|
|
431
|
+
}
|
|
432
|
+
mockPath(originalId, path, external, mockType, factory) {
|
|
433
|
+
const registry = this.getMockerRegistry();
|
|
434
|
+
const id = this.normalizePath(path);
|
|
435
|
+
if (mockType === "manual") {
|
|
436
|
+
registry.register("manual", originalId, id, id, factory);
|
|
437
|
+
} else if (mockType === "autospy") {
|
|
438
|
+
registry.register("autospy", originalId, id, id);
|
|
439
|
+
} else {
|
|
440
|
+
const redirect = this.resolveMockPath(id, external);
|
|
441
|
+
if (redirect) {
|
|
442
|
+
registry.register("redirect", originalId, id, id, redirect);
|
|
443
|
+
} else {
|
|
444
|
+
registry.register("automock", originalId, id, id);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
this.deleteCachedItem(id);
|
|
448
|
+
}
|
|
449
|
+
async importActual(rawId, importer, callstack) {
|
|
450
|
+
const { id, fsPath } = await this.resolvePath(rawId, importer);
|
|
451
|
+
const result = await this.executor.cachedRequest(id, fsPath, callstack || [importer]);
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
454
|
+
async importMock(rawId, importee) {
|
|
455
|
+
const { id, fsPath, external } = await this.resolvePath(rawId, importee);
|
|
456
|
+
const normalizedId = this.normalizePath(fsPath);
|
|
457
|
+
let mock = this.getDependencyMock(normalizedId);
|
|
458
|
+
if (!mock) {
|
|
459
|
+
const redirect = this.resolveMockPath(normalizedId, external);
|
|
460
|
+
if (redirect) {
|
|
461
|
+
mock = new RedirectedModule(rawId, normalizedId, normalizedId, redirect);
|
|
462
|
+
} else {
|
|
463
|
+
mock = new AutomockedModule(rawId, normalizedId, normalizedId);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (mock.type === "automock" || mock.type === "autospy") {
|
|
467
|
+
const mod = await this.executor.cachedRequest(id, fsPath, [importee]);
|
|
468
|
+
return this.mockObject(mod, {}, mock.type);
|
|
469
|
+
}
|
|
470
|
+
if (mock.type === "manual") {
|
|
471
|
+
return this.callFunctionMock(fsPath, mock);
|
|
472
|
+
}
|
|
473
|
+
return this.executor.dependencyRequest(mock.redirect, mock.redirect, [importee]);
|
|
474
|
+
}
|
|
475
|
+
async requestWithMock(url, callstack) {
|
|
476
|
+
const id = this.normalizePath(url);
|
|
477
|
+
const mock = this.getDependencyMock(id);
|
|
478
|
+
if (!mock) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const mockPath = this.getMockPath(id);
|
|
482
|
+
if (mock.type === "automock" || mock.type === "autospy") {
|
|
483
|
+
const cache = this.moduleCache.get(mockPath);
|
|
484
|
+
if (cache.exports) {
|
|
485
|
+
return cache.exports;
|
|
486
|
+
}
|
|
487
|
+
const exports = {};
|
|
488
|
+
this.moduleCache.set(mockPath, { exports });
|
|
489
|
+
const mod = await this.executor.directRequest(url, url, callstack);
|
|
490
|
+
this.mockObject(mod, exports, mock.type);
|
|
491
|
+
return exports;
|
|
492
|
+
}
|
|
493
|
+
if (mock.type === "manual" && !callstack.includes(mockPath) && !callstack.includes(url)) {
|
|
494
|
+
try {
|
|
495
|
+
callstack.push(mockPath);
|
|
496
|
+
this.mockContext.callstack = callstack;
|
|
497
|
+
return await this.callFunctionMock(mockPath, mock);
|
|
498
|
+
} finally {
|
|
499
|
+
this.mockContext.callstack = null;
|
|
500
|
+
const indexMock = callstack.indexOf(mockPath);
|
|
501
|
+
callstack.splice(indexMock, 1);
|
|
502
|
+
}
|
|
503
|
+
} else if (mock.type === "redirect" && !callstack.includes(mock.redirect)) {
|
|
504
|
+
return mock.redirect;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
queueMock(id, importer, factoryOrOptions) {
|
|
508
|
+
const mockType = getMockType(factoryOrOptions);
|
|
509
|
+
VitestMocker.pendingIds.push({
|
|
510
|
+
action: "mock",
|
|
511
|
+
id,
|
|
512
|
+
importer,
|
|
513
|
+
factory: typeof factoryOrOptions === "function" ? factoryOrOptions : undefined,
|
|
514
|
+
type: mockType
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
queueUnmock(id, importer) {
|
|
518
|
+
VitestMocker.pendingIds.push({
|
|
519
|
+
action: "unmock",
|
|
520
|
+
id,
|
|
521
|
+
importer
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
function getMockType(factoryOrOptions) {
|
|
526
|
+
if (!factoryOrOptions) {
|
|
527
|
+
return "automock";
|
|
528
|
+
}
|
|
529
|
+
if (typeof factoryOrOptions === "function") {
|
|
530
|
+
return "manual";
|
|
531
|
+
}
|
|
532
|
+
return factoryOrOptions.spy ? "autospy" : "automock";
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const normalizedDistDir = normalize$1(distDir);
|
|
536
|
+
const { readFileSync } = fs;
|
|
537
|
+
async function createVitestExecutor(options) {
|
|
538
|
+
const runner = new VitestExecutor(options);
|
|
539
|
+
await runner.executeId("/@vite/env");
|
|
540
|
+
await runner.mocker.initializeSpyModule();
|
|
541
|
+
return runner;
|
|
542
|
+
}
|
|
543
|
+
const externalizeMap = new Map();
|
|
544
|
+
const bareVitestRegexp = /^@?vitest(?:\/|$)/;
|
|
545
|
+
const dispose = [];
|
|
546
|
+
function listenForErrors(state) {
|
|
547
|
+
dispose.forEach((fn) => fn());
|
|
548
|
+
dispose.length = 0;
|
|
549
|
+
function catchError(err, type, event) {
|
|
550
|
+
const worker = state();
|
|
551
|
+
if (worker.current?.type === "test") {
|
|
552
|
+
const listeners = process.listeners(event);
|
|
553
|
+
if (listeners.length > 1) {
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
const error = processError(err);
|
|
558
|
+
if (!isPrimitive(error)) {
|
|
559
|
+
error.VITEST_TEST_NAME = worker.current?.type === "test" ? worker.current.name : undefined;
|
|
560
|
+
if (worker.filepath) {
|
|
561
|
+
error.VITEST_TEST_PATH = worker.filepath;
|
|
562
|
+
}
|
|
563
|
+
error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun;
|
|
564
|
+
}
|
|
565
|
+
state().rpc.onUnhandledError(error, type);
|
|
566
|
+
}
|
|
567
|
+
const uncaughtException = (e) => catchError(e, "Uncaught Exception", "uncaughtException");
|
|
568
|
+
const unhandledRejection = (e) => catchError(e, "Unhandled Rejection", "unhandledRejection");
|
|
569
|
+
process.on("uncaughtException", uncaughtException);
|
|
570
|
+
process.on("unhandledRejection", unhandledRejection);
|
|
571
|
+
dispose.push(() => {
|
|
572
|
+
process.off("uncaughtException", uncaughtException);
|
|
573
|
+
process.off("unhandledRejection", unhandledRejection);
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
async function startVitestExecutor(options) {
|
|
577
|
+
const state = () => globalThis.__vitest_worker__ || options.state;
|
|
578
|
+
const rpc = () => state().rpc;
|
|
579
|
+
process.exit = (code = process.exitCode || 0) => {
|
|
580
|
+
throw new Error(`process.exit unexpectedly called with "${code}"`);
|
|
581
|
+
};
|
|
582
|
+
listenForErrors(state);
|
|
583
|
+
const getTransformMode = () => {
|
|
584
|
+
return state().environment.transformMode ?? "ssr";
|
|
585
|
+
};
|
|
586
|
+
return await createVitestExecutor({
|
|
587
|
+
async fetchModule(id) {
|
|
588
|
+
if (externalizeMap.has(id)) {
|
|
589
|
+
return { externalize: externalizeMap.get(id) };
|
|
590
|
+
}
|
|
591
|
+
if (id.includes(distDir) || id.includes(normalizedDistDir)) {
|
|
592
|
+
const { path } = toFilePath(id, state().config.root);
|
|
593
|
+
const externalize = pathToFileURL(path).toString();
|
|
594
|
+
externalizeMap.set(id, externalize);
|
|
595
|
+
return { externalize };
|
|
596
|
+
}
|
|
597
|
+
if (bareVitestRegexp.test(id)) {
|
|
598
|
+
externalizeMap.set(id, id);
|
|
599
|
+
return { externalize: id };
|
|
600
|
+
}
|
|
601
|
+
const result = await rpc().fetch(id, getTransformMode());
|
|
602
|
+
if (result.id && !result.externalize) {
|
|
603
|
+
const code = readFileSync(result.id, "utf-8");
|
|
604
|
+
return { code };
|
|
605
|
+
}
|
|
606
|
+
return result;
|
|
607
|
+
},
|
|
608
|
+
resolveId(id, importer) {
|
|
609
|
+
return rpc().resolveId(id, importer, getTransformMode());
|
|
610
|
+
},
|
|
611
|
+
get moduleCache() {
|
|
612
|
+
return state().moduleCache;
|
|
613
|
+
},
|
|
614
|
+
get moduleExecutionInfo() {
|
|
615
|
+
return state().moduleExecutionInfo;
|
|
616
|
+
},
|
|
617
|
+
get interopDefault() {
|
|
618
|
+
return state().config.deps.interopDefault;
|
|
619
|
+
},
|
|
620
|
+
get moduleDirectories() {
|
|
621
|
+
return state().config.deps.moduleDirectories;
|
|
622
|
+
},
|
|
623
|
+
get root() {
|
|
624
|
+
return state().config.root;
|
|
625
|
+
},
|
|
626
|
+
get base() {
|
|
627
|
+
return state().config.base;
|
|
628
|
+
},
|
|
629
|
+
...options
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
function updateStyle(id, css) {
|
|
633
|
+
if (typeof document === "undefined") {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
const element = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
637
|
+
if (element) {
|
|
638
|
+
element.textContent = css;
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const head = document.querySelector("head");
|
|
642
|
+
const style = document.createElement("style");
|
|
643
|
+
style.setAttribute("type", "text/css");
|
|
644
|
+
style.setAttribute("data-vite-dev-id", id);
|
|
645
|
+
style.textContent = css;
|
|
646
|
+
head?.appendChild(style);
|
|
647
|
+
}
|
|
648
|
+
function removeStyle(id) {
|
|
649
|
+
if (typeof document === "undefined") {
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
const sheet = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
653
|
+
if (sheet) {
|
|
654
|
+
document.head.removeChild(sheet);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
function getDefaultRequestStubs(context) {
|
|
658
|
+
if (!context) {
|
|
659
|
+
const clientStub = {
|
|
660
|
+
...DEFAULT_REQUEST_STUBS["@vite/client"],
|
|
661
|
+
updateStyle,
|
|
662
|
+
removeStyle
|
|
663
|
+
};
|
|
664
|
+
return {
|
|
665
|
+
"/@vite/client": clientStub,
|
|
666
|
+
"@vite/client": clientStub
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
const clientStub = vm.runInContext(`(defaultClient) => ({ ...defaultClient, updateStyle: ${updateStyle.toString()}, removeStyle: ${removeStyle.toString()} })`, context)(DEFAULT_REQUEST_STUBS["@vite/client"]);
|
|
670
|
+
return {
|
|
671
|
+
"/@vite/client": clientStub,
|
|
672
|
+
"@vite/client": clientStub
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
class VitestExecutor extends ViteNodeRunner {
|
|
676
|
+
mocker;
|
|
677
|
+
externalModules;
|
|
678
|
+
primitives;
|
|
679
|
+
constructor(options) {
|
|
680
|
+
super({
|
|
681
|
+
...options,
|
|
682
|
+
interopDefault: options.context ? false : options.interopDefault
|
|
683
|
+
});
|
|
684
|
+
this.options = options;
|
|
685
|
+
this.mocker = new VitestMocker(this);
|
|
686
|
+
if (!options.context) {
|
|
687
|
+
Object.defineProperty(globalThis, "__vitest_mocker__", {
|
|
688
|
+
value: this.mocker,
|
|
689
|
+
writable: true,
|
|
690
|
+
configurable: true
|
|
691
|
+
});
|
|
692
|
+
this.primitives = {
|
|
693
|
+
Object,
|
|
694
|
+
Reflect,
|
|
695
|
+
Symbol
|
|
696
|
+
};
|
|
697
|
+
} else if (options.externalModulesExecutor) {
|
|
698
|
+
this.primitives = vm.runInContext("({ Object, Reflect, Symbol })", options.context);
|
|
699
|
+
this.externalModules = options.externalModulesExecutor;
|
|
700
|
+
} else {
|
|
701
|
+
throw new Error("When context is provided, externalModulesExecutor must be provided as well.");
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
getContextPrimitives() {
|
|
705
|
+
return this.primitives;
|
|
706
|
+
}
|
|
707
|
+
get state() {
|
|
708
|
+
return globalThis.__vitest_worker__ || this.options.state;
|
|
709
|
+
}
|
|
710
|
+
get moduleExecutionInfo() {
|
|
711
|
+
return this.options.moduleExecutionInfo;
|
|
712
|
+
}
|
|
713
|
+
shouldResolveId(id, _importee) {
|
|
714
|
+
if (isInternalRequest(id) || id.startsWith("data:")) {
|
|
715
|
+
return false;
|
|
716
|
+
}
|
|
717
|
+
const transformMode = this.state.environment?.transformMode ?? "ssr";
|
|
718
|
+
return transformMode === "ssr" ? !isNodeBuiltin$1(id) : !id.startsWith("node:");
|
|
719
|
+
}
|
|
720
|
+
async originalResolveUrl(id, importer) {
|
|
721
|
+
return super.resolveUrl(id, importer);
|
|
722
|
+
}
|
|
723
|
+
async resolveUrl(id, importer) {
|
|
724
|
+
if (VitestMocker.pendingIds.length) {
|
|
725
|
+
await this.mocker.resolveMocks();
|
|
726
|
+
}
|
|
727
|
+
if (importer && importer.startsWith("mock:")) {
|
|
728
|
+
importer = importer.slice(5);
|
|
729
|
+
}
|
|
730
|
+
try {
|
|
731
|
+
return await super.resolveUrl(id, importer);
|
|
732
|
+
} catch (error) {
|
|
733
|
+
if (error.code === "ERR_MODULE_NOT_FOUND") {
|
|
734
|
+
const { id } = error[Symbol.for("vitest.error.not_found.data")];
|
|
735
|
+
const path = this.mocker.normalizePath(id);
|
|
736
|
+
const mock = this.mocker.getDependencyMock(path);
|
|
737
|
+
if (mock !== undefined) {
|
|
738
|
+
return [id, id];
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
throw error;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
async runModule(context, transformed) {
|
|
745
|
+
const vmContext = this.options.context;
|
|
746
|
+
if (!vmContext || !this.externalModules) {
|
|
747
|
+
return super.runModule(context, transformed);
|
|
748
|
+
}
|
|
749
|
+
const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
|
|
750
|
+
const code = `${codeDefinition}${transformed}\n}}`;
|
|
751
|
+
const options = {
|
|
752
|
+
filename: context.__filename,
|
|
753
|
+
lineOffset: 0,
|
|
754
|
+
columnOffset: -codeDefinition.length
|
|
755
|
+
};
|
|
756
|
+
this.options.moduleExecutionInfo?.set(options.filename, { startOffset: codeDefinition.length });
|
|
757
|
+
const fn = vm.runInContext(code, vmContext, {
|
|
758
|
+
...options,
|
|
759
|
+
importModuleDynamically: this.externalModules.importModuleDynamically
|
|
760
|
+
});
|
|
761
|
+
await fn(...Object.values(context));
|
|
762
|
+
}
|
|
763
|
+
async importExternalModule(path) {
|
|
764
|
+
if (this.externalModules) {
|
|
765
|
+
return this.externalModules.import(path);
|
|
766
|
+
}
|
|
767
|
+
return super.importExternalModule(path);
|
|
768
|
+
}
|
|
769
|
+
async dependencyRequest(id, fsPath, callstack) {
|
|
770
|
+
const mocked = await this.mocker.requestWithMock(fsPath, callstack);
|
|
771
|
+
if (typeof mocked === "string") {
|
|
772
|
+
return super.dependencyRequest(mocked, mocked, callstack);
|
|
773
|
+
}
|
|
774
|
+
if (mocked && typeof mocked === "object") {
|
|
775
|
+
return mocked;
|
|
776
|
+
}
|
|
777
|
+
return super.dependencyRequest(id, fsPath, callstack);
|
|
778
|
+
}
|
|
779
|
+
prepareContext(context) {
|
|
780
|
+
if (this.state.filepath && normalize$1(this.state.filepath) === normalize$1(context.__filename)) {
|
|
781
|
+
const globalNamespace = this.options.context || globalThis;
|
|
782
|
+
Object.defineProperty(context.__vite_ssr_import_meta__, "vitest", { get: () => globalNamespace.__vitest_index__ });
|
|
783
|
+
}
|
|
784
|
+
if (this.options.context && this.externalModules) {
|
|
785
|
+
context.require = this.externalModules.createRequire(context.__filename);
|
|
786
|
+
}
|
|
787
|
+
return context;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
export { VitestExecutor as V, getDefaultRequestStubs as g, startVitestExecutor as s };
|