vitest 4.0.0-beta.4 → 4.0.0-beta.6
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 +1 -1
- package/dist/browser.d.ts +8 -9
- package/dist/browser.js +3 -2
- package/dist/chunks/base.BXI97p6t.js +39 -0
- package/dist/chunks/{benchmark.CYdenmiT.js → benchmark.UW6Ezvxy.js} +6 -8
- package/dist/chunks/{browser.d.BRP8scJf.d.ts → browser.d.Cawq_X_N.d.ts} +1 -1
- package/dist/chunks/{cac.CY0IAxC4.js → cac.WE-urWw5.js} +38 -115
- package/dist/chunks/{cli-api.B8xRY9Zt.js → cli-api.CZz3evYC.js} +931 -1439
- package/dist/chunks/{config.d.DZo8c7fw.d.ts → config.d.CKNVOKm0.d.ts} +3 -8
- package/dist/chunks/{console.DoJHFxmj.js → console.B0quX7yH.js} +32 -68
- package/dist/chunks/{constants.CXzqaLmq.js → constants.D_Q9UYh-.js} +1 -6
- package/dist/chunks/{coverage.C84l9G-M.js → coverage.BPRS6xgn.js} +395 -665
- package/dist/chunks/{coverage.DVF1vEu8.js → coverage.D_JHT54q.js} +2 -2
- package/dist/chunks/{coverage.d.CNYjU4GF.d.ts → coverage.d.BZtK59WP.d.ts} +7 -5
- package/dist/chunks/{creator.yfA2ExGt.js → creator.KEg6n5IC.js} +29 -75
- package/dist/chunks/{date.Bq6ZW5rf.js → date.-jtEtIeV.js} +6 -17
- package/dist/chunks/{environment.d.Bhm9oc0v.d.ts → environment.d.2fYMoz3o.d.ts} +26 -4
- package/dist/chunks/{git.BVQ8w_Sw.js → git.BFNcloKD.js} +1 -2
- package/dist/chunks/{global.d.DAhT2emn.d.ts → global.d.K6uBQHzY.d.ts} +1 -1
- package/dist/chunks/{globals.Dgo-vS5G.js → globals.lgsmH00r.js} +7 -6
- package/dist/chunks/{index.D3SKT3tv.js → index.7w0eqmYM.js} +14 -24
- package/dist/chunks/{index.D1_MsKEt.js → index.AR8aAkCC.js} +4 -2
- package/dist/chunks/{index.CmSc2RE5.js → index.BG0gqZH-.js} +43 -106
- package/dist/chunks/{index.CtUvr1c8.js → index.CsFXYRkW.js} +27 -46
- package/dist/chunks/{index.Bz6b0Ib7.js → index.VNI-1z5c.js} +276 -604
- package/dist/chunks/{inspector.C914Efll.js → inspector.CvQD-Nie.js} +10 -25
- package/dist/chunks/moduleRunner.d.8kKUsuDg.d.ts +202 -0
- package/dist/chunks/moduleTransport.I-bgQy0S.js +19 -0
- package/dist/chunks/{node.fjCdwEIl.js → node.BOqcT2jW.js} +1 -1
- package/dist/chunks/{plugin.d.CLhMcYdD.d.ts → plugin.d.DuiQJfUL.d.ts} +1 -1
- package/dist/chunks/{reporters.d.DWg40D2B.d.ts → reporters.d.CqR9-CDJ.d.ts} +52 -101
- package/dist/chunks/resolver.Bx6lE0iq.js +119 -0
- package/dist/chunks/{rpc.jnQO9F8a.js → rpc.RpPylpp0.js} +7 -21
- package/dist/chunks/runBaseTests.D6sfuWBM.js +99 -0
- package/dist/chunks/{setup-common.Ebx5x0eP.js → setup-common.hLGRxhC8.js} +15 -27
- package/dist/chunks/startModuleRunner.C8TW8zTN.js +655 -0
- package/dist/chunks/{typechecker.CMNPqJOo.js → typechecker.Cd1wvxUM.js} +97 -209
- package/dist/chunks/{utils.CcGm2cd1.js → utils.C2YI6McM.js} +4 -13
- package/dist/chunks/{utils.XdZDrNZV.js → utils.C7__0Iv5.js} +7 -17
- package/dist/chunks/{vi.CA0EPI9Y.js → vi.BfdOiD4j.js} +116 -269
- package/dist/chunks/{vm.BUnLJt_P.js → vm.BHBje7cC.js} +101 -225
- package/dist/chunks/{worker.d.zjyR34Pb.d.ts → worker.d.D9QWnzAe.d.ts} +16 -13
- package/dist/chunks/{worker.d.C-1AbnVe.d.ts → worker.d.Db-UVmXc.d.ts} +1 -1
- package/dist/cli.js +4 -4
- package/dist/config.cjs +3 -9
- package/dist/config.d.ts +10 -12
- package/dist/config.js +1 -1
- package/dist/coverage.d.ts +10 -11
- package/dist/coverage.js +5 -6
- package/dist/environments.d.ts +2 -2
- package/dist/environments.js +1 -1
- package/dist/index.d.ts +10 -9
- package/dist/index.js +6 -5
- package/dist/module-evaluator.d.ts +12 -0
- package/dist/module-evaluator.js +276 -0
- package/dist/module-runner.js +15 -0
- package/dist/node.d.ts +12 -13
- package/dist/node.js +19 -24
- package/dist/reporters.d.ts +7 -8
- package/dist/reporters.js +3 -3
- package/dist/runners.d.ts +3 -3
- package/dist/runners.js +35 -57
- package/dist/snapshot.js +2 -2
- package/dist/suite.js +2 -2
- package/dist/worker.js +82 -45
- package/dist/workers/forks.js +11 -10
- package/dist/workers/runVmTests.js +27 -46
- package/dist/workers/threads.js +11 -10
- package/dist/workers/vmForks.js +11 -10
- package/dist/workers/vmThreads.js +11 -10
- package/dist/workers.d.ts +5 -4
- package/dist/workers.js +17 -16
- package/package.json +22 -17
- package/dist/chunks/base.BaCDDRPG.js +0 -38
- package/dist/chunks/execute.Dt-pCVcL.js +0 -708
- package/dist/chunks/runBaseTests.DBVVLMSb.js +0 -129
- package/dist/execute.d.ts +0 -148
- package/dist/execute.js +0 -13
|
@@ -1,708 +0,0 @@
|
|
|
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) return input;
|
|
17
|
-
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
18
|
-
}
|
|
19
|
-
const _UNC_REGEX = /^[/\\]{2}/;
|
|
20
|
-
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
21
|
-
const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
|
|
22
|
-
const _EXTNAME_RE = /.(\.[^./]+|\.)$/;
|
|
23
|
-
const normalize = function(path) {
|
|
24
|
-
if (path.length === 0) return ".";
|
|
25
|
-
path = normalizeWindowsPath(path);
|
|
26
|
-
const isUNCPath = path.match(_UNC_REGEX);
|
|
27
|
-
const isPathAbsolute = isAbsolute(path);
|
|
28
|
-
const trailingSeparator = path[path.length - 1] === "/";
|
|
29
|
-
path = normalizeString(path, !isPathAbsolute);
|
|
30
|
-
if (path.length === 0) {
|
|
31
|
-
if (isPathAbsolute) return "/";
|
|
32
|
-
return trailingSeparator ? "./" : ".";
|
|
33
|
-
}
|
|
34
|
-
if (trailingSeparator) path += "/";
|
|
35
|
-
if (_DRIVE_LETTER_RE.test(path)) path += "/";
|
|
36
|
-
if (isUNCPath) {
|
|
37
|
-
if (!isPathAbsolute) return `//./${path}`;
|
|
38
|
-
return `//${path}`;
|
|
39
|
-
}
|
|
40
|
-
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
|
|
41
|
-
};
|
|
42
|
-
const join = function(...segments) {
|
|
43
|
-
let path = "";
|
|
44
|
-
for (const seg of segments) {
|
|
45
|
-
if (!seg) continue;
|
|
46
|
-
if (path.length > 0) {
|
|
47
|
-
const pathTrailing = path[path.length - 1] === "/";
|
|
48
|
-
const segLeading = seg[0] === "/";
|
|
49
|
-
const both = pathTrailing && segLeading;
|
|
50
|
-
if (both) path += seg.slice(1);
|
|
51
|
-
else path += pathTrailing || segLeading ? seg : `/${seg}`;
|
|
52
|
-
} else path += seg;
|
|
53
|
-
}
|
|
54
|
-
return normalize(path);
|
|
55
|
-
};
|
|
56
|
-
function cwd() {
|
|
57
|
-
if (typeof process !== "undefined" && typeof process.cwd === "function") return process.cwd().replace(/\\/g, "/");
|
|
58
|
-
return "/";
|
|
59
|
-
}
|
|
60
|
-
const resolve = function(...arguments_) {
|
|
61
|
-
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
|
|
62
|
-
let resolvedPath = "";
|
|
63
|
-
let resolvedAbsolute = false;
|
|
64
|
-
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
65
|
-
const path = index >= 0 ? arguments_[index] : cwd();
|
|
66
|
-
if (!path || path.length === 0) continue;
|
|
67
|
-
resolvedPath = `${path}/${resolvedPath}`;
|
|
68
|
-
resolvedAbsolute = isAbsolute(path);
|
|
69
|
-
}
|
|
70
|
-
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
71
|
-
if (resolvedAbsolute && !isAbsolute(resolvedPath)) return `/${resolvedPath}`;
|
|
72
|
-
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
73
|
-
};
|
|
74
|
-
function normalizeString(path, allowAboveRoot) {
|
|
75
|
-
let res = "";
|
|
76
|
-
let lastSegmentLength = 0;
|
|
77
|
-
let lastSlash = -1;
|
|
78
|
-
let dots = 0;
|
|
79
|
-
let char = null;
|
|
80
|
-
for (let index = 0; index <= path.length; ++index) {
|
|
81
|
-
if (index < path.length) char = path[index];
|
|
82
|
-
else if (char === "/") break;
|
|
83
|
-
else char = "/";
|
|
84
|
-
if (char === "/") {
|
|
85
|
-
if (lastSlash === index - 1 || dots === 1);
|
|
86
|
-
else if (dots === 2) {
|
|
87
|
-
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
88
|
-
if (res.length > 2) {
|
|
89
|
-
const lastSlashIndex = res.lastIndexOf("/");
|
|
90
|
-
if (lastSlashIndex === -1) {
|
|
91
|
-
res = "";
|
|
92
|
-
lastSegmentLength = 0;
|
|
93
|
-
} else {
|
|
94
|
-
res = res.slice(0, lastSlashIndex);
|
|
95
|
-
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
96
|
-
}
|
|
97
|
-
lastSlash = index;
|
|
98
|
-
dots = 0;
|
|
99
|
-
continue;
|
|
100
|
-
} else if (res.length > 0) {
|
|
101
|
-
res = "";
|
|
102
|
-
lastSegmentLength = 0;
|
|
103
|
-
lastSlash = index;
|
|
104
|
-
dots = 0;
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (allowAboveRoot) {
|
|
109
|
-
res += res.length > 0 ? "/.." : "..";
|
|
110
|
-
lastSegmentLength = 2;
|
|
111
|
-
}
|
|
112
|
-
} else {
|
|
113
|
-
if (res.length > 0) res += `/${path.slice(lastSlash + 1, index)}`;
|
|
114
|
-
else res = path.slice(lastSlash + 1, index);
|
|
115
|
-
lastSegmentLength = index - lastSlash - 1;
|
|
116
|
-
}
|
|
117
|
-
lastSlash = index;
|
|
118
|
-
dots = 0;
|
|
119
|
-
} else if (char === "." && dots !== -1) ++dots;
|
|
120
|
-
else dots = -1;
|
|
121
|
-
}
|
|
122
|
-
return res;
|
|
123
|
-
}
|
|
124
|
-
const isAbsolute = function(p) {
|
|
125
|
-
return _IS_ABSOLUTE_RE.test(p);
|
|
126
|
-
};
|
|
127
|
-
const extname = function(p) {
|
|
128
|
-
if (p === "..") return "";
|
|
129
|
-
const match = _EXTNAME_RE.exec(normalizeWindowsPath(p));
|
|
130
|
-
return match && match[1] || "";
|
|
131
|
-
};
|
|
132
|
-
const dirname = function(p) {
|
|
133
|
-
const segments = normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1);
|
|
134
|
-
if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0])) segments[0] += "/";
|
|
135
|
-
return segments.join("/") || (isAbsolute(p) ? "/" : ".");
|
|
136
|
-
};
|
|
137
|
-
const basename = function(p, extension) {
|
|
138
|
-
const segments = normalizeWindowsPath(p).split("/");
|
|
139
|
-
let lastSegment = "";
|
|
140
|
-
for (let i = segments.length - 1; i >= 0; i--) {
|
|
141
|
-
const val = segments[i];
|
|
142
|
-
if (val) {
|
|
143
|
-
lastSegment = val;
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return extension && lastSegment.endsWith(extension) ? lastSegment.slice(0, -extension.length) : lastSegment;
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
const { existsSync, readdirSync, statSync } = fs;
|
|
151
|
-
function findMockRedirect(root, mockPath, external) {
|
|
152
|
-
const path = external || mockPath;
|
|
153
|
-
// it's a node_module alias
|
|
154
|
-
// all mocks should be inside <root>/__mocks__
|
|
155
|
-
if (external || isNodeBuiltin(mockPath) || !existsSync(mockPath)) {
|
|
156
|
-
const mockDirname = dirname(path);
|
|
157
|
-
const mockFolder = join(root, "__mocks__", mockDirname);
|
|
158
|
-
if (!existsSync(mockFolder)) return null;
|
|
159
|
-
const baseOriginal = basename(path);
|
|
160
|
-
function findFile(mockFolder, baseOriginal) {
|
|
161
|
-
const files = readdirSync(mockFolder);
|
|
162
|
-
for (const file of files) {
|
|
163
|
-
const baseFile = basename(file, extname(file));
|
|
164
|
-
if (baseFile === baseOriginal) {
|
|
165
|
-
const path = resolve(mockFolder, file);
|
|
166
|
-
// if the same name, return the file
|
|
167
|
-
if (statSync(path).isFile()) return path;
|
|
168
|
-
else {
|
|
169
|
-
// find folder/index.{js,ts}
|
|
170
|
-
const indexFile = findFile(path, "index");
|
|
171
|
-
if (indexFile) return indexFile;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
return findFile(mockFolder, baseOriginal);
|
|
178
|
-
}
|
|
179
|
-
const dir = dirname(path);
|
|
180
|
-
const baseId = basename(path);
|
|
181
|
-
const fullPath = resolve(dir, "__mocks__", baseId);
|
|
182
|
-
return existsSync(fullPath) ? fullPath : null;
|
|
183
|
-
}
|
|
184
|
-
const builtins = new Set([
|
|
185
|
-
...builtinModules,
|
|
186
|
-
"assert/strict",
|
|
187
|
-
"diagnostics_channel",
|
|
188
|
-
"dns/promises",
|
|
189
|
-
"fs/promises",
|
|
190
|
-
"path/posix",
|
|
191
|
-
"path/win32",
|
|
192
|
-
"readline/promises",
|
|
193
|
-
"stream/consumers",
|
|
194
|
-
"stream/promises",
|
|
195
|
-
"stream/web",
|
|
196
|
-
"timers/promises",
|
|
197
|
-
"util/types",
|
|
198
|
-
"wasi"
|
|
199
|
-
]);
|
|
200
|
-
// https://nodejs.org/api/modules.html#built-in-modules-with-mandatory-node-prefix
|
|
201
|
-
const prefixedBuiltins = new Set([
|
|
202
|
-
"node:sea",
|
|
203
|
-
"node:sqlite",
|
|
204
|
-
"node:test",
|
|
205
|
-
"node:test/reporters"
|
|
206
|
-
]);
|
|
207
|
-
const NODE_BUILTIN_NAMESPACE = "node:";
|
|
208
|
-
function isNodeBuiltin(id) {
|
|
209
|
-
if (prefixedBuiltins.has(id)) return true;
|
|
210
|
-
return builtins.has(id.startsWith(NODE_BUILTIN_NAMESPACE) ? id.slice(5) : id);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const spyModulePath = resolve$1(distDir, "spy.js");
|
|
214
|
-
class VitestMocker {
|
|
215
|
-
static pendingIds = [];
|
|
216
|
-
spyModule;
|
|
217
|
-
primitives;
|
|
218
|
-
filterPublicKeys;
|
|
219
|
-
registries = /* @__PURE__ */ new Map();
|
|
220
|
-
mockContext = { callstack: null };
|
|
221
|
-
constructor(executor) {
|
|
222
|
-
this.executor = executor;
|
|
223
|
-
const context = this.executor.options.context;
|
|
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
|
-
};
|
|
234
|
-
const Symbol = this.primitives.Symbol;
|
|
235
|
-
this.filterPublicKeys = [
|
|
236
|
-
"__esModule",
|
|
237
|
-
Symbol.asyncIterator,
|
|
238
|
-
Symbol.hasInstance,
|
|
239
|
-
Symbol.isConcatSpreadable,
|
|
240
|
-
Symbol.iterator,
|
|
241
|
-
Symbol.match,
|
|
242
|
-
Symbol.matchAll,
|
|
243
|
-
Symbol.replace,
|
|
244
|
-
Symbol.search,
|
|
245
|
-
Symbol.split,
|
|
246
|
-
Symbol.species,
|
|
247
|
-
Symbol.toPrimitive,
|
|
248
|
-
Symbol.toStringTag,
|
|
249
|
-
Symbol.unscopables
|
|
250
|
-
];
|
|
251
|
-
}
|
|
252
|
-
get root() {
|
|
253
|
-
return this.executor.options.root;
|
|
254
|
-
}
|
|
255
|
-
get moduleCache() {
|
|
256
|
-
return this.executor.moduleCache;
|
|
257
|
-
}
|
|
258
|
-
get moduleDirectories() {
|
|
259
|
-
return this.executor.options.moduleDirectories || [];
|
|
260
|
-
}
|
|
261
|
-
async initializeSpyModule() {
|
|
262
|
-
this.spyModule = await this.executor.executeId(spyModulePath);
|
|
263
|
-
}
|
|
264
|
-
getMockerRegistry() {
|
|
265
|
-
const suite = this.getSuiteFilepath();
|
|
266
|
-
if (!this.registries.has(suite)) this.registries.set(suite, new MockerRegistry());
|
|
267
|
-
return this.registries.get(suite);
|
|
268
|
-
}
|
|
269
|
-
reset() {
|
|
270
|
-
this.registries.clear();
|
|
271
|
-
}
|
|
272
|
-
deleteCachedItem(id) {
|
|
273
|
-
const mockId = this.getMockPath(id);
|
|
274
|
-
if (this.moduleCache.has(mockId)) this.moduleCache.delete(mockId);
|
|
275
|
-
}
|
|
276
|
-
isModuleDirectory(path) {
|
|
277
|
-
return this.moduleDirectories.some((dir) => path.includes(dir));
|
|
278
|
-
}
|
|
279
|
-
getSuiteFilepath() {
|
|
280
|
-
return this.executor.state.filepath || "global";
|
|
281
|
-
}
|
|
282
|
-
createError(message, codeFrame) {
|
|
283
|
-
const Error = this.primitives.Error;
|
|
284
|
-
const error = new Error(message);
|
|
285
|
-
Object.assign(error, { codeFrame });
|
|
286
|
-
return error;
|
|
287
|
-
}
|
|
288
|
-
async resolvePath(rawId, importer) {
|
|
289
|
-
let id;
|
|
290
|
-
let fsPath;
|
|
291
|
-
try {
|
|
292
|
-
[id, fsPath] = await this.executor.originalResolveUrl(rawId, importer);
|
|
293
|
-
} catch (error) {
|
|
294
|
-
// it's allowed to mock unresolved modules
|
|
295
|
-
if (error.code === "ERR_MODULE_NOT_FOUND") {
|
|
296
|
-
const { id: unresolvedId } = error[Symbol.for("vitest.error.not_found.data")];
|
|
297
|
-
id = unresolvedId;
|
|
298
|
-
fsPath = unresolvedId;
|
|
299
|
-
} else throw error;
|
|
300
|
-
}
|
|
301
|
-
// external is node_module or unresolved module
|
|
302
|
-
// for example, some people mock "vscode" and don't have it installed
|
|
303
|
-
const external = !isAbsolute$1(fsPath) || this.isModuleDirectory(fsPath) ? rawId : null;
|
|
304
|
-
return {
|
|
305
|
-
id,
|
|
306
|
-
fsPath,
|
|
307
|
-
external: external ? this.normalizePath(external) : external
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
async resolveMocks() {
|
|
311
|
-
if (!VitestMocker.pendingIds.length) return;
|
|
312
|
-
await Promise.all(VitestMocker.pendingIds.map(async (mock) => {
|
|
313
|
-
const { fsPath, external } = await this.resolvePath(mock.id, mock.importer);
|
|
314
|
-
if (mock.action === "unmock") this.unmockPath(fsPath);
|
|
315
|
-
if (mock.action === "mock") this.mockPath(mock.id, fsPath, external, mock.type, mock.factory);
|
|
316
|
-
}));
|
|
317
|
-
VitestMocker.pendingIds = [];
|
|
318
|
-
}
|
|
319
|
-
async callFunctionMock(dep, mock) {
|
|
320
|
-
const cached = this.moduleCache.get(dep)?.exports;
|
|
321
|
-
if (cached) return cached;
|
|
322
|
-
const exports = await mock.resolve();
|
|
323
|
-
const moduleExports = new Proxy(exports, { get: (target, prop) => {
|
|
324
|
-
const val = target[prop];
|
|
325
|
-
// 'then' can exist on non-Promise objects, need nested instanceof check for logic to work
|
|
326
|
-
if (prop === "then") {
|
|
327
|
-
if (target instanceof Promise) return target.then.bind(target);
|
|
328
|
-
} else if (!(prop in target)) {
|
|
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) => {
|
|
333
|
-
const actual = await importOriginal()
|
|
334
|
-
return {
|
|
335
|
-
...actual,
|
|
336
|
-
// your mocked methods
|
|
337
|
-
}
|
|
338
|
-
})`));
|
|
339
|
-
}
|
|
340
|
-
return val;
|
|
341
|
-
} });
|
|
342
|
-
this.moduleCache.set(dep, { exports: moduleExports });
|
|
343
|
-
return moduleExports;
|
|
344
|
-
}
|
|
345
|
-
// public method to avoid circular dependency
|
|
346
|
-
getMockContext() {
|
|
347
|
-
return this.mockContext;
|
|
348
|
-
}
|
|
349
|
-
// path used to store mocked dependencies
|
|
350
|
-
getMockPath(dep) {
|
|
351
|
-
return `mock:${dep}`;
|
|
352
|
-
}
|
|
353
|
-
getDependencyMock(id) {
|
|
354
|
-
const registry = this.getMockerRegistry();
|
|
355
|
-
return registry.get(id);
|
|
356
|
-
}
|
|
357
|
-
normalizePath(path) {
|
|
358
|
-
return this.moduleCache.normalizePath(path);
|
|
359
|
-
}
|
|
360
|
-
resolveMockPath(mockPath, external) {
|
|
361
|
-
return findMockRedirect(this.root, mockPath, external);
|
|
362
|
-
}
|
|
363
|
-
mockObject(object, mockExports = {}, behavior = "automock") {
|
|
364
|
-
const spyOn = this.spyModule?.spyOn;
|
|
365
|
-
if (!spyOn) throw this.createError("[vitest] `spyModule` is not defined. This is a Vitest error. Please open a new issue with reproduction.");
|
|
366
|
-
return mockObject({
|
|
367
|
-
globalConstructors: this.primitives,
|
|
368
|
-
spyOn,
|
|
369
|
-
type: behavior
|
|
370
|
-
}, object, mockExports);
|
|
371
|
-
}
|
|
372
|
-
unmockPath(path) {
|
|
373
|
-
const registry = this.getMockerRegistry();
|
|
374
|
-
const id = this.normalizePath(path);
|
|
375
|
-
registry.delete(id);
|
|
376
|
-
this.deleteCachedItem(id);
|
|
377
|
-
}
|
|
378
|
-
mockPath(originalId, path, external, mockType, factory) {
|
|
379
|
-
const registry = this.getMockerRegistry();
|
|
380
|
-
const id = this.normalizePath(path);
|
|
381
|
-
if (mockType === "manual") registry.register("manual", originalId, id, id, factory);
|
|
382
|
-
else if (mockType === "autospy") registry.register("autospy", originalId, id, id);
|
|
383
|
-
else {
|
|
384
|
-
const redirect = this.resolveMockPath(id, external);
|
|
385
|
-
if (redirect) registry.register("redirect", originalId, id, id, redirect);
|
|
386
|
-
else registry.register("automock", originalId, id, id);
|
|
387
|
-
}
|
|
388
|
-
// every time the mock is registered, we remove the previous one from the cache
|
|
389
|
-
this.deleteCachedItem(id);
|
|
390
|
-
}
|
|
391
|
-
async importActual(rawId, importer, callstack) {
|
|
392
|
-
const { id, fsPath } = await this.resolvePath(rawId, importer);
|
|
393
|
-
const result = await this.executor.cachedRequest(id, fsPath, callstack || [importer]);
|
|
394
|
-
return result;
|
|
395
|
-
}
|
|
396
|
-
async importMock(rawId, importee) {
|
|
397
|
-
const { id, fsPath, external } = await this.resolvePath(rawId, importee);
|
|
398
|
-
const normalizedId = this.normalizePath(fsPath);
|
|
399
|
-
let mock = this.getDependencyMock(normalizedId);
|
|
400
|
-
if (!mock) {
|
|
401
|
-
const redirect = this.resolveMockPath(normalizedId, external);
|
|
402
|
-
if (redirect) mock = new RedirectedModule(rawId, normalizedId, normalizedId, redirect);
|
|
403
|
-
else mock = new AutomockedModule(rawId, normalizedId, normalizedId);
|
|
404
|
-
}
|
|
405
|
-
if (mock.type === "automock" || mock.type === "autospy") {
|
|
406
|
-
const mod = await this.executor.cachedRequest(id, fsPath, [importee]);
|
|
407
|
-
return this.mockObject(mod, {}, mock.type);
|
|
408
|
-
}
|
|
409
|
-
if (mock.type === "manual") return this.callFunctionMock(fsPath, mock);
|
|
410
|
-
return this.executor.dependencyRequest(mock.redirect, mock.redirect, [importee]);
|
|
411
|
-
}
|
|
412
|
-
async requestWithMock(url, callstack) {
|
|
413
|
-
const id = this.normalizePath(url);
|
|
414
|
-
const mock = this.getDependencyMock(id);
|
|
415
|
-
if (!mock) return;
|
|
416
|
-
const mockPath = this.getMockPath(id);
|
|
417
|
-
if (mock.type === "automock" || mock.type === "autospy") {
|
|
418
|
-
const cache = this.moduleCache.get(mockPath);
|
|
419
|
-
if (cache.exports) return cache.exports;
|
|
420
|
-
const exports = {};
|
|
421
|
-
// Assign the empty exports object early to allow for cycles to work. The object will be filled by mockObject()
|
|
422
|
-
this.moduleCache.set(mockPath, { exports });
|
|
423
|
-
const mod = await this.executor.directRequest(url, url, callstack);
|
|
424
|
-
this.mockObject(mod, exports, mock.type);
|
|
425
|
-
return exports;
|
|
426
|
-
}
|
|
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);
|
|
438
|
-
}
|
|
439
|
-
else if (mock.type === "redirect" && !callstack.includes(mock.redirect)) return mock.redirect;
|
|
440
|
-
}
|
|
441
|
-
queueMock(id, importer, factoryOrOptions) {
|
|
442
|
-
const mockType = getMockType(factoryOrOptions);
|
|
443
|
-
VitestMocker.pendingIds.push({
|
|
444
|
-
action: "mock",
|
|
445
|
-
id,
|
|
446
|
-
importer,
|
|
447
|
-
factory: typeof factoryOrOptions === "function" ? factoryOrOptions : void 0,
|
|
448
|
-
type: mockType
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
queueUnmock(id, importer) {
|
|
452
|
-
VitestMocker.pendingIds.push({
|
|
453
|
-
action: "unmock",
|
|
454
|
-
id,
|
|
455
|
-
importer
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
function getMockType(factoryOrOptions) {
|
|
460
|
-
if (!factoryOrOptions) return "automock";
|
|
461
|
-
if (typeof factoryOrOptions === "function") return "manual";
|
|
462
|
-
return factoryOrOptions.spy ? "autospy" : "automock";
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
const normalizedDistDir = normalize$1(distDir);
|
|
466
|
-
const { readFileSync } = fs;
|
|
467
|
-
async function createVitestExecutor(options) {
|
|
468
|
-
const runner = new VitestExecutor(options);
|
|
469
|
-
await runner.executeId("/@vite/env");
|
|
470
|
-
await runner.mocker.initializeSpyModule();
|
|
471
|
-
return runner;
|
|
472
|
-
}
|
|
473
|
-
const externalizeMap = /* @__PURE__ */ new Map();
|
|
474
|
-
const bareVitestRegexp = /^@?vitest(?:\/|$)/;
|
|
475
|
-
const dispose = [];
|
|
476
|
-
function listenForErrors(state) {
|
|
477
|
-
dispose.forEach((fn) => fn());
|
|
478
|
-
dispose.length = 0;
|
|
479
|
-
function catchError(err, type, event) {
|
|
480
|
-
const worker = state();
|
|
481
|
-
const listeners = process.listeners(event);
|
|
482
|
-
// if there is another listener, assume that it's handled by user code
|
|
483
|
-
// one is Vitest's own listener
|
|
484
|
-
if (listeners.length > 1) return;
|
|
485
|
-
const error = processError(err);
|
|
486
|
-
if (!isPrimitive(error)) {
|
|
487
|
-
error.VITEST_TEST_NAME = worker.current?.type === "test" ? worker.current.name : void 0;
|
|
488
|
-
if (worker.filepath) error.VITEST_TEST_PATH = worker.filepath;
|
|
489
|
-
error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun;
|
|
490
|
-
}
|
|
491
|
-
state().rpc.onUnhandledError(error, type);
|
|
492
|
-
}
|
|
493
|
-
const uncaughtException = (e) => catchError(e, "Uncaught Exception", "uncaughtException");
|
|
494
|
-
const unhandledRejection = (e) => catchError(e, "Unhandled Rejection", "unhandledRejection");
|
|
495
|
-
process.on("uncaughtException", uncaughtException);
|
|
496
|
-
process.on("unhandledRejection", unhandledRejection);
|
|
497
|
-
dispose.push(() => {
|
|
498
|
-
process.off("uncaughtException", uncaughtException);
|
|
499
|
-
process.off("unhandledRejection", unhandledRejection);
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
const relativeIds = {};
|
|
503
|
-
function getVitestImport(id, state) {
|
|
504
|
-
if (externalizeMap.has(id)) return { externalize: externalizeMap.get(id) };
|
|
505
|
-
// always externalize Vitest because we import from there before running tests
|
|
506
|
-
// so we already have it cached by Node.js
|
|
507
|
-
const root = state().config.root;
|
|
508
|
-
const relativeRoot = relativeIds[root] ?? (relativeIds[root] = normalizedDistDir.slice(root.length));
|
|
509
|
-
if (id.includes(distDir) || id.includes(normalizedDistDir) || relativeRoot && relativeRoot !== "/" && id.startsWith(relativeRoot)) {
|
|
510
|
-
const { path } = toFilePath(id, root);
|
|
511
|
-
const externalize = pathToFileURL(path).toString();
|
|
512
|
-
externalizeMap.set(id, externalize);
|
|
513
|
-
return { externalize };
|
|
514
|
-
}
|
|
515
|
-
if (bareVitestRegexp.test(id)) {
|
|
516
|
-
externalizeMap.set(id, id);
|
|
517
|
-
return { externalize: id };
|
|
518
|
-
}
|
|
519
|
-
return null;
|
|
520
|
-
}
|
|
521
|
-
async function startVitestExecutor(options) {
|
|
522
|
-
const state = () => globalThis.__vitest_worker__ || options.state;
|
|
523
|
-
const rpc = () => state().rpc;
|
|
524
|
-
process.exit = (code = process.exitCode || 0) => {
|
|
525
|
-
throw new Error(`process.exit unexpectedly called with "${code}"`);
|
|
526
|
-
};
|
|
527
|
-
listenForErrors(state);
|
|
528
|
-
const getTransformMode = () => {
|
|
529
|
-
return state().environment.transformMode ?? "ssr";
|
|
530
|
-
};
|
|
531
|
-
return await createVitestExecutor({
|
|
532
|
-
async fetchModule(id) {
|
|
533
|
-
const vitest = getVitestImport(id, state);
|
|
534
|
-
if (vitest) return vitest;
|
|
535
|
-
const result = await rpc().fetch(id, getTransformMode());
|
|
536
|
-
if (result.id && !result.externalize) {
|
|
537
|
-
const code = readFileSync(result.id, "utf-8");
|
|
538
|
-
return { code };
|
|
539
|
-
}
|
|
540
|
-
return result;
|
|
541
|
-
},
|
|
542
|
-
resolveId(id, importer) {
|
|
543
|
-
return rpc().resolveId(id, importer, getTransformMode());
|
|
544
|
-
},
|
|
545
|
-
get moduleCache() {
|
|
546
|
-
return state().moduleCache;
|
|
547
|
-
},
|
|
548
|
-
get moduleExecutionInfo() {
|
|
549
|
-
return state().moduleExecutionInfo;
|
|
550
|
-
},
|
|
551
|
-
get interopDefault() {
|
|
552
|
-
return state().config.deps.interopDefault;
|
|
553
|
-
},
|
|
554
|
-
get moduleDirectories() {
|
|
555
|
-
return state().config.deps.moduleDirectories;
|
|
556
|
-
},
|
|
557
|
-
get root() {
|
|
558
|
-
return state().config.root;
|
|
559
|
-
},
|
|
560
|
-
get base() {
|
|
561
|
-
return state().config.base;
|
|
562
|
-
},
|
|
563
|
-
...options
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
function updateStyle(id, css) {
|
|
567
|
-
if (typeof document === "undefined") return;
|
|
568
|
-
const element = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
569
|
-
if (element) {
|
|
570
|
-
element.textContent = css;
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
573
|
-
const head = document.querySelector("head");
|
|
574
|
-
const style = document.createElement("style");
|
|
575
|
-
style.setAttribute("type", "text/css");
|
|
576
|
-
style.setAttribute("data-vite-dev-id", id);
|
|
577
|
-
style.textContent = css;
|
|
578
|
-
head?.appendChild(style);
|
|
579
|
-
}
|
|
580
|
-
function removeStyle(id) {
|
|
581
|
-
if (typeof document === "undefined") return;
|
|
582
|
-
const sheet = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
583
|
-
if (sheet) document.head.removeChild(sheet);
|
|
584
|
-
}
|
|
585
|
-
function getDefaultRequestStubs(context) {
|
|
586
|
-
if (!context) {
|
|
587
|
-
const clientStub = {
|
|
588
|
-
...DEFAULT_REQUEST_STUBS["@vite/client"],
|
|
589
|
-
updateStyle,
|
|
590
|
-
removeStyle
|
|
591
|
-
};
|
|
592
|
-
return {
|
|
593
|
-
"/@vite/client": clientStub,
|
|
594
|
-
"@vite/client": clientStub
|
|
595
|
-
};
|
|
596
|
-
}
|
|
597
|
-
const clientStub = vm.runInContext(`(defaultClient) => ({ ...defaultClient, updateStyle: ${updateStyle.toString()}, removeStyle: ${removeStyle.toString()} })`, context)(DEFAULT_REQUEST_STUBS["@vite/client"]);
|
|
598
|
-
return {
|
|
599
|
-
"/@vite/client": clientStub,
|
|
600
|
-
"@vite/client": clientStub
|
|
601
|
-
};
|
|
602
|
-
}
|
|
603
|
-
class VitestExecutor extends ViteNodeRunner {
|
|
604
|
-
mocker;
|
|
605
|
-
externalModules;
|
|
606
|
-
primitives;
|
|
607
|
-
constructor(options) {
|
|
608
|
-
super({
|
|
609
|
-
...options,
|
|
610
|
-
interopDefault: options.context ? false : options.interopDefault
|
|
611
|
-
});
|
|
612
|
-
this.options = options;
|
|
613
|
-
this.mocker = new VitestMocker(this);
|
|
614
|
-
if (!options.context) {
|
|
615
|
-
Object.defineProperty(globalThis, "__vitest_mocker__", {
|
|
616
|
-
value: this.mocker,
|
|
617
|
-
writable: true,
|
|
618
|
-
configurable: true
|
|
619
|
-
});
|
|
620
|
-
this.primitives = {
|
|
621
|
-
Object,
|
|
622
|
-
Reflect,
|
|
623
|
-
Symbol
|
|
624
|
-
};
|
|
625
|
-
} else if (options.externalModulesExecutor) {
|
|
626
|
-
this.primitives = vm.runInContext("({ Object, Reflect, Symbol })", options.context);
|
|
627
|
-
this.externalModules = options.externalModulesExecutor;
|
|
628
|
-
} else throw new Error("When context is provided, externalModulesExecutor must be provided as well.");
|
|
629
|
-
}
|
|
630
|
-
getContextPrimitives() {
|
|
631
|
-
return this.primitives;
|
|
632
|
-
}
|
|
633
|
-
get state() {
|
|
634
|
-
// @ts-expect-error injected untyped global
|
|
635
|
-
return globalThis.__vitest_worker__ || this.options.state;
|
|
636
|
-
}
|
|
637
|
-
get moduleExecutionInfo() {
|
|
638
|
-
return this.options.moduleExecutionInfo;
|
|
639
|
-
}
|
|
640
|
-
shouldResolveId(id, _importee) {
|
|
641
|
-
if (isInternalRequest(id) || id.startsWith("data:")) return false;
|
|
642
|
-
const transformMode = this.state.environment?.transformMode ?? "ssr";
|
|
643
|
-
// do not try and resolve node builtins in Node
|
|
644
|
-
// import('url') returns Node internal even if 'url' package is installed
|
|
645
|
-
return transformMode === "ssr" ? !isNodeBuiltin$1(id) : !id.startsWith("node:");
|
|
646
|
-
}
|
|
647
|
-
async originalResolveUrl(id, importer) {
|
|
648
|
-
return super.resolveUrl(id, importer);
|
|
649
|
-
}
|
|
650
|
-
async resolveUrl(id, importer) {
|
|
651
|
-
if (VitestMocker.pendingIds.length) await this.mocker.resolveMocks();
|
|
652
|
-
if (importer && importer.startsWith("mock:")) importer = importer.slice(5);
|
|
653
|
-
try {
|
|
654
|
-
return await super.resolveUrl(id, importer);
|
|
655
|
-
} catch (error) {
|
|
656
|
-
if (error.code === "ERR_MODULE_NOT_FOUND") {
|
|
657
|
-
const { id } = error[Symbol.for("vitest.error.not_found.data")];
|
|
658
|
-
const path = this.mocker.normalizePath(id);
|
|
659
|
-
const mock = this.mocker.getDependencyMock(path);
|
|
660
|
-
if (mock !== void 0) return [id, id];
|
|
661
|
-
}
|
|
662
|
-
throw error;
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
async runModule(context, transformed) {
|
|
666
|
-
const vmContext = this.options.context;
|
|
667
|
-
if (!vmContext || !this.externalModules) return super.runModule(context, transformed);
|
|
668
|
-
// add 'use strict' since ESM enables it by default
|
|
669
|
-
const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
|
|
670
|
-
const code = `${codeDefinition}${transformed}\n}}`;
|
|
671
|
-
const options = {
|
|
672
|
-
filename: context.__filename,
|
|
673
|
-
lineOffset: 0,
|
|
674
|
-
columnOffset: -codeDefinition.length
|
|
675
|
-
};
|
|
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
|
-
}
|
|
686
|
-
}
|
|
687
|
-
async importExternalModule(path) {
|
|
688
|
-
if (this.externalModules) return this.externalModules.import(path);
|
|
689
|
-
return super.importExternalModule(path);
|
|
690
|
-
}
|
|
691
|
-
async dependencyRequest(id, fsPath, callstack) {
|
|
692
|
-
const mocked = await this.mocker.requestWithMock(fsPath, callstack);
|
|
693
|
-
if (typeof mocked === "string") return super.dependencyRequest(mocked, mocked, callstack);
|
|
694
|
-
if (mocked && typeof mocked === "object") return mocked;
|
|
695
|
-
return super.dependencyRequest(id, fsPath, callstack);
|
|
696
|
-
}
|
|
697
|
-
prepareContext(context) {
|
|
698
|
-
// support `import.meta.vitest` for test entry
|
|
699
|
-
if (this.state.filepath && normalize$1(this.state.filepath) === normalize$1(context.__filename)) {
|
|
700
|
-
const globalNamespace = this.options.context || globalThis;
|
|
701
|
-
Object.defineProperty(context.__vite_ssr_import_meta__, "vitest", { get: () => globalNamespace.__vitest_index__ });
|
|
702
|
-
}
|
|
703
|
-
if (this.options.context && this.externalModules) context.require = this.externalModules.createRequire(context.__filename);
|
|
704
|
-
return context;
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
export { VitestExecutor as V, getDefaultRequestStubs as g, startVitestExecutor as s };
|