vitest 2.0.0-beta.1 → 2.0.0-beta.11
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 +22 -2
- package/dist/browser.js +5 -3
- package/dist/chunks/{integrations-globals.B5Jl0grA.js → integrations-globals.CC2ed6Py.js} +9 -9
- package/dist/chunks/{node-git.CCI8evVZ.js → node-git.ZtkbKc8u.js} +14 -15
- package/dist/chunks/{runtime-console.CUES-L8X.js → runtime-console.Ckl0vEQr.js} +76 -31
- package/dist/chunks/{runtime-runBaseTests._dXkRAZc.js → runtime-runBaseTests.BXW_BJeO.js} +40 -32
- package/dist/cli.js +3 -3
- package/dist/config.cjs +43 -20
- package/dist/config.d.ts +2 -2
- package/dist/config.js +43 -21
- package/dist/coverage.d.ts +4 -4
- package/dist/coverage.js +125 -37
- package/dist/environments.d.ts +1 -1
- package/dist/environments.js +1 -1
- package/dist/execute.d.ts +2 -2
- package/dist/execute.js +2 -2
- package/dist/index.d.ts +20 -30
- package/dist/index.js +9 -9
- package/dist/node.d.ts +22 -9
- package/dist/node.js +25 -15
- package/dist/path.js +4 -1
- package/dist/{reporters-MGvT5U9f.d.ts → reporters-fiIq_dT9.d.ts} +310 -148
- package/dist/reporters.d.ts +1 -1
- package/dist/reporters.js +8 -8
- package/dist/runners.d.ts +4 -2
- package/dist/runners.js +103 -57
- package/dist/{suite-8WAe-urM.d.ts → suite-D4aoU9rI.d.ts} +1 -1
- package/dist/suite.d.ts +2 -2
- package/dist/suite.js +2 -2
- package/dist/utils.d.ts +1 -5
- package/dist/utils.js +1 -6
- package/dist/vendor/{base.VFkIJ66g.js → base.C2DbLEfT.js} +4 -3
- package/dist/vendor/{base._gnK9Slw.js → base.CTYV4Gnz.js} +24 -17
- package/dist/vendor/{benchmark.BNLebNi5.js → benchmark.CMp8QfyL.js} +13 -14
- package/dist/vendor/{cac.DzKZaJu2.js → cac.BcJW7n2j.js} +127 -51
- package/dist/vendor/{cli-api.DTeni0Qq.js → cli-api.C8t8m4__.js} +2771 -1711
- package/dist/vendor/{constants.5SOfHUj0.js → constants.BWsVtsAj.js} +6 -22
- package/dist/vendor/{coverage.ChSqD-qS.js → coverage.BhYSDdTT.js} +27 -11
- package/dist/vendor/{date.BKM1wewY.js → date.W2xKR2qe.js} +5 -3
- package/dist/vendor/{execute.CLLNVNnK.js → execute.T3gg2ZK6.js} +177 -63
- package/dist/vendor/{index.BfoZyXD1.js → index.-dbR4KUi.js} +17 -9
- package/dist/vendor/{index.CRxYS9H3.js → index.BC5zhX9y.js} +1201 -491
- package/dist/vendor/{index.DP-km6lF.js → index.BMmMjLIQ.js} +64 -51
- package/dist/vendor/{index._7XLd8Kd.js → index.C9Thslzw.js} +2 -1
- package/dist/vendor/{index.CmILuxzC.js → index.CQJ2m700.js} +3 -3
- package/dist/vendor/{index.DeR1hhfY.js → index.D4nqnQWz.js} +71 -74
- package/dist/vendor/{rpc.DRDE9Pu1.js → rpc.BGx7q_k2.js} +30 -19
- package/dist/vendor/{run-once.DLomgGUH.js → run-once.Db8Hgq9X.js} +2 -1
- package/dist/vendor/{setup-common.XeoZAW8t.js → setup-common.uqZOEWuR.js} +30 -15
- package/dist/vendor/spy.Cf_4R5Oe.js +22 -0
- package/dist/vendor/{tasks.WC7M-K-v.js → tasks.DhVtQBtW.js} +3 -1
- package/dist/vendor/{utils.D5gGkwyH.js → utils.DSO2UK15.js} +41 -26
- package/dist/vendor/{utils.CUjzkRH7.js → utils.DkxLWvS1.js} +12 -5
- package/dist/vendor/{vi.ClD3hi7L.js → vi.BPjl8cAZ.js} +350 -166
- package/dist/vendor/{vm.Bi3bljci.js → vm.CycSoHnJ.js} +151 -86
- package/dist/worker.js +31 -15
- package/dist/workers/forks.js +4 -4
- package/dist/workers/runVmTests.js +14 -13
- 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 +9 -9
- package/package.json +22 -22
- package/suppress-warnings.cjs +2 -4
|
@@ -4,36 +4,20 @@ const defaultInspectPort = 9229;
|
|
|
4
4
|
const EXIT_CODE_RESTART = 43;
|
|
5
5
|
const API_PATH = "/__vitest_api__";
|
|
6
6
|
const extraInlineDeps = [
|
|
7
|
-
/^(?!.*
|
|
8
|
-
/^(?!.*
|
|
7
|
+
/^(?!.*node_modules).*\.mjs$/,
|
|
8
|
+
/^(?!.*node_modules).*\.cjs\.js$/,
|
|
9
9
|
// Vite client
|
|
10
10
|
/vite\w*\/dist\/client\/env.mjs/,
|
|
11
11
|
// Nuxt
|
|
12
12
|
"@nuxt/test-utils"
|
|
13
13
|
];
|
|
14
|
-
const CONFIG_NAMES = [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
];
|
|
18
|
-
const WORKSPACES_NAMES = [
|
|
19
|
-
"vitest.workspace",
|
|
20
|
-
"vitest.projects"
|
|
21
|
-
];
|
|
22
|
-
const CONFIG_EXTENSIONS = [
|
|
23
|
-
".ts",
|
|
24
|
-
".mts",
|
|
25
|
-
".cts",
|
|
26
|
-
".js",
|
|
27
|
-
".mjs",
|
|
28
|
-
".cjs"
|
|
29
|
-
];
|
|
14
|
+
const CONFIG_NAMES = ["vitest.config", "vite.config"];
|
|
15
|
+
const WORKSPACES_NAMES = ["vitest.workspace", "vitest.projects"];
|
|
16
|
+
const CONFIG_EXTENSIONS = [".ts", ".mts", ".cts", ".js", ".mjs", ".cjs"];
|
|
30
17
|
const configFiles = CONFIG_NAMES.flatMap(
|
|
31
18
|
(name) => CONFIG_EXTENSIONS.map((ext) => name + ext)
|
|
32
19
|
);
|
|
33
|
-
const WORKSPACES_EXTENSIONS = [
|
|
34
|
-
...CONFIG_EXTENSIONS,
|
|
35
|
-
".json"
|
|
36
|
-
];
|
|
20
|
+
const WORKSPACES_EXTENSIONS = [...CONFIG_EXTENSIONS, ".json"];
|
|
37
21
|
const workspacesFiles = WORKSPACES_NAMES.flatMap(
|
|
38
22
|
(name) => WORKSPACES_EXTENSIONS.map((ext) => name + ext)
|
|
39
23
|
);
|
|
@@ -3,50 +3,66 @@ const CoverageProviderMap = {
|
|
|
3
3
|
istanbul: "@vitest/coverage-istanbul"
|
|
4
4
|
};
|
|
5
5
|
async function resolveCoverageProviderModule(options, loader) {
|
|
6
|
-
if (!(options == null ? void 0 : options.enabled) || !options.provider)
|
|
6
|
+
if (!(options == null ? void 0 : options.enabled) || !options.provider) {
|
|
7
7
|
return null;
|
|
8
|
+
}
|
|
8
9
|
const provider = options.provider;
|
|
9
10
|
if (provider === "v8" || provider === "istanbul") {
|
|
10
|
-
const { default: coverageModule } = await loader.executeId(
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const { default: coverageModule } = await loader.executeId(
|
|
12
|
+
CoverageProviderMap[provider]
|
|
13
|
+
);
|
|
14
|
+
if (!coverageModule) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Failed to load ${CoverageProviderMap[provider]}. Default export is missing.`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
13
19
|
return coverageModule;
|
|
14
20
|
}
|
|
15
21
|
let customProviderModule;
|
|
16
22
|
try {
|
|
17
23
|
customProviderModule = await loader.executeId(options.customProviderModule);
|
|
18
24
|
} catch (error) {
|
|
19
|
-
throw new Error(
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Failed to load custom CoverageProviderModule from ${options.customProviderModule}`,
|
|
27
|
+
{ cause: error }
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
if (customProviderModule.default == null) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`Custom CoverageProviderModule loaded from ${options.customProviderModule} was not the default export`
|
|
33
|
+
);
|
|
20
34
|
}
|
|
21
|
-
if (customProviderModule.default == null)
|
|
22
|
-
throw new Error(`Custom CoverageProviderModule loaded from ${options.customProviderModule} was not the default export`);
|
|
23
35
|
return customProviderModule.default;
|
|
24
36
|
}
|
|
25
37
|
async function getCoverageProvider(options, loader) {
|
|
26
38
|
const coverageModule = await resolveCoverageProviderModule(options, loader);
|
|
27
|
-
if (coverageModule)
|
|
39
|
+
if (coverageModule) {
|
|
28
40
|
return coverageModule.getProvider();
|
|
41
|
+
}
|
|
29
42
|
return null;
|
|
30
43
|
}
|
|
31
44
|
async function startCoverageInsideWorker(options, loader) {
|
|
32
45
|
var _a;
|
|
33
46
|
const coverageModule = await resolveCoverageProviderModule(options, loader);
|
|
34
|
-
if (coverageModule)
|
|
47
|
+
if (coverageModule) {
|
|
35
48
|
return (_a = coverageModule.startCoverage) == null ? void 0 : _a.call(coverageModule);
|
|
49
|
+
}
|
|
36
50
|
return null;
|
|
37
51
|
}
|
|
38
52
|
async function takeCoverageInsideWorker(options, loader) {
|
|
39
53
|
var _a;
|
|
40
54
|
const coverageModule = await resolveCoverageProviderModule(options, loader);
|
|
41
|
-
if (coverageModule)
|
|
55
|
+
if (coverageModule) {
|
|
42
56
|
return (_a = coverageModule.takeCoverage) == null ? void 0 : _a.call(coverageModule);
|
|
57
|
+
}
|
|
43
58
|
return null;
|
|
44
59
|
}
|
|
45
60
|
async function stopCoverageInsideWorker(options, loader) {
|
|
46
61
|
var _a;
|
|
47
62
|
const coverageModule = await resolveCoverageProviderModule(options, loader);
|
|
48
|
-
if (coverageModule)
|
|
63
|
+
if (coverageModule) {
|
|
49
64
|
return (_a = coverageModule.stopCoverage) == null ? void 0 : _a.call(coverageModule);
|
|
65
|
+
}
|
|
50
66
|
return null;
|
|
51
67
|
}
|
|
52
68
|
|
|
@@ -6,10 +6,11 @@ class MockDate extends RealDate {
|
|
|
6
6
|
let date;
|
|
7
7
|
switch (arguments.length) {
|
|
8
8
|
case 0:
|
|
9
|
-
if (now !== null)
|
|
9
|
+
if (now !== null) {
|
|
10
10
|
date = new RealDate(now.valueOf());
|
|
11
|
-
else
|
|
11
|
+
} else {
|
|
12
12
|
date = new RealDate();
|
|
13
|
+
}
|
|
13
14
|
break;
|
|
14
15
|
case 1:
|
|
15
16
|
date = new RealDate(y);
|
|
@@ -39,8 +40,9 @@ MockDate.toString = function() {
|
|
|
39
40
|
};
|
|
40
41
|
function mockDate(date) {
|
|
41
42
|
const dateObj = new RealDate(date.valueOf());
|
|
42
|
-
if (Number.isNaN(dateObj.getTime()))
|
|
43
|
+
if (Number.isNaN(dateObj.getTime())) {
|
|
43
44
|
throw new TypeError(`mockdate: The time set is an invalid date: ${date}`);
|
|
45
|
+
}
|
|
44
46
|
globalThis.Date = MockDate;
|
|
45
47
|
now = dateObj.valueOf();
|
|
46
48
|
}
|
|
@@ -7,7 +7,7 @@ import { resolve, isAbsolute, dirname, join, basename, extname, normalize, relat
|
|
|
7
7
|
import { processError } from '@vitest/utils/error';
|
|
8
8
|
import { distDir } from '../path.js';
|
|
9
9
|
import { highlight, getType } from '@vitest/utils';
|
|
10
|
-
import { g as getAllMockableProperties } from './base.
|
|
10
|
+
import { g as getAllMockableProperties } from './base.CTYV4Gnz.js';
|
|
11
11
|
|
|
12
12
|
const spyModulePath = resolve(distDir, "spy.js");
|
|
13
13
|
class RefTracker {
|
|
@@ -33,12 +33,39 @@ class VitestMocker {
|
|
|
33
33
|
constructor(executor) {
|
|
34
34
|
this.executor = executor;
|
|
35
35
|
const context = this.executor.options.context;
|
|
36
|
-
if (context)
|
|
37
|
-
this.primitives = vm.runInContext(
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
if (context) {
|
|
37
|
+
this.primitives = vm.runInContext(
|
|
38
|
+
"({ Object, Error, Function, RegExp, Symbol, Array, Map })",
|
|
39
|
+
context
|
|
40
|
+
);
|
|
41
|
+
} else {
|
|
42
|
+
this.primitives = {
|
|
43
|
+
Object,
|
|
44
|
+
Error,
|
|
45
|
+
Function,
|
|
46
|
+
RegExp,
|
|
47
|
+
Symbol: globalThis.Symbol,
|
|
48
|
+
Array,
|
|
49
|
+
Map
|
|
50
|
+
};
|
|
51
|
+
}
|
|
40
52
|
const Symbol2 = this.primitives.Symbol;
|
|
41
|
-
this.filterPublicKeys = [
|
|
53
|
+
this.filterPublicKeys = [
|
|
54
|
+
"__esModule",
|
|
55
|
+
Symbol2.asyncIterator,
|
|
56
|
+
Symbol2.hasInstance,
|
|
57
|
+
Symbol2.isConcatSpreadable,
|
|
58
|
+
Symbol2.iterator,
|
|
59
|
+
Symbol2.match,
|
|
60
|
+
Symbol2.matchAll,
|
|
61
|
+
Symbol2.replace,
|
|
62
|
+
Symbol2.search,
|
|
63
|
+
Symbol2.split,
|
|
64
|
+
Symbol2.species,
|
|
65
|
+
Symbol2.toPrimitive,
|
|
66
|
+
Symbol2.toStringTag,
|
|
67
|
+
Symbol2.unscopables
|
|
68
|
+
];
|
|
42
69
|
}
|
|
43
70
|
static pendingIds = [];
|
|
44
71
|
spyModule;
|
|
@@ -65,10 +92,11 @@ class VitestMocker {
|
|
|
65
92
|
}
|
|
66
93
|
deleteCachedItem(id) {
|
|
67
94
|
const mockId = this.getMockPath(id);
|
|
68
|
-
if (this.moduleCache.has(mockId))
|
|
95
|
+
if (this.moduleCache.has(mockId)) {
|
|
69
96
|
this.moduleCache.delete(mockId);
|
|
97
|
+
}
|
|
70
98
|
}
|
|
71
|
-
|
|
99
|
+
isModuleDirectory(path) {
|
|
72
100
|
return this.moduleDirectories.some((dir) => path.includes(dir));
|
|
73
101
|
}
|
|
74
102
|
getSuiteFilepath() {
|
|
@@ -103,7 +131,7 @@ class VitestMocker {
|
|
|
103
131
|
throw error;
|
|
104
132
|
}
|
|
105
133
|
}
|
|
106
|
-
const external = !isAbsolute(fsPath) || this.
|
|
134
|
+
const external = !isAbsolute(fsPath) || this.isModuleDirectory(fsPath) ? rawId : null;
|
|
107
135
|
return {
|
|
108
136
|
id,
|
|
109
137
|
fsPath,
|
|
@@ -111,22 +139,31 @@ class VitestMocker {
|
|
|
111
139
|
};
|
|
112
140
|
}
|
|
113
141
|
async resolveMocks() {
|
|
114
|
-
if (!VitestMocker.pendingIds.length)
|
|
142
|
+
if (!VitestMocker.pendingIds.length) {
|
|
115
143
|
return;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
this.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
144
|
+
}
|
|
145
|
+
await Promise.all(
|
|
146
|
+
VitestMocker.pendingIds.map(async (mock) => {
|
|
147
|
+
const { fsPath, external } = await this.resolvePath(
|
|
148
|
+
mock.id,
|
|
149
|
+
mock.importer
|
|
150
|
+
);
|
|
151
|
+
if (mock.type === "unmock") {
|
|
152
|
+
this.unmockPath(fsPath);
|
|
153
|
+
}
|
|
154
|
+
if (mock.type === "mock") {
|
|
155
|
+
this.mockPath(mock.id, fsPath, external, mock.factory);
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
);
|
|
123
159
|
VitestMocker.pendingIds = [];
|
|
124
160
|
}
|
|
125
161
|
async callFunctionMock(dep, mock) {
|
|
126
162
|
var _a, _b;
|
|
127
163
|
const cached = (_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports;
|
|
128
|
-
if (cached)
|
|
164
|
+
if (cached) {
|
|
129
165
|
return cached;
|
|
166
|
+
}
|
|
130
167
|
let exports;
|
|
131
168
|
try {
|
|
132
169
|
exports = await mock();
|
|
@@ -139,19 +176,26 @@ class VitestMocker {
|
|
|
139
176
|
}
|
|
140
177
|
const filepath = dep.slice(5);
|
|
141
178
|
const mockpath = ((_b = this.resolveCache.get(this.getSuiteFilepath())) == null ? void 0 : _b[filepath]) || filepath;
|
|
142
|
-
if (exports === null || typeof exports !== "object")
|
|
143
|
-
throw this.createError(
|
|
179
|
+
if (exports === null || typeof exports !== "object") {
|
|
180
|
+
throw this.createError(
|
|
181
|
+
`[vitest] vi.mock("${mockpath}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
144
184
|
const moduleExports = new Proxy(exports, {
|
|
145
185
|
get: (target, prop) => {
|
|
146
186
|
const val = target[prop];
|
|
147
187
|
if (prop === "then") {
|
|
148
|
-
if (target instanceof Promise)
|
|
188
|
+
if (target instanceof Promise) {
|
|
149
189
|
return target.then.bind(target);
|
|
190
|
+
}
|
|
150
191
|
} else if (!(prop in target)) {
|
|
151
|
-
if (this.filterPublicKeys.includes(prop))
|
|
192
|
+
if (this.filterPublicKeys.includes(prop)) {
|
|
152
193
|
return void 0;
|
|
194
|
+
}
|
|
153
195
|
throw this.createError(
|
|
154
|
-
`[vitest] No "${String(
|
|
196
|
+
`[vitest] No "${String(
|
|
197
|
+
prop
|
|
198
|
+
)}" export is defined on the "${mockpath}" mock. Did you forget to return it from "vi.mock"?
|
|
155
199
|
If you need to partially mock a module, you can use "importOriginal" helper inside:
|
|
156
200
|
`,
|
|
157
201
|
highlight(`vi.mock("${mockpath}", async (importOriginal) => {
|
|
@@ -186,14 +230,16 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
186
230
|
if (external || isNodeBuiltin(mockPath) || !existsSync(mockPath)) {
|
|
187
231
|
const mockDirname = dirname(path);
|
|
188
232
|
const mockFolder = join(this.root, "__mocks__", mockDirname);
|
|
189
|
-
if (!existsSync(mockFolder))
|
|
233
|
+
if (!existsSync(mockFolder)) {
|
|
190
234
|
return null;
|
|
235
|
+
}
|
|
191
236
|
const files = readdirSync(mockFolder);
|
|
192
237
|
const baseOriginal = basename(path);
|
|
193
238
|
for (const file of files) {
|
|
194
239
|
const baseFile = basename(file, extname(file));
|
|
195
|
-
if (baseFile === baseOriginal)
|
|
240
|
+
if (baseFile === baseOriginal) {
|
|
196
241
|
return resolve(mockFolder, file);
|
|
242
|
+
}
|
|
197
243
|
}
|
|
198
244
|
return null;
|
|
199
245
|
}
|
|
@@ -216,7 +262,11 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
216
262
|
const mockPropertiesOf = (container, newContainer) => {
|
|
217
263
|
const containerType = getType(container);
|
|
218
264
|
const isModule = containerType === "Module" || !!container.__esModule;
|
|
219
|
-
for (const { key: property, descriptor } of getAllMockableProperties(
|
|
265
|
+
for (const { key: property, descriptor } of getAllMockableProperties(
|
|
266
|
+
container,
|
|
267
|
+
isModule,
|
|
268
|
+
this.primitives
|
|
269
|
+
)) {
|
|
220
270
|
if (!isModule && descriptor.get) {
|
|
221
271
|
try {
|
|
222
272
|
Object.defineProperty(newContainer, property, descriptor);
|
|
@@ -224,12 +274,15 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
224
274
|
}
|
|
225
275
|
continue;
|
|
226
276
|
}
|
|
227
|
-
if (isSpecialProp(property, containerType))
|
|
277
|
+
if (isSpecialProp(property, containerType)) {
|
|
228
278
|
continue;
|
|
279
|
+
}
|
|
229
280
|
const value = container[property];
|
|
230
281
|
const refId = refs.getId(value);
|
|
231
282
|
if (refId !== void 0) {
|
|
232
|
-
finalizers.push(
|
|
283
|
+
finalizers.push(
|
|
284
|
+
() => define(newContainer, property, refs.getMockedValue(refId))
|
|
285
|
+
);
|
|
233
286
|
continue;
|
|
234
287
|
}
|
|
235
288
|
const type = getType(value);
|
|
@@ -242,14 +295,20 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
242
295
|
define(newContainer, property, value);
|
|
243
296
|
continue;
|
|
244
297
|
}
|
|
245
|
-
if (!define(newContainer, property, isFunction ? value : {}))
|
|
298
|
+
if (!define(newContainer, property, isFunction ? value : {})) {
|
|
246
299
|
continue;
|
|
300
|
+
}
|
|
247
301
|
if (isFunction) {
|
|
248
302
|
let mockFunction2 = function() {
|
|
249
303
|
if (this instanceof newContainer[property]) {
|
|
250
|
-
for (const { key, descriptor: descriptor2 } of getAllMockableProperties(
|
|
251
|
-
|
|
304
|
+
for (const { key, descriptor: descriptor2 } of getAllMockableProperties(
|
|
305
|
+
this,
|
|
306
|
+
false,
|
|
307
|
+
primitives
|
|
308
|
+
)) {
|
|
309
|
+
if (descriptor2.get) {
|
|
252
310
|
continue;
|
|
311
|
+
}
|
|
253
312
|
const value2 = this[key];
|
|
254
313
|
const type2 = getType(value2);
|
|
255
314
|
const isFunction2 = type2.includes("Function") && typeof value2 === "function";
|
|
@@ -265,8 +324,11 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
265
324
|
}
|
|
266
325
|
}
|
|
267
326
|
};
|
|
268
|
-
if (!this.spyModule)
|
|
269
|
-
throw this.createError(
|
|
327
|
+
if (!this.spyModule) {
|
|
328
|
+
throw this.createError(
|
|
329
|
+
"[vitest] `spyModule` is not defined. This is Vitest error. Please open a new issue with reproduction."
|
|
330
|
+
);
|
|
331
|
+
}
|
|
270
332
|
const spyModule = this.spyModule;
|
|
271
333
|
const primitives = this.primitives;
|
|
272
334
|
const mock = spyModule.spyOn(newContainer, property).mockImplementation(mockFunction2);
|
|
@@ -283,16 +345,18 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
283
345
|
};
|
|
284
346
|
const mockedObject = mockExports;
|
|
285
347
|
mockPropertiesOf(object, mockedObject);
|
|
286
|
-
for (const finalizer of finalizers)
|
|
348
|
+
for (const finalizer of finalizers) {
|
|
287
349
|
finalizer();
|
|
350
|
+
}
|
|
288
351
|
return mockedObject;
|
|
289
352
|
}
|
|
290
353
|
unmockPath(path) {
|
|
291
354
|
const suitefile = this.getSuiteFilepath();
|
|
292
355
|
const id = this.normalizePath(path);
|
|
293
356
|
const mock = this.mockMap.get(suitefile);
|
|
294
|
-
if (mock && id in mock)
|
|
357
|
+
if (mock && id in mock) {
|
|
295
358
|
delete mock[id];
|
|
359
|
+
}
|
|
296
360
|
this.deleteCachedItem(id);
|
|
297
361
|
}
|
|
298
362
|
mockPath(originalId, path, external, factory) {
|
|
@@ -308,21 +372,27 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
308
372
|
}
|
|
309
373
|
async importActual(rawId, importer, callstack) {
|
|
310
374
|
const { id, fsPath } = await this.resolvePath(rawId, importer);
|
|
311
|
-
const result = await this.executor.cachedRequest(
|
|
375
|
+
const result = await this.executor.cachedRequest(
|
|
376
|
+
id,
|
|
377
|
+
fsPath,
|
|
378
|
+
callstack || [importer]
|
|
379
|
+
);
|
|
312
380
|
return result;
|
|
313
381
|
}
|
|
314
382
|
async importMock(rawId, importee) {
|
|
315
383
|
const { id, fsPath, external } = await this.resolvePath(rawId, importee);
|
|
316
384
|
const normalizedId = this.normalizePath(fsPath);
|
|
317
385
|
let mock = this.getDependencyMock(normalizedId);
|
|
318
|
-
if (mock === void 0)
|
|
386
|
+
if (mock === void 0) {
|
|
319
387
|
mock = this.resolveMockPath(fsPath, external);
|
|
388
|
+
}
|
|
320
389
|
if (mock === null) {
|
|
321
390
|
const mod = await this.executor.cachedRequest(id, fsPath, [importee]);
|
|
322
391
|
return this.mockObject(mod);
|
|
323
392
|
}
|
|
324
|
-
if (typeof mock === "function")
|
|
393
|
+
if (typeof mock === "function") {
|
|
325
394
|
return this.callFunctionMock(fsPath, mock);
|
|
395
|
+
}
|
|
326
396
|
return this.executor.dependencyRequest(mock, mock, [importee]);
|
|
327
397
|
}
|
|
328
398
|
async requestWithMock(url, callstack) {
|
|
@@ -331,8 +401,9 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
331
401
|
const mockPath = this.getMockPath(id);
|
|
332
402
|
if (mock === null) {
|
|
333
403
|
const cache = this.moduleCache.get(mockPath);
|
|
334
|
-
if (cache.exports)
|
|
404
|
+
if (cache.exports) {
|
|
335
405
|
return cache.exports;
|
|
406
|
+
}
|
|
336
407
|
const exports = {};
|
|
337
408
|
this.moduleCache.set(mockPath, { exports });
|
|
338
409
|
const mod = await this.executor.directRequest(url, url, callstack);
|
|
@@ -350,14 +421,26 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
|
|
|
350
421
|
callstack.splice(indexMock, 1);
|
|
351
422
|
}
|
|
352
423
|
}
|
|
353
|
-
if (typeof mock === "string" && !callstack.includes(mock))
|
|
424
|
+
if (typeof mock === "string" && !callstack.includes(mock)) {
|
|
354
425
|
return mock;
|
|
426
|
+
}
|
|
355
427
|
}
|
|
356
428
|
queueMock(id, importer, factory, throwIfCached = false) {
|
|
357
|
-
VitestMocker.pendingIds.push({
|
|
429
|
+
VitestMocker.pendingIds.push({
|
|
430
|
+
type: "mock",
|
|
431
|
+
id,
|
|
432
|
+
importer,
|
|
433
|
+
factory,
|
|
434
|
+
throwIfCached
|
|
435
|
+
});
|
|
358
436
|
}
|
|
359
437
|
queueUnmock(id, importer, throwIfCached = false) {
|
|
360
|
-
VitestMocker.pendingIds.push({
|
|
438
|
+
VitestMocker.pendingIds.push({
|
|
439
|
+
type: "unmock",
|
|
440
|
+
id,
|
|
441
|
+
importer,
|
|
442
|
+
throwIfCached
|
|
443
|
+
});
|
|
361
444
|
}
|
|
362
445
|
}
|
|
363
446
|
|
|
@@ -368,7 +451,7 @@ async function createVitestExecutor(options) {
|
|
|
368
451
|
return runner;
|
|
369
452
|
}
|
|
370
453
|
const externalizeMap = /* @__PURE__ */ new Map();
|
|
371
|
-
const bareVitestRegexp = /^@?vitest(
|
|
454
|
+
const bareVitestRegexp = /^@?vitest(?:\/|$)/;
|
|
372
455
|
const dispose = [];
|
|
373
456
|
function listenForErrors(state) {
|
|
374
457
|
dispose.forEach((fn) => fn());
|
|
@@ -379,8 +462,9 @@ function listenForErrors(state) {
|
|
|
379
462
|
const error = processError(err);
|
|
380
463
|
if (!isPrimitive(error)) {
|
|
381
464
|
error.VITEST_TEST_NAME = (_a = worker.current) == null ? void 0 : _a.name;
|
|
382
|
-
if (worker.filepath)
|
|
465
|
+
if (worker.filepath) {
|
|
383
466
|
error.VITEST_TEST_PATH = relative(state().config.root, worker.filepath);
|
|
467
|
+
}
|
|
384
468
|
error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun;
|
|
385
469
|
}
|
|
386
470
|
state().rpc.onUnhandledError(error, type);
|
|
@@ -395,7 +479,10 @@ function listenForErrors(state) {
|
|
|
395
479
|
});
|
|
396
480
|
}
|
|
397
481
|
async function startVitestExecutor(options) {
|
|
398
|
-
const state = () =>
|
|
482
|
+
const state = () => (
|
|
483
|
+
// @ts-expect-error injected untyped global
|
|
484
|
+
globalThis.__vitest_worker__ || options.state
|
|
485
|
+
);
|
|
399
486
|
const rpc = () => state().rpc;
|
|
400
487
|
process.exit = (code = process.exitCode || 0) => {
|
|
401
488
|
throw new Error(`process.exit unexpectedly called with "${code}"`);
|
|
@@ -406,8 +493,9 @@ async function startVitestExecutor(options) {
|
|
|
406
493
|
};
|
|
407
494
|
return await createVitestExecutor({
|
|
408
495
|
async fetchModule(id) {
|
|
409
|
-
if (externalizeMap.has(id))
|
|
496
|
+
if (externalizeMap.has(id)) {
|
|
410
497
|
return { externalize: externalizeMap.get(id) };
|
|
498
|
+
}
|
|
411
499
|
if (id.includes(distDir)) {
|
|
412
500
|
const { path } = toFilePath(id, state().config.root);
|
|
413
501
|
const externalize = pathToFileURL(path).toString();
|
|
@@ -450,8 +538,9 @@ async function startVitestExecutor(options) {
|
|
|
450
538
|
});
|
|
451
539
|
}
|
|
452
540
|
function updateStyle(id, css) {
|
|
453
|
-
if (typeof document === "undefined")
|
|
541
|
+
if (typeof document === "undefined") {
|
|
454
542
|
return;
|
|
543
|
+
}
|
|
455
544
|
const element = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
456
545
|
if (element) {
|
|
457
546
|
element.textContent = css;
|
|
@@ -465,15 +554,21 @@ function updateStyle(id, css) {
|
|
|
465
554
|
head == null ? void 0 : head.appendChild(style);
|
|
466
555
|
}
|
|
467
556
|
function removeStyle(id) {
|
|
468
|
-
if (typeof document === "undefined")
|
|
557
|
+
if (typeof document === "undefined") {
|
|
469
558
|
return;
|
|
559
|
+
}
|
|
470
560
|
const sheet = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
471
|
-
if (sheet)
|
|
561
|
+
if (sheet) {
|
|
472
562
|
document.head.removeChild(sheet);
|
|
563
|
+
}
|
|
473
564
|
}
|
|
474
565
|
function getDefaultRequestStubs(context) {
|
|
475
566
|
if (!context) {
|
|
476
|
-
const clientStub2 = {
|
|
567
|
+
const clientStub2 = {
|
|
568
|
+
...DEFAULT_REQUEST_STUBS["@vite/client"],
|
|
569
|
+
updateStyle,
|
|
570
|
+
removeStyle
|
|
571
|
+
};
|
|
477
572
|
return {
|
|
478
573
|
"/@vite/client": clientStub2,
|
|
479
574
|
"@vite/client": clientStub2
|
|
@@ -505,10 +600,15 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
505
600
|
});
|
|
506
601
|
this.primitives = { Object, Reflect, Symbol };
|
|
507
602
|
} else if (options.externalModulesExecutor) {
|
|
508
|
-
this.primitives = vm.runInContext(
|
|
603
|
+
this.primitives = vm.runInContext(
|
|
604
|
+
"({ Object, Reflect, Symbol })",
|
|
605
|
+
options.context
|
|
606
|
+
);
|
|
509
607
|
this.externalModules = options.externalModulesExecutor;
|
|
510
608
|
} else {
|
|
511
|
-
throw new Error(
|
|
609
|
+
throw new Error(
|
|
610
|
+
"When context is provided, externalModulesExecutor must be provided as well."
|
|
611
|
+
);
|
|
512
612
|
}
|
|
513
613
|
}
|
|
514
614
|
mocker;
|
|
@@ -522,8 +622,9 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
522
622
|
}
|
|
523
623
|
shouldResolveId(id, _importee) {
|
|
524
624
|
var _a;
|
|
525
|
-
if (isInternalRequest(id) || id.startsWith("data:"))
|
|
625
|
+
if (isInternalRequest(id) || id.startsWith("data:")) {
|
|
526
626
|
return false;
|
|
627
|
+
}
|
|
527
628
|
const transformMode = ((_a = this.state.environment) == null ? void 0 : _a.transformMode) ?? "ssr";
|
|
528
629
|
return transformMode === "ssr" ? !isNodeBuiltin(id) : !id.startsWith("node:");
|
|
529
630
|
}
|
|
@@ -531,10 +632,12 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
531
632
|
return super.resolveUrl(id, importer);
|
|
532
633
|
}
|
|
533
634
|
async resolveUrl(id, importer) {
|
|
534
|
-
if (VitestMocker.pendingIds.length)
|
|
635
|
+
if (VitestMocker.pendingIds.length) {
|
|
535
636
|
await this.mocker.resolveMocks();
|
|
536
|
-
|
|
637
|
+
}
|
|
638
|
+
if (importer && importer.startsWith("mock:")) {
|
|
537
639
|
importer = importer.slice(5);
|
|
640
|
+
}
|
|
538
641
|
try {
|
|
539
642
|
return await super.resolveUrl(id, importer);
|
|
540
643
|
} catch (error) {
|
|
@@ -542,17 +645,21 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
542
645
|
const { id: id2 } = error[Symbol.for("vitest.error.not_found.data")];
|
|
543
646
|
const path = this.mocker.normalizePath(id2);
|
|
544
647
|
const mock = this.mocker.getDependencyMock(path);
|
|
545
|
-
if (mock !== void 0)
|
|
648
|
+
if (mock !== void 0) {
|
|
546
649
|
return [id2, id2];
|
|
650
|
+
}
|
|
547
651
|
}
|
|
548
652
|
throw error;
|
|
549
653
|
}
|
|
550
654
|
}
|
|
551
655
|
async runModule(context, transformed) {
|
|
552
656
|
const vmContext = this.options.context;
|
|
553
|
-
if (!vmContext || !this.externalModules)
|
|
657
|
+
if (!vmContext || !this.externalModules) {
|
|
554
658
|
return super.runModule(context, transformed);
|
|
555
|
-
|
|
659
|
+
}
|
|
660
|
+
const codeDefinition = `'use strict';async (${Object.keys(context).join(
|
|
661
|
+
","
|
|
662
|
+
)})=>{{`;
|
|
556
663
|
const code = `${codeDefinition}${transformed}
|
|
557
664
|
}}`;
|
|
558
665
|
const options = {
|
|
@@ -568,25 +675,32 @@ class VitestExecutor extends ViteNodeRunner {
|
|
|
568
675
|
await fn(...Object.values(context));
|
|
569
676
|
}
|
|
570
677
|
async importExternalModule(path) {
|
|
571
|
-
if (this.externalModules)
|
|
678
|
+
if (this.externalModules) {
|
|
572
679
|
return this.externalModules.import(path);
|
|
680
|
+
}
|
|
573
681
|
return super.importExternalModule(path);
|
|
574
682
|
}
|
|
575
683
|
async dependencyRequest(id, fsPath, callstack) {
|
|
576
684
|
const mocked = await this.mocker.requestWithMock(fsPath, callstack);
|
|
577
|
-
if (typeof mocked === "string")
|
|
685
|
+
if (typeof mocked === "string") {
|
|
578
686
|
return super.dependencyRequest(mocked, mocked, callstack);
|
|
579
|
-
|
|
687
|
+
}
|
|
688
|
+
if (mocked && typeof mocked === "object") {
|
|
580
689
|
return mocked;
|
|
690
|
+
}
|
|
581
691
|
return super.dependencyRequest(id, fsPath, callstack);
|
|
582
692
|
}
|
|
583
693
|
prepareContext(context) {
|
|
584
694
|
if (this.state.filepath && normalize(this.state.filepath) === normalize(context.__filename)) {
|
|
585
695
|
const globalNamespace = this.options.context || globalThis;
|
|
586
|
-
Object.defineProperty(context.__vite_ssr_import_meta__, "vitest", {
|
|
696
|
+
Object.defineProperty(context.__vite_ssr_import_meta__, "vitest", {
|
|
697
|
+
// @ts-expect-error injected untyped global
|
|
698
|
+
get: () => globalNamespace.__vitest_index__
|
|
699
|
+
});
|
|
587
700
|
}
|
|
588
|
-
if (this.options.context && this.externalModules)
|
|
701
|
+
if (this.options.context && this.externalModules) {
|
|
589
702
|
context.require = this.externalModules.createRequire(context.__filename);
|
|
703
|
+
}
|
|
590
704
|
return context;
|
|
591
705
|
}
|
|
592
706
|
}
|