vitest 0.32.4 → 0.34.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 +0 -83
- package/dist/browser.d.ts +3 -2
- package/dist/browser.js +4 -8
- package/dist/child.js +43 -29
- package/dist/{chunk-api-setup.8f785c4a.js → chunk-api-setup.644415c3.js} +13 -7
- package/dist/{chunk-install-pkg.3fc886f2.js → chunk-install-pkg.dd70081b.js} +14 -13
- package/dist/{chunk-integrations-globals.0093e2ed.js → chunk-integrations-globals.877c84db.js} +7 -6
- package/dist/chunk-runtime-console.ea222ffb.js +108 -0
- package/dist/cli.js +15 -16
- package/dist/config.cjs +6 -5
- package/dist/config.d.ts +15 -19
- package/dist/config.js +6 -5
- package/dist/coverage.d.ts +3 -2
- package/dist/entry-vm.js +60 -0
- package/dist/entry.js +34 -213
- package/dist/environments.d.ts +3 -2
- package/dist/environments.js +4 -2
- package/dist/execute.js +15 -0
- package/dist/index.d.ts +12 -7
- package/dist/index.js +8 -7
- package/dist/loader.js +6 -4
- package/dist/node.d.ts +6 -53
- package/dist/node.js +17 -20
- package/dist/runners.d.ts +3 -2
- package/dist/runners.js +6 -5
- package/dist/{types-198fd1d9.d.ts → types-3c7dbfa5.d.ts} +139 -121
- package/dist/vendor-base.9c08bbd0.js +96 -0
- package/dist/{vendor-coverage.2e41927a.js → vendor-coverage.78040316.js} +1 -2
- package/dist/vendor-date.6e993429.js +50 -0
- package/dist/{vendor-environments.75f2b63d.js → vendor-environments.443ecd82.js} +202 -3
- package/dist/vendor-execute.9ab1c1a7.js +978 -0
- package/dist/{vendor-global.6795f91f.js → vendor-global.97e4527c.js} +2 -1
- package/dist/{vendor-index.23ac4e13.js → vendor-index.087d1af7.js} +2 -20
- package/dist/vendor-index.9378c9a4.js +89 -0
- package/dist/vendor-index.b271ebe4.js +92 -0
- package/dist/{vendor-index.2af39fbb.js → vendor-index.eff408fd.js} +2 -3
- package/dist/{vendor-cli-api.cb31e1db.js → vendor-node.00226ab1.js} +1671 -1882
- package/dist/{vendor-rpc.ad5b08c7.js → vendor-rpc.cbd8e972.js} +7 -4
- package/dist/{vendor-run-once.1fa85ba7.js → vendor-run-once.3e5ef7d7.js} +1 -2
- package/dist/vendor-source-map.e6c1997b.js +747 -0
- package/dist/{vendor-vi.dd6706cb.js → vendor-vi.271667ef.js} +26 -59
- package/dist/vm.js +114 -0
- package/dist/worker.js +39 -27
- package/execute.d.ts +1 -0
- package/package.json +14 -11
- package/suppress-warnings.cjs +2 -0
- package/dist/vendor-execute.3576af13.js +0 -421
- package/dist/vendor-index.cc463d9e.js +0 -189
- package/dist/vendor-tasks.f9d75aed.js +0 -14
|
@@ -0,0 +1,978 @@
|
|
|
1
|
+
import { pathToFileURL, fileURLToPath } from 'node:url';
|
|
2
|
+
import vm from 'node:vm';
|
|
3
|
+
import { ModuleCacheMap, ViteNodeRunner, DEFAULT_REQUEST_STUBS } from 'vite-node/client';
|
|
4
|
+
import { isNodeBuiltin, getCachedData, setCacheData, isInternalRequest, isPrimitive } from 'vite-node/utils';
|
|
5
|
+
import { resolve, isAbsolute, dirname, join, basename, extname, normalize, relative } from 'pathe';
|
|
6
|
+
import { processError } from '@vitest/utils/error';
|
|
7
|
+
import { d as distDir } from './vendor-paths.84fc7a99.js';
|
|
8
|
+
import { g as getWorkerState } from './vendor-global.97e4527c.js';
|
|
9
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
10
|
+
import { getColors, getType } from '@vitest/utils';
|
|
11
|
+
import { b as getAllMockableProperties } from './vendor-base.9c08bbd0.js';
|
|
12
|
+
import { dirname as dirname$1 } from 'node:path';
|
|
13
|
+
import { createRequire, Module } from 'node:module';
|
|
14
|
+
import { CSS_LANGS_RE, KNOWN_ASSET_RE } from 'vite-node/constants';
|
|
15
|
+
|
|
16
|
+
const spyModulePath = resolve(distDir, "spy.js");
|
|
17
|
+
class RefTracker {
|
|
18
|
+
idMap = /* @__PURE__ */ new Map();
|
|
19
|
+
mockedValueMap = /* @__PURE__ */ new Map();
|
|
20
|
+
getId(value) {
|
|
21
|
+
return this.idMap.get(value);
|
|
22
|
+
}
|
|
23
|
+
getMockedValue(id) {
|
|
24
|
+
return this.mockedValueMap.get(id);
|
|
25
|
+
}
|
|
26
|
+
track(originalValue, mockedValue) {
|
|
27
|
+
const newId = this.idMap.size;
|
|
28
|
+
this.idMap.set(originalValue, newId);
|
|
29
|
+
this.mockedValueMap.set(newId, mockedValue);
|
|
30
|
+
return newId;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function isSpecialProp(prop, parentType) {
|
|
34
|
+
return parentType.includes("Function") && typeof prop === "string" && ["arguments", "callee", "caller", "length", "name"].includes(prop);
|
|
35
|
+
}
|
|
36
|
+
class VitestMocker {
|
|
37
|
+
constructor(executor) {
|
|
38
|
+
this.executor = executor;
|
|
39
|
+
const context = this.executor.options.context;
|
|
40
|
+
if (context)
|
|
41
|
+
this.primitives = vm.runInContext("({ Object, Error, Function, RegExp, Symbol, Array, Map })", context);
|
|
42
|
+
else
|
|
43
|
+
this.primitives = { Object, Error, Function, RegExp, Symbol: globalThis.Symbol, Array, Map };
|
|
44
|
+
const Symbol2 = this.primitives.Symbol;
|
|
45
|
+
this.filterPublicKeys = ["__esModule", Symbol2.asyncIterator, Symbol2.hasInstance, Symbol2.isConcatSpreadable, Symbol2.iterator, Symbol2.match, Symbol2.matchAll, Symbol2.replace, Symbol2.search, Symbol2.split, Symbol2.species, Symbol2.toPrimitive, Symbol2.toStringTag, Symbol2.unscopables];
|
|
46
|
+
}
|
|
47
|
+
static pendingIds = [];
|
|
48
|
+
spyModule;
|
|
49
|
+
resolveCache = /* @__PURE__ */ new Map();
|
|
50
|
+
primitives;
|
|
51
|
+
filterPublicKeys;
|
|
52
|
+
get root() {
|
|
53
|
+
return this.executor.options.root;
|
|
54
|
+
}
|
|
55
|
+
get mockMap() {
|
|
56
|
+
return this.executor.options.mockMap;
|
|
57
|
+
}
|
|
58
|
+
get moduleCache() {
|
|
59
|
+
return this.executor.moduleCache;
|
|
60
|
+
}
|
|
61
|
+
get moduleDirectories() {
|
|
62
|
+
return this.executor.options.moduleDirectories || [];
|
|
63
|
+
}
|
|
64
|
+
async initializeSpyModule() {
|
|
65
|
+
this.spyModule = await this.executor.executeId(spyModulePath);
|
|
66
|
+
}
|
|
67
|
+
deleteCachedItem(id) {
|
|
68
|
+
const mockId = this.getMockPath(id);
|
|
69
|
+
if (this.moduleCache.has(mockId))
|
|
70
|
+
this.moduleCache.delete(mockId);
|
|
71
|
+
}
|
|
72
|
+
isAModuleDirectory(path) {
|
|
73
|
+
return this.moduleDirectories.some((dir) => path.includes(dir));
|
|
74
|
+
}
|
|
75
|
+
getSuiteFilepath() {
|
|
76
|
+
return this.executor.state.filepath || "global";
|
|
77
|
+
}
|
|
78
|
+
createError(message) {
|
|
79
|
+
const Error2 = this.primitives.Error;
|
|
80
|
+
return new Error2(message);
|
|
81
|
+
}
|
|
82
|
+
getMocks() {
|
|
83
|
+
const suite = this.getSuiteFilepath();
|
|
84
|
+
const suiteMocks = this.mockMap.get(suite);
|
|
85
|
+
const globalMocks = this.mockMap.get("global");
|
|
86
|
+
return {
|
|
87
|
+
...globalMocks,
|
|
88
|
+
...suiteMocks
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
async resolvePath(rawId, importer) {
|
|
92
|
+
let id;
|
|
93
|
+
let fsPath;
|
|
94
|
+
try {
|
|
95
|
+
[id, fsPath] = await this.executor.originalResolveUrl(rawId, importer);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (error.code === "ERR_MODULE_NOT_FOUND") {
|
|
98
|
+
const { id: unresolvedId } = error[Symbol.for("vitest.error.not_found.data")];
|
|
99
|
+
id = unresolvedId;
|
|
100
|
+
fsPath = unresolvedId;
|
|
101
|
+
} else {
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const external = !isAbsolute(fsPath) || this.isAModuleDirectory(fsPath) ? rawId : null;
|
|
106
|
+
return {
|
|
107
|
+
id,
|
|
108
|
+
fsPath,
|
|
109
|
+
external
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async resolveMocks() {
|
|
113
|
+
if (!VitestMocker.pendingIds.length)
|
|
114
|
+
return;
|
|
115
|
+
await Promise.all(VitestMocker.pendingIds.map(async (mock) => {
|
|
116
|
+
const { fsPath, external } = await this.resolvePath(mock.id, mock.importer);
|
|
117
|
+
if (mock.type === "unmock")
|
|
118
|
+
this.unmockPath(fsPath);
|
|
119
|
+
if (mock.type === "mock")
|
|
120
|
+
this.mockPath(mock.id, fsPath, external, mock.factory);
|
|
121
|
+
}));
|
|
122
|
+
VitestMocker.pendingIds = [];
|
|
123
|
+
}
|
|
124
|
+
async callFunctionMock(dep, mock) {
|
|
125
|
+
var _a, _b;
|
|
126
|
+
const cached = (_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports;
|
|
127
|
+
if (cached)
|
|
128
|
+
return cached;
|
|
129
|
+
let exports;
|
|
130
|
+
try {
|
|
131
|
+
exports = await mock();
|
|
132
|
+
} catch (err) {
|
|
133
|
+
const vitestError = this.createError(
|
|
134
|
+
'[vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock'
|
|
135
|
+
);
|
|
136
|
+
vitestError.cause = err;
|
|
137
|
+
throw vitestError;
|
|
138
|
+
}
|
|
139
|
+
const filepath = dep.slice(5);
|
|
140
|
+
const mockpath = ((_b = this.resolveCache.get(this.getSuiteFilepath())) == null ? void 0 : _b[filepath]) || filepath;
|
|
141
|
+
if (exports === null || typeof exports !== "object")
|
|
142
|
+
throw this.createError(`[vitest] vi.mock("${mockpath}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`);
|
|
143
|
+
const moduleExports = new Proxy(exports, {
|
|
144
|
+
get: (target, prop) => {
|
|
145
|
+
const val = target[prop];
|
|
146
|
+
if (prop === "then") {
|
|
147
|
+
if (target instanceof Promise)
|
|
148
|
+
return target.then.bind(target);
|
|
149
|
+
} else if (!(prop in target)) {
|
|
150
|
+
if (this.filterPublicKeys.includes(prop))
|
|
151
|
+
return void 0;
|
|
152
|
+
const c = getColors();
|
|
153
|
+
throw this.createError(
|
|
154
|
+
`[vitest] No "${String(prop)}" export is defined on the "${mockpath}" mock. Did you forget to return it from "vi.mock"?
|
|
155
|
+
If you need to partially mock a module, you can use "vi.importActual" inside:
|
|
156
|
+
|
|
157
|
+
${c.green(`vi.mock("${mockpath}", async () => {
|
|
158
|
+
const actual = await vi.importActual("${mockpath}")
|
|
159
|
+
return {
|
|
160
|
+
...actual,
|
|
161
|
+
// your mocked methods
|
|
162
|
+
},
|
|
163
|
+
})`)}
|
|
164
|
+
`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
return val;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
this.moduleCache.set(dep, { exports: moduleExports });
|
|
171
|
+
return moduleExports;
|
|
172
|
+
}
|
|
173
|
+
getMockPath(dep) {
|
|
174
|
+
return `mock:${dep}`;
|
|
175
|
+
}
|
|
176
|
+
getDependencyMock(id) {
|
|
177
|
+
return this.getMocks()[id];
|
|
178
|
+
}
|
|
179
|
+
normalizePath(path) {
|
|
180
|
+
return this.moduleCache.normalizePath(path);
|
|
181
|
+
}
|
|
182
|
+
resolveMockPath(mockPath, external) {
|
|
183
|
+
const path = external || mockPath;
|
|
184
|
+
if (external || isNodeBuiltin(mockPath) || !existsSync(mockPath)) {
|
|
185
|
+
const mockDirname = dirname(path);
|
|
186
|
+
const mockFolder = join(this.root, "__mocks__", mockDirname);
|
|
187
|
+
if (!existsSync(mockFolder))
|
|
188
|
+
return null;
|
|
189
|
+
const files = readdirSync(mockFolder);
|
|
190
|
+
const baseOriginal = basename(path);
|
|
191
|
+
for (const file of files) {
|
|
192
|
+
const baseFile = basename(file, extname(file));
|
|
193
|
+
if (baseFile === baseOriginal)
|
|
194
|
+
return resolve(mockFolder, file);
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
const dir = dirname(path);
|
|
199
|
+
const baseId = basename(path);
|
|
200
|
+
const fullPath = resolve(dir, "__mocks__", baseId);
|
|
201
|
+
return existsSync(fullPath) ? fullPath : null;
|
|
202
|
+
}
|
|
203
|
+
mockObject(object, mockExports = {}) {
|
|
204
|
+
const finalizers = new Array();
|
|
205
|
+
const refs = new RefTracker();
|
|
206
|
+
const define = (container, key, value) => {
|
|
207
|
+
try {
|
|
208
|
+
container[key] = value;
|
|
209
|
+
return true;
|
|
210
|
+
} catch {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
const mockPropertiesOf = (container, newContainer) => {
|
|
215
|
+
const containerType = getType(container);
|
|
216
|
+
const isModule = containerType === "Module" || !!container.__esModule;
|
|
217
|
+
for (const { key: property, descriptor } of getAllMockableProperties(container, isModule, this.primitives)) {
|
|
218
|
+
if (!isModule && descriptor.get) {
|
|
219
|
+
try {
|
|
220
|
+
Object.defineProperty(newContainer, property, descriptor);
|
|
221
|
+
} catch (error) {
|
|
222
|
+
}
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (isSpecialProp(property, containerType))
|
|
226
|
+
continue;
|
|
227
|
+
const value = container[property];
|
|
228
|
+
const refId = refs.getId(value);
|
|
229
|
+
if (refId !== void 0) {
|
|
230
|
+
finalizers.push(() => define(newContainer, property, refs.getMockedValue(refId)));
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
const type = getType(value);
|
|
234
|
+
if (Array.isArray(value)) {
|
|
235
|
+
define(newContainer, property, []);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const isFunction = type.includes("Function") && typeof value === "function";
|
|
239
|
+
if ((!isFunction || value.__isMockFunction) && type !== "Object" && type !== "Module") {
|
|
240
|
+
define(newContainer, property, value);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (!define(newContainer, property, isFunction ? value : {}))
|
|
244
|
+
continue;
|
|
245
|
+
if (isFunction) {
|
|
246
|
+
const spyModule = this.spyModule;
|
|
247
|
+
if (!spyModule)
|
|
248
|
+
throw this.createError("[vitest] `spyModule` is not defined. This is Vitest error. Please open a new issue with reproduction.");
|
|
249
|
+
const mock = spyModule.spyOn(newContainer, property).mockImplementation(() => void 0);
|
|
250
|
+
mock.mockRestore = () => {
|
|
251
|
+
mock.mockReset();
|
|
252
|
+
mock.mockImplementation(() => void 0);
|
|
253
|
+
return mock;
|
|
254
|
+
};
|
|
255
|
+
Object.defineProperty(newContainer[property], "length", { value: 0 });
|
|
256
|
+
}
|
|
257
|
+
refs.track(value, newContainer[property]);
|
|
258
|
+
mockPropertiesOf(value, newContainer[property]);
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
const mockedObject = mockExports;
|
|
262
|
+
mockPropertiesOf(object, mockedObject);
|
|
263
|
+
for (const finalizer of finalizers)
|
|
264
|
+
finalizer();
|
|
265
|
+
return mockedObject;
|
|
266
|
+
}
|
|
267
|
+
unmockPath(path) {
|
|
268
|
+
const suitefile = this.getSuiteFilepath();
|
|
269
|
+
const id = this.normalizePath(path);
|
|
270
|
+
const mock = this.mockMap.get(suitefile);
|
|
271
|
+
if (mock && id in mock)
|
|
272
|
+
delete mock[id];
|
|
273
|
+
this.deleteCachedItem(id);
|
|
274
|
+
}
|
|
275
|
+
mockPath(originalId, path, external, factory) {
|
|
276
|
+
const suitefile = this.getSuiteFilepath();
|
|
277
|
+
const id = this.normalizePath(path);
|
|
278
|
+
const mocks = this.mockMap.get(suitefile) || {};
|
|
279
|
+
const resolves = this.resolveCache.get(suitefile) || {};
|
|
280
|
+
mocks[id] = factory || this.resolveMockPath(path, external);
|
|
281
|
+
resolves[id] = originalId;
|
|
282
|
+
this.mockMap.set(suitefile, mocks);
|
|
283
|
+
this.resolveCache.set(suitefile, resolves);
|
|
284
|
+
this.deleteCachedItem(id);
|
|
285
|
+
}
|
|
286
|
+
async importActual(rawId, importee) {
|
|
287
|
+
const { id, fsPath } = await this.resolvePath(rawId, importee);
|
|
288
|
+
const result = await this.executor.cachedRequest(id, fsPath, [importee]);
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
async importMock(rawId, importee) {
|
|
292
|
+
const { id, fsPath, external } = await this.resolvePath(rawId, importee);
|
|
293
|
+
const normalizedId = this.normalizePath(fsPath);
|
|
294
|
+
let mock = this.getDependencyMock(normalizedId);
|
|
295
|
+
if (mock === void 0)
|
|
296
|
+
mock = this.resolveMockPath(fsPath, external);
|
|
297
|
+
if (mock === null) {
|
|
298
|
+
const mod = await this.executor.cachedRequest(id, fsPath, [importee]);
|
|
299
|
+
return this.mockObject(mod);
|
|
300
|
+
}
|
|
301
|
+
if (typeof mock === "function")
|
|
302
|
+
return this.callFunctionMock(fsPath, mock);
|
|
303
|
+
return this.executor.dependencyRequest(mock, mock, [importee]);
|
|
304
|
+
}
|
|
305
|
+
async requestWithMock(url, callstack) {
|
|
306
|
+
const id = this.normalizePath(url);
|
|
307
|
+
const mock = this.getDependencyMock(id);
|
|
308
|
+
const mockPath = this.getMockPath(id);
|
|
309
|
+
if (mock === null) {
|
|
310
|
+
const cache = this.moduleCache.get(mockPath);
|
|
311
|
+
if (cache.exports)
|
|
312
|
+
return cache.exports;
|
|
313
|
+
const exports = {};
|
|
314
|
+
this.moduleCache.set(mockPath, { exports });
|
|
315
|
+
const mod = await this.executor.directRequest(url, url, callstack);
|
|
316
|
+
this.mockObject(mod, exports);
|
|
317
|
+
return exports;
|
|
318
|
+
}
|
|
319
|
+
if (typeof mock === "function" && !callstack.includes(mockPath) && !callstack.includes(url)) {
|
|
320
|
+
callstack.push(mockPath);
|
|
321
|
+
const result = await this.callFunctionMock(mockPath, mock);
|
|
322
|
+
const indexMock = callstack.indexOf(mockPath);
|
|
323
|
+
callstack.splice(indexMock, 1);
|
|
324
|
+
return result;
|
|
325
|
+
}
|
|
326
|
+
if (typeof mock === "string" && !callstack.includes(mock))
|
|
327
|
+
return mock;
|
|
328
|
+
}
|
|
329
|
+
queueMock(id, importer, factory) {
|
|
330
|
+
VitestMocker.pendingIds.push({ type: "mock", id, importer, factory });
|
|
331
|
+
}
|
|
332
|
+
queueUnmock(id, importer) {
|
|
333
|
+
VitestMocker.pendingIds.push({ type: "unmock", id, importer });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const SyntheticModule = vm.SyntheticModule;
|
|
338
|
+
const SourceTextModule = vm.SourceTextModule;
|
|
339
|
+
const _require = createRequire(import.meta.url);
|
|
340
|
+
const nativeResolve = import.meta.resolve;
|
|
341
|
+
const dataURIRegex = /^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/;
|
|
342
|
+
class ExternalModulesExecutor {
|
|
343
|
+
constructor(options) {
|
|
344
|
+
this.options = options;
|
|
345
|
+
this.context = options.context;
|
|
346
|
+
const primitives = vm.runInContext("({ Object, Array, Error })", this.context);
|
|
347
|
+
this.primitives = primitives;
|
|
348
|
+
const executor = this;
|
|
349
|
+
this.Module = class Module$1 {
|
|
350
|
+
exports;
|
|
351
|
+
isPreloading = false;
|
|
352
|
+
require;
|
|
353
|
+
id;
|
|
354
|
+
filename;
|
|
355
|
+
loaded;
|
|
356
|
+
parent;
|
|
357
|
+
children = [];
|
|
358
|
+
path;
|
|
359
|
+
paths = [];
|
|
360
|
+
constructor(id, parent) {
|
|
361
|
+
this.exports = primitives.Object.create(Object.prototype);
|
|
362
|
+
this.require = Module$1.createRequire(id);
|
|
363
|
+
this.path = dirname$1(id);
|
|
364
|
+
this.id = id;
|
|
365
|
+
this.filename = id;
|
|
366
|
+
this.loaded = false;
|
|
367
|
+
this.parent = parent;
|
|
368
|
+
}
|
|
369
|
+
_compile(code, filename) {
|
|
370
|
+
const cjsModule = Module$1.wrap(code);
|
|
371
|
+
const script = new vm.Script(cjsModule, {
|
|
372
|
+
filename,
|
|
373
|
+
importModuleDynamically: executor.importModuleDynamically
|
|
374
|
+
});
|
|
375
|
+
script.identifier = filename;
|
|
376
|
+
const fn = script.runInContext(executor.context);
|
|
377
|
+
const __dirname = dirname$1(filename);
|
|
378
|
+
executor.requireCache[filename] = this;
|
|
379
|
+
try {
|
|
380
|
+
fn(this.exports, this.require, this, filename, __dirname);
|
|
381
|
+
return this.exports;
|
|
382
|
+
} finally {
|
|
383
|
+
this.loaded = true;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// exposed for external use, Node.js does the opposite
|
|
387
|
+
static _load = (request, parent, _isMain) => {
|
|
388
|
+
const require = Module$1.createRequire((parent == null ? void 0 : parent.filename) ?? request);
|
|
389
|
+
return require(request);
|
|
390
|
+
};
|
|
391
|
+
static wrap = (script) => {
|
|
392
|
+
return Module$1.wrapper[0] + script + Module$1.wrapper[1];
|
|
393
|
+
};
|
|
394
|
+
static wrapper = new primitives.Array(
|
|
395
|
+
"(function (exports, require, module, __filename, __dirname) { ",
|
|
396
|
+
"\n});"
|
|
397
|
+
);
|
|
398
|
+
static builtinModules = Module.builtinModules;
|
|
399
|
+
static findSourceMap = Module.findSourceMap;
|
|
400
|
+
static SourceMap = Module.SourceMap;
|
|
401
|
+
static syncBuiltinESMExports = Module.syncBuiltinESMExports;
|
|
402
|
+
static _cache = executor.moduleCache;
|
|
403
|
+
static _extensions = executor.extensions;
|
|
404
|
+
static createRequire = (filename) => {
|
|
405
|
+
return executor.createRequire(filename);
|
|
406
|
+
};
|
|
407
|
+
static runMain = () => {
|
|
408
|
+
throw new primitives.Error('[vitest] "runMain" is not implemented.');
|
|
409
|
+
};
|
|
410
|
+
// @ts-expect-error not typed
|
|
411
|
+
static _resolveFilename = Module._resolveFilename;
|
|
412
|
+
// @ts-expect-error not typed
|
|
413
|
+
static _findPath = Module._findPath;
|
|
414
|
+
// @ts-expect-error not typed
|
|
415
|
+
static _initPaths = Module._initPaths;
|
|
416
|
+
// @ts-expect-error not typed
|
|
417
|
+
static _preloadModules = Module._preloadModules;
|
|
418
|
+
// @ts-expect-error not typed
|
|
419
|
+
static _resolveLookupPaths = Module._resolveLookupPaths;
|
|
420
|
+
// @ts-expect-error not typed
|
|
421
|
+
static globalPaths = Module.globalPaths;
|
|
422
|
+
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
|
|
423
|
+
// @ts-ignore not typed in lower versions
|
|
424
|
+
static isBuiltin = Module.isBuiltin;
|
|
425
|
+
static Module = Module$1;
|
|
426
|
+
};
|
|
427
|
+
this.extensions[".js"] = this.requireJs;
|
|
428
|
+
this.extensions[".json"] = this.requireJson;
|
|
429
|
+
}
|
|
430
|
+
requireCache = /* @__PURE__ */ Object.create(null);
|
|
431
|
+
builtinCache = /* @__PURE__ */ Object.create(null);
|
|
432
|
+
moduleCache = /* @__PURE__ */ new Map();
|
|
433
|
+
extensions = /* @__PURE__ */ Object.create(null);
|
|
434
|
+
esmLinkMap = /* @__PURE__ */ new WeakMap();
|
|
435
|
+
context;
|
|
436
|
+
fsCache = /* @__PURE__ */ new Map();
|
|
437
|
+
fsBufferCache = /* @__PURE__ */ new Map();
|
|
438
|
+
Module;
|
|
439
|
+
primitives;
|
|
440
|
+
requireJs = (m, filename) => {
|
|
441
|
+
const content = this.readFile(filename);
|
|
442
|
+
m._compile(content, filename);
|
|
443
|
+
};
|
|
444
|
+
requireJson = (m, filename) => {
|
|
445
|
+
const code = this.readFile(filename);
|
|
446
|
+
m.exports = JSON.parse(code);
|
|
447
|
+
};
|
|
448
|
+
importModuleDynamically = async (specifier, referencer) => {
|
|
449
|
+
const module = await this.resolveModule(specifier, referencer.identifier);
|
|
450
|
+
return this.evaluateModule(module);
|
|
451
|
+
};
|
|
452
|
+
resolveModule = async (specifier, referencer) => {
|
|
453
|
+
const identifier = await this.resolveAsync(specifier, referencer);
|
|
454
|
+
return await this.createModule(identifier);
|
|
455
|
+
};
|
|
456
|
+
async resolveAsync(specifier, parent) {
|
|
457
|
+
return nativeResolve(specifier, parent);
|
|
458
|
+
}
|
|
459
|
+
readFile(path) {
|
|
460
|
+
const cached = this.fsCache.get(path);
|
|
461
|
+
if (cached)
|
|
462
|
+
return cached;
|
|
463
|
+
const source = readFileSync(path, "utf-8");
|
|
464
|
+
this.fsCache.set(path, source);
|
|
465
|
+
return source;
|
|
466
|
+
}
|
|
467
|
+
readBuffer(path) {
|
|
468
|
+
const cached = this.fsBufferCache.get(path);
|
|
469
|
+
if (cached)
|
|
470
|
+
return cached;
|
|
471
|
+
const buffer = readFileSync(path);
|
|
472
|
+
this.fsBufferCache.set(path, buffer);
|
|
473
|
+
return buffer;
|
|
474
|
+
}
|
|
475
|
+
findNearestPackageData(basedir) {
|
|
476
|
+
var _a;
|
|
477
|
+
const originalBasedir = basedir;
|
|
478
|
+
const packageCache = this.options.packageCache;
|
|
479
|
+
while (basedir) {
|
|
480
|
+
const cached = getCachedData(packageCache, basedir, originalBasedir);
|
|
481
|
+
if (cached)
|
|
482
|
+
return cached;
|
|
483
|
+
const pkgPath = join(basedir, "package.json");
|
|
484
|
+
try {
|
|
485
|
+
if ((_a = statSync(pkgPath, { throwIfNoEntry: false })) == null ? void 0 : _a.isFile()) {
|
|
486
|
+
const pkgData = JSON.parse(this.readFile(pkgPath));
|
|
487
|
+
if (packageCache)
|
|
488
|
+
setCacheData(packageCache, pkgData, basedir, originalBasedir);
|
|
489
|
+
return pkgData;
|
|
490
|
+
}
|
|
491
|
+
} catch {
|
|
492
|
+
}
|
|
493
|
+
const nextBasedir = dirname$1(basedir);
|
|
494
|
+
if (nextBasedir === basedir)
|
|
495
|
+
break;
|
|
496
|
+
basedir = nextBasedir;
|
|
497
|
+
}
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
wrapSynteticModule(identifier, exports) {
|
|
501
|
+
const moduleKeys = Object.keys(exports).filter((key) => key !== "default");
|
|
502
|
+
const m = new SyntheticModule(
|
|
503
|
+
[...moduleKeys, "default"],
|
|
504
|
+
() => {
|
|
505
|
+
for (const key of moduleKeys)
|
|
506
|
+
m.setExport(key, exports[key]);
|
|
507
|
+
m.setExport("default", exports);
|
|
508
|
+
},
|
|
509
|
+
{
|
|
510
|
+
context: this.context,
|
|
511
|
+
identifier
|
|
512
|
+
}
|
|
513
|
+
);
|
|
514
|
+
return m;
|
|
515
|
+
}
|
|
516
|
+
async evaluateModule(m) {
|
|
517
|
+
if (m.status === "unlinked") {
|
|
518
|
+
this.esmLinkMap.set(
|
|
519
|
+
m,
|
|
520
|
+
m.link(
|
|
521
|
+
(identifier, referencer) => this.resolveModule(identifier, referencer.identifier)
|
|
522
|
+
)
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
await this.esmLinkMap.get(m);
|
|
526
|
+
if (m.status === "linked")
|
|
527
|
+
await m.evaluate();
|
|
528
|
+
return m;
|
|
529
|
+
}
|
|
530
|
+
findLongestRegisteredExtension(filename) {
|
|
531
|
+
const name = basename(filename);
|
|
532
|
+
let currentExtension;
|
|
533
|
+
let index;
|
|
534
|
+
let startIndex = 0;
|
|
535
|
+
while ((index = name.indexOf(".", startIndex)) !== -1) {
|
|
536
|
+
startIndex = index + 1;
|
|
537
|
+
if (index === 0)
|
|
538
|
+
continue;
|
|
539
|
+
currentExtension = name.slice(index);
|
|
540
|
+
if (this.extensions[currentExtension])
|
|
541
|
+
return currentExtension;
|
|
542
|
+
}
|
|
543
|
+
return ".js";
|
|
544
|
+
}
|
|
545
|
+
createRequire = (filename) => {
|
|
546
|
+
const _require2 = createRequire(filename);
|
|
547
|
+
const require = (id) => {
|
|
548
|
+
const resolved = _require2.resolve(id);
|
|
549
|
+
const ext = extname(resolved);
|
|
550
|
+
if (ext === ".node" || isNodeBuiltin(resolved))
|
|
551
|
+
return this.requireCoreModule(resolved);
|
|
552
|
+
const module = this.createCommonJSNodeModule(resolved);
|
|
553
|
+
return this.loadCommonJSModule(module, resolved);
|
|
554
|
+
};
|
|
555
|
+
require.resolve = _require2.resolve;
|
|
556
|
+
Object.defineProperty(require, "extensions", {
|
|
557
|
+
get: () => this.extensions,
|
|
558
|
+
set: () => {
|
|
559
|
+
},
|
|
560
|
+
configurable: true
|
|
561
|
+
});
|
|
562
|
+
require.main = _require2.main;
|
|
563
|
+
require.cache = this.requireCache;
|
|
564
|
+
return require;
|
|
565
|
+
};
|
|
566
|
+
createCommonJSNodeModule(filename) {
|
|
567
|
+
return new this.Module(filename);
|
|
568
|
+
}
|
|
569
|
+
// very naive implementation for Node.js require
|
|
570
|
+
loadCommonJSModule(module, filename) {
|
|
571
|
+
const cached = this.requireCache[filename];
|
|
572
|
+
if (cached)
|
|
573
|
+
return cached.exports;
|
|
574
|
+
const extension = this.findLongestRegisteredExtension(filename);
|
|
575
|
+
const loader = this.extensions[extension] || this.extensions[".js"];
|
|
576
|
+
loader(module, filename);
|
|
577
|
+
return module.exports;
|
|
578
|
+
}
|
|
579
|
+
async createEsmModule(fileUrl, code) {
|
|
580
|
+
const cached = this.moduleCache.get(fileUrl);
|
|
581
|
+
if (cached)
|
|
582
|
+
return cached;
|
|
583
|
+
const [urlPath] = fileUrl.split("?");
|
|
584
|
+
if (CSS_LANGS_RE.test(urlPath) || KNOWN_ASSET_RE.test(urlPath)) {
|
|
585
|
+
const path = normalize(urlPath);
|
|
586
|
+
let name = path.split("/node_modules/").pop() || "";
|
|
587
|
+
if (name == null ? void 0 : name.startsWith("@"))
|
|
588
|
+
name = name.split("/").slice(0, 2).join("/");
|
|
589
|
+
else
|
|
590
|
+
name = name.split("/")[0];
|
|
591
|
+
const ext = extname(path);
|
|
592
|
+
let error = `[vitest] Cannot import ${fileUrl}. At the moment, importing ${ext} files inside external dependencies is not allowed. `;
|
|
593
|
+
if (name) {
|
|
594
|
+
const c = getColors();
|
|
595
|
+
error += `As a temporary workaround you can try to inline the package by updating your config:
|
|
596
|
+
|
|
597
|
+
${c.gray(c.dim("// vitest.config.js"))}
|
|
598
|
+
${c.green(`export default {
|
|
599
|
+
test: {
|
|
600
|
+
deps: {
|
|
601
|
+
optimizer: {
|
|
602
|
+
web: {
|
|
603
|
+
include: [
|
|
604
|
+
${c.yellow(c.bold(`"${name}"`))}
|
|
605
|
+
]
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
`)}`;
|
|
612
|
+
}
|
|
613
|
+
throw new this.primitives.Error(error);
|
|
614
|
+
}
|
|
615
|
+
if (fileUrl.endsWith(".json")) {
|
|
616
|
+
const m2 = new SyntheticModule(
|
|
617
|
+
["default"],
|
|
618
|
+
() => {
|
|
619
|
+
const result = JSON.parse(code);
|
|
620
|
+
m2.setExport("default", result);
|
|
621
|
+
}
|
|
622
|
+
);
|
|
623
|
+
this.moduleCache.set(fileUrl, m2);
|
|
624
|
+
return m2;
|
|
625
|
+
}
|
|
626
|
+
const m = new SourceTextModule(
|
|
627
|
+
code,
|
|
628
|
+
{
|
|
629
|
+
identifier: fileUrl,
|
|
630
|
+
context: this.context,
|
|
631
|
+
importModuleDynamically: this.importModuleDynamically,
|
|
632
|
+
initializeImportMeta: (meta, mod) => {
|
|
633
|
+
meta.url = mod.identifier;
|
|
634
|
+
meta.resolve = (specifier, importer) => {
|
|
635
|
+
return nativeResolve(specifier, importer ?? mod.identifier);
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
);
|
|
640
|
+
this.moduleCache.set(fileUrl, m);
|
|
641
|
+
return m;
|
|
642
|
+
}
|
|
643
|
+
requireCoreModule(identifier) {
|
|
644
|
+
const normalized = identifier.replace(/^node:/, "");
|
|
645
|
+
if (this.builtinCache[normalized])
|
|
646
|
+
return this.builtinCache[normalized].exports;
|
|
647
|
+
const moduleExports = _require(identifier);
|
|
648
|
+
if (identifier === "node:module" || identifier === "module") {
|
|
649
|
+
const module = new this.Module("/module.js");
|
|
650
|
+
module.exports = this.Module;
|
|
651
|
+
this.builtinCache[normalized] = module;
|
|
652
|
+
return module.exports;
|
|
653
|
+
}
|
|
654
|
+
this.builtinCache[normalized] = _require.cache[normalized];
|
|
655
|
+
return moduleExports;
|
|
656
|
+
}
|
|
657
|
+
async loadWebAssemblyModule(source, identifier) {
|
|
658
|
+
const cached = this.moduleCache.get(identifier);
|
|
659
|
+
if (cached)
|
|
660
|
+
return cached;
|
|
661
|
+
const wasmModule = await WebAssembly.compile(source);
|
|
662
|
+
const exports = WebAssembly.Module.exports(wasmModule);
|
|
663
|
+
const imports = WebAssembly.Module.imports(wasmModule);
|
|
664
|
+
const moduleLookup = {};
|
|
665
|
+
for (const { module } of imports) {
|
|
666
|
+
if (moduleLookup[module] === void 0) {
|
|
667
|
+
const resolvedModule = await this.resolveModule(
|
|
668
|
+
module,
|
|
669
|
+
identifier
|
|
670
|
+
);
|
|
671
|
+
moduleLookup[module] = await this.evaluateModule(resolvedModule);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
const syntheticModule = new SyntheticModule(
|
|
675
|
+
exports.map(({ name }) => name),
|
|
676
|
+
() => {
|
|
677
|
+
const importsObject = {};
|
|
678
|
+
for (const { module, name } of imports) {
|
|
679
|
+
if (!importsObject[module])
|
|
680
|
+
importsObject[module] = {};
|
|
681
|
+
importsObject[module][name] = moduleLookup[module].namespace[name];
|
|
682
|
+
}
|
|
683
|
+
const wasmInstance = new WebAssembly.Instance(
|
|
684
|
+
wasmModule,
|
|
685
|
+
importsObject
|
|
686
|
+
);
|
|
687
|
+
for (const { name } of exports)
|
|
688
|
+
syntheticModule.setExport(name, wasmInstance.exports[name]);
|
|
689
|
+
},
|
|
690
|
+
{ context: this.context, identifier }
|
|
691
|
+
);
|
|
692
|
+
return syntheticModule;
|
|
693
|
+
}
|
|
694
|
+
async createDataModule(identifier) {
|
|
695
|
+
const cached = this.moduleCache.get(identifier);
|
|
696
|
+
if (cached)
|
|
697
|
+
return cached;
|
|
698
|
+
const Error = this.primitives.Error;
|
|
699
|
+
const match = identifier.match(dataURIRegex);
|
|
700
|
+
if (!match || !match.groups)
|
|
701
|
+
throw new Error("Invalid data URI");
|
|
702
|
+
const mime = match.groups.mime;
|
|
703
|
+
const encoding = match.groups.encoding;
|
|
704
|
+
if (mime === "application/wasm") {
|
|
705
|
+
if (!encoding)
|
|
706
|
+
throw new Error("Missing data URI encoding");
|
|
707
|
+
if (encoding !== "base64")
|
|
708
|
+
throw new Error(`Invalid data URI encoding: ${encoding}`);
|
|
709
|
+
const module = await this.loadWebAssemblyModule(
|
|
710
|
+
Buffer.from(match.groups.code, "base64"),
|
|
711
|
+
identifier
|
|
712
|
+
);
|
|
713
|
+
this.moduleCache.set(identifier, module);
|
|
714
|
+
return module;
|
|
715
|
+
}
|
|
716
|
+
let code = match.groups.code;
|
|
717
|
+
if (!encoding || encoding === "charset=utf-8")
|
|
718
|
+
code = decodeURIComponent(code);
|
|
719
|
+
else if (encoding === "base64")
|
|
720
|
+
code = Buffer.from(code, "base64").toString();
|
|
721
|
+
else
|
|
722
|
+
throw new Error(`Invalid data URI encoding: ${encoding}`);
|
|
723
|
+
if (mime === "application/json") {
|
|
724
|
+
const module = new SyntheticModule(
|
|
725
|
+
["default"],
|
|
726
|
+
() => {
|
|
727
|
+
const obj = JSON.parse(code);
|
|
728
|
+
module.setExport("default", obj);
|
|
729
|
+
},
|
|
730
|
+
{ context: this.context, identifier }
|
|
731
|
+
);
|
|
732
|
+
this.moduleCache.set(identifier, module);
|
|
733
|
+
return module;
|
|
734
|
+
}
|
|
735
|
+
return this.createEsmModule(identifier, code);
|
|
736
|
+
}
|
|
737
|
+
async createModule(identifier) {
|
|
738
|
+
if (identifier.startsWith("data:"))
|
|
739
|
+
return this.createDataModule(identifier);
|
|
740
|
+
const extension = extname(identifier);
|
|
741
|
+
if (extension === ".node" || isNodeBuiltin(identifier)) {
|
|
742
|
+
const exports2 = this.requireCoreModule(identifier);
|
|
743
|
+
return this.wrapSynteticModule(identifier, exports2);
|
|
744
|
+
}
|
|
745
|
+
const isFileUrl = identifier.startsWith("file://");
|
|
746
|
+
const fileUrl = isFileUrl ? identifier : pathToFileURL(identifier).toString();
|
|
747
|
+
const pathUrl = isFileUrl ? fileURLToPath(identifier.split("?")[0]) : identifier;
|
|
748
|
+
if (extension === ".cjs") {
|
|
749
|
+
const module2 = this.createCommonJSNodeModule(pathUrl);
|
|
750
|
+
const exports2 = this.loadCommonJSModule(module2, pathUrl);
|
|
751
|
+
return this.wrapSynteticModule(fileUrl, exports2);
|
|
752
|
+
}
|
|
753
|
+
if (extension === ".mjs")
|
|
754
|
+
return await this.createEsmModule(fileUrl, this.readFile(pathUrl));
|
|
755
|
+
const pkgData = this.findNearestPackageData(normalize(pathUrl));
|
|
756
|
+
if (pkgData.type === "module")
|
|
757
|
+
return await this.createEsmModule(fileUrl, this.readFile(pathUrl));
|
|
758
|
+
const module = this.createCommonJSNodeModule(pathUrl);
|
|
759
|
+
const exports = this.loadCommonJSModule(module, pathUrl);
|
|
760
|
+
return this.wrapSynteticModule(fileUrl, exports);
|
|
761
|
+
}
|
|
762
|
+
async import(identifier) {
|
|
763
|
+
const module = await this.createModule(identifier);
|
|
764
|
+
await this.evaluateModule(module);
|
|
765
|
+
return module.namespace;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
const entryUrl = pathToFileURL(resolve(distDir, "entry.js")).href;
|
|
770
|
+
async function createVitestExecutor(options) {
|
|
771
|
+
const runner = new VitestExecutor(options);
|
|
772
|
+
await runner.executeId("/@vite/env");
|
|
773
|
+
await runner.mocker.initializeSpyModule();
|
|
774
|
+
return runner;
|
|
775
|
+
}
|
|
776
|
+
let _viteNode;
|
|
777
|
+
const packageCache = /* @__PURE__ */ new Map();
|
|
778
|
+
const moduleCache = new ModuleCacheMap();
|
|
779
|
+
const mockMap = /* @__PURE__ */ new Map();
|
|
780
|
+
async function startViteNode(options) {
|
|
781
|
+
if (_viteNode)
|
|
782
|
+
return _viteNode;
|
|
783
|
+
const executor = await startVitestExecutor(options);
|
|
784
|
+
const { run } = await import(entryUrl);
|
|
785
|
+
_viteNode = { run, executor };
|
|
786
|
+
return _viteNode;
|
|
787
|
+
}
|
|
788
|
+
async function startVitestExecutor(options) {
|
|
789
|
+
const state = () => getWorkerState() || options.state;
|
|
790
|
+
const rpc = () => state().rpc;
|
|
791
|
+
const processExit = process.exit;
|
|
792
|
+
process.exit = (code = process.exitCode || 0) => {
|
|
793
|
+
const error = new Error(`process.exit called with "${code}"`);
|
|
794
|
+
rpc().onWorkerExit(error, code);
|
|
795
|
+
return processExit(code);
|
|
796
|
+
};
|
|
797
|
+
function catchError(err, type) {
|
|
798
|
+
var _a;
|
|
799
|
+
const worker = state();
|
|
800
|
+
const error = processError(err);
|
|
801
|
+
if (!isPrimitive(error)) {
|
|
802
|
+
error.VITEST_TEST_NAME = (_a = worker.current) == null ? void 0 : _a.name;
|
|
803
|
+
if (worker.filepath)
|
|
804
|
+
error.VITEST_TEST_PATH = relative(state().config.root, worker.filepath);
|
|
805
|
+
error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun;
|
|
806
|
+
}
|
|
807
|
+
rpc().onUnhandledError(error, type);
|
|
808
|
+
}
|
|
809
|
+
process.on("uncaughtException", (e) => catchError(e, "Uncaught Exception"));
|
|
810
|
+
process.on("unhandledRejection", (e) => catchError(e, "Unhandled Rejection"));
|
|
811
|
+
const getTransformMode = () => {
|
|
812
|
+
return state().environment.transformMode ?? "ssr";
|
|
813
|
+
};
|
|
814
|
+
return await createVitestExecutor({
|
|
815
|
+
fetchModule(id) {
|
|
816
|
+
return rpc().fetch(id, getTransformMode());
|
|
817
|
+
},
|
|
818
|
+
resolveId(id, importer) {
|
|
819
|
+
return rpc().resolveId(id, importer, getTransformMode());
|
|
820
|
+
},
|
|
821
|
+
packageCache,
|
|
822
|
+
moduleCache,
|
|
823
|
+
mockMap,
|
|
824
|
+
get interopDefault() {
|
|
825
|
+
return state().config.deps.interopDefault;
|
|
826
|
+
},
|
|
827
|
+
get moduleDirectories() {
|
|
828
|
+
return state().config.deps.moduleDirectories;
|
|
829
|
+
},
|
|
830
|
+
get root() {
|
|
831
|
+
return state().config.root;
|
|
832
|
+
},
|
|
833
|
+
get base() {
|
|
834
|
+
return state().config.base;
|
|
835
|
+
},
|
|
836
|
+
...options
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
function updateStyle(id, css) {
|
|
840
|
+
if (typeof document === "undefined")
|
|
841
|
+
return;
|
|
842
|
+
const element = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
843
|
+
if (element) {
|
|
844
|
+
element.textContent = css;
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
const head = document.querySelector("head");
|
|
848
|
+
const style = document.createElement("style");
|
|
849
|
+
style.setAttribute("type", "text/css");
|
|
850
|
+
style.setAttribute("data-vite-dev-id", id);
|
|
851
|
+
style.textContent = css;
|
|
852
|
+
head == null ? void 0 : head.appendChild(style);
|
|
853
|
+
}
|
|
854
|
+
function removeStyle(id) {
|
|
855
|
+
if (typeof document === "undefined")
|
|
856
|
+
return;
|
|
857
|
+
const sheet = document.querySelector(`[data-vite-dev-id="${id}"]`);
|
|
858
|
+
if (sheet)
|
|
859
|
+
document.head.removeChild(sheet);
|
|
860
|
+
}
|
|
861
|
+
class VitestExecutor extends ViteNodeRunner {
|
|
862
|
+
constructor(options) {
|
|
863
|
+
super(options);
|
|
864
|
+
this.options = options;
|
|
865
|
+
this.mocker = new VitestMocker(this);
|
|
866
|
+
if (!options.context) {
|
|
867
|
+
Object.defineProperty(globalThis, "__vitest_mocker__", {
|
|
868
|
+
value: this.mocker,
|
|
869
|
+
writable: true,
|
|
870
|
+
configurable: true
|
|
871
|
+
});
|
|
872
|
+
const clientStub = { ...DEFAULT_REQUEST_STUBS["@vite/client"], updateStyle, removeStyle };
|
|
873
|
+
this.options.requestStubs = {
|
|
874
|
+
"/@vite/client": clientStub,
|
|
875
|
+
"@vite/client": clientStub
|
|
876
|
+
};
|
|
877
|
+
this.primitives = {
|
|
878
|
+
Object,
|
|
879
|
+
Reflect,
|
|
880
|
+
Symbol
|
|
881
|
+
};
|
|
882
|
+
} else {
|
|
883
|
+
this.externalModules = new ExternalModulesExecutor({
|
|
884
|
+
context: options.context,
|
|
885
|
+
packageCache: options.packageCache
|
|
886
|
+
});
|
|
887
|
+
const clientStub = vm.runInContext(
|
|
888
|
+
`(defaultClient) => ({ ...defaultClient, updateStyle: ${updateStyle.toString()}, removeStyle: ${removeStyle.toString()} })`,
|
|
889
|
+
options.context
|
|
890
|
+
)(DEFAULT_REQUEST_STUBS["@vite/client"]);
|
|
891
|
+
this.options.requestStubs = {
|
|
892
|
+
"/@vite/client": clientStub,
|
|
893
|
+
"@vite/client": clientStub
|
|
894
|
+
};
|
|
895
|
+
this.primitives = vm.runInContext("({ Object, Reflect, Symbol })", options.context);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
mocker;
|
|
899
|
+
externalModules;
|
|
900
|
+
primitives;
|
|
901
|
+
getContextPrimitives() {
|
|
902
|
+
return this.primitives;
|
|
903
|
+
}
|
|
904
|
+
get state() {
|
|
905
|
+
return getWorkerState() || this.options.state;
|
|
906
|
+
}
|
|
907
|
+
shouldResolveId(id, _importee) {
|
|
908
|
+
var _a;
|
|
909
|
+
if (isInternalRequest(id) || id.startsWith("data:"))
|
|
910
|
+
return false;
|
|
911
|
+
const transformMode = ((_a = this.state.environment) == null ? void 0 : _a.transformMode) ?? "ssr";
|
|
912
|
+
return transformMode === "ssr" ? !isNodeBuiltin(id) : !id.startsWith("node:");
|
|
913
|
+
}
|
|
914
|
+
async originalResolveUrl(id, importer) {
|
|
915
|
+
return super.resolveUrl(id, importer);
|
|
916
|
+
}
|
|
917
|
+
async resolveUrl(id, importer) {
|
|
918
|
+
if (VitestMocker.pendingIds.length)
|
|
919
|
+
await this.mocker.resolveMocks();
|
|
920
|
+
if (importer && importer.startsWith("mock:"))
|
|
921
|
+
importer = importer.slice(5);
|
|
922
|
+
try {
|
|
923
|
+
return await super.resolveUrl(id, importer);
|
|
924
|
+
} catch (error) {
|
|
925
|
+
if (error.code === "ERR_MODULE_NOT_FOUND") {
|
|
926
|
+
const { id: id2 } = error[Symbol.for("vitest.error.not_found.data")];
|
|
927
|
+
const path = this.mocker.normalizePath(id2);
|
|
928
|
+
const mock = this.mocker.getDependencyMock(path);
|
|
929
|
+
if (mock !== void 0)
|
|
930
|
+
return [id2, id2];
|
|
931
|
+
}
|
|
932
|
+
throw error;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
async runModule(context, transformed) {
|
|
936
|
+
const vmContext = this.options.context;
|
|
937
|
+
if (!vmContext || !this.externalModules)
|
|
938
|
+
return super.runModule(context, transformed);
|
|
939
|
+
const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
|
|
940
|
+
const code = `${codeDefinition}${transformed}
|
|
941
|
+
}}`;
|
|
942
|
+
const options = {
|
|
943
|
+
filename: context.__filename,
|
|
944
|
+
lineOffset: 0,
|
|
945
|
+
columnOffset: -codeDefinition.length
|
|
946
|
+
};
|
|
947
|
+
const fn = vm.runInContext(code, vmContext, {
|
|
948
|
+
...options,
|
|
949
|
+
// if we encountered an import, it's not inlined
|
|
950
|
+
importModuleDynamically: this.externalModules.importModuleDynamically
|
|
951
|
+
});
|
|
952
|
+
await fn(...Object.values(context));
|
|
953
|
+
}
|
|
954
|
+
async importExternalModule(path) {
|
|
955
|
+
if (this.externalModules)
|
|
956
|
+
return this.externalModules.import(path);
|
|
957
|
+
return super.importExternalModule(path);
|
|
958
|
+
}
|
|
959
|
+
async dependencyRequest(id, fsPath, callstack) {
|
|
960
|
+
const mocked = await this.mocker.requestWithMock(fsPath, callstack);
|
|
961
|
+
if (typeof mocked === "string")
|
|
962
|
+
return super.dependencyRequest(mocked, mocked, callstack);
|
|
963
|
+
if (mocked && typeof mocked === "object")
|
|
964
|
+
return mocked;
|
|
965
|
+
return super.dependencyRequest(id, fsPath, callstack);
|
|
966
|
+
}
|
|
967
|
+
prepareContext(context) {
|
|
968
|
+
if (this.state.filepath && normalize(this.state.filepath) === normalize(context.__filename)) {
|
|
969
|
+
const globalNamespace = this.options.context || globalThis;
|
|
970
|
+
Object.defineProperty(context.__vite_ssr_import_meta__, "vitest", { get: () => globalNamespace.__vitest_index__ });
|
|
971
|
+
}
|
|
972
|
+
if (this.options.context && this.externalModules)
|
|
973
|
+
context.require = this.externalModules.createRequire(context.__filename);
|
|
974
|
+
return context;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
export { VitestExecutor as V, mockMap as a, startVitestExecutor as b, moduleCache as m, startViteNode as s };
|