vitest 4.1.12 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +192 -10
- package/browser/context.d.ts +7 -1
- package/browser/context.js +5 -5
- package/config.d.ts +0 -2
- package/dist/browser.d.ts +52 -21
- package/dist/browser.js +12 -13
- package/dist/chunks/{acorn.B2iPLyUM.js → acorn.C1kjbUFw.js} +30 -30
- package/dist/chunks/{base.B6Opl8PE.js → base.Cof-UHJf.js} +81 -52
- package/dist/chunks/{browser.d.BcoexmFG.d.ts → browser.d.C7tWgUvw.d.ts} +17 -14
- package/dist/chunks/{cac.DzDtg8hz.js → cac.BYbt0aJp.js} +143 -87
- package/dist/chunks/{cli-api.B0Pz1_R7.js → cli-api.COfZo8lK.js} +7160 -7189
- package/dist/chunks/config.d.B3YV4t0O.d.ts +3873 -0
- package/dist/chunks/{console.3WNpx0tS.js → console.CQZ877Hi.js} +69 -63
- package/dist/chunks/{constants.CPYnjOGj.js → constants.DLYM6cZY.js} +2 -2
- package/dist/chunks/coverage.9iqXE2gN.js +26 -0
- package/dist/chunks/{coverage.CTzCuANN.js → coverage.jGCktcGP.js} +7 -2
- package/dist/chunks/{creator.DgVhQm5q.js → creator.DHWmytIK.js} +65 -28
- package/dist/chunks/{defaults.9aQKnqFk.js → defaults.B1FCaFfh.js} +16 -9
- package/dist/chunks/display.BlTmubjj.js +194 -0
- package/dist/chunks/doctor.B-oPbwps.js +465 -0
- package/dist/chunks/{env.D4Lgay0q.js → env.BKKtU2WC.js} +2 -1
- package/dist/chunks/{environment.d.CrsxCzP1.d.ts → environment.d.BFQtFKdZ.d.ts} +9 -1
- package/dist/chunks/globals.ZKtKSkEf.js +29 -0
- package/dist/chunks/index.Bi0NXYPf.js +1708 -0
- package/dist/chunks/{index.CMESou6r.js → index.BoyTG_B7.js} +82 -81
- package/dist/chunks/index.CZzC1gGg.js +8501 -0
- package/dist/chunks/index.D0heQG4v.js +1318 -0
- package/dist/chunks/index.Di8au7q4.js +15665 -0
- package/dist/chunks/{index.Chj8NDwU.js → index.DmDMHCg8.js} +6 -12
- package/dist/chunks/{index.DXx9Dtk7.js → index.w_oBZ9N8.js} +25 -12
- package/dist/chunks/{init-forks.H5ZuobOQ.js → init-forks.BaDXQSD8.js} +7 -10
- package/dist/chunks/{init-threads.6kl1khcL.js → init-threads.ZX_Vov0z.js} +1 -1
- package/dist/chunks/{init.k9zZ9sLh.js → init.CO4AB7HZ.js} +113 -38
- package/dist/chunks/module-dependencies._2Eei3FO.js +71 -0
- package/dist/chunks/{native.DPzPHdi5.js → native.upB-V0CK.js} +50 -37
- package/dist/chunks/{nativeModuleMocker.BkNfQMkH.js → nativeModuleMocker.Dh5azSB4.js} +48 -19
- package/dist/chunks/{index.BCY_7LL2.js → nativeModuleRunner.9xPIMzi7.js} +110 -42
- package/dist/chunks/node.B9mkVmLH.js +47 -0
- package/dist/chunks/offset.Dy-5Fdfn.js +24 -0
- package/dist/chunks/pathe.M-eThtNZ.zq3TM4NT.js +492 -0
- package/dist/chunks/{reporters.d.DtoKVV2s.d.ts → plugin.d.LRFSN46m.d.ts} +2573 -1453
- package/dist/chunks/plugins.DPQHVT0p.js +37 -0
- package/dist/chunks/rawSnapshot.DqSiqIEx.js +356 -0
- package/dist/chunks/resolver.BzpmeQeT.js +48 -0
- package/dist/chunks/rpc.Uhkwr2NM.js +93 -0
- package/dist/chunks/rpc.d.3FOhdFx8.d.ts +75 -0
- package/dist/chunks/run.CjdgFZiW.js +4231 -0
- package/dist/chunks/setup-common.CqAQCXzx.js +36 -0
- package/dist/chunks/source-map.DakdQc29.js +1788 -0
- package/dist/chunks/source-map.d.BkG8YK_3.d.ts +89 -0
- package/dist/chunks/spy.DDMXghXv.js +509 -0
- package/dist/chunks/task-utils.d.D-gFPNFi.d.ts +144 -0
- package/dist/chunks/tinyrainbow.Do8I1XRG.js +88 -0
- package/dist/chunks/{utils.BS4fH3nR.js → utils.BBk_icFG.js} +50 -52
- package/dist/chunks/{utils.BX5Fg8C4.js → utils.DYj33du9.js} +1 -17
- package/dist/chunks/vm.b4MbCAJQ.js +1329 -0
- package/dist/chunks/{worker.d.ZpHpO4yb.d.ts → worker.d.CyJRpyfZ.d.ts} +34 -19
- package/dist/cli.js +5 -6
- package/dist/config.cjs +9 -6
- package/dist/config.d.ts +27 -25
- package/dist/config.js +3 -3
- package/dist/index.d.ts +471 -38
- package/dist/index.js +18 -19
- package/dist/module-evaluator.d.ts +64 -5
- package/dist/module-evaluator.js +122 -62
- package/dist/node.d.ts +355 -87
- package/dist/node.js +49 -73
- package/dist/runtime.d.ts +43 -6
- package/dist/runtime.js +7 -9
- package/dist/spy.js +1 -1
- package/dist/task-utils.js +290 -0
- package/dist/{chunks/traces.DT5aQ62U.js → traces.js} +3 -2
- package/dist/worker.d.ts +8 -10
- package/dist/worker.js +33 -43
- package/dist/workers/forks.js +30 -40
- package/dist/workers/runVmTests.js +66 -43
- package/dist/workers/threads.js +30 -40
- package/dist/workers/vmForks.js +19 -20
- package/dist/workers/vmThreads.js +19 -20
- package/globals.d.ts +19 -19
- package/index.cjs +3 -3
- package/optional-runtime-types.d.ts +1 -1
- package/optional-types.d.ts +1 -1
- package/package.json +59 -74
- package/suppress-warnings.cjs +1 -1
- package/dist/chunks/_commonjsHelpers.D26ty3Ew.js +0 -6
- package/dist/chunks/benchmark.CX_oY03V.js +0 -40
- package/dist/chunks/benchmark.d.DAaHLpsq.d.ts +0 -24
- package/dist/chunks/config.d.A1h_Y6Jt.d.ts +0 -232
- package/dist/chunks/coverage.DM_a_rWm.js +0 -1087
- package/dist/chunks/coverage.d.BZtK59WP.d.ts +0 -37
- package/dist/chunks/evaluatedModules.Dg1zASAC.js +0 -17
- package/dist/chunks/global.d.DVsSRdQ5.d.ts +0 -100
- package/dist/chunks/globals.Dj1TGiMC.js +0 -31
- package/dist/chunks/index.DC7d2Pf8.js +0 -729
- package/dist/chunks/index.DdgEv5B1.js +0 -42
- package/dist/chunks/index.UpGiHP7g.js +0 -4255
- package/dist/chunks/index.og1WyBLx.js +0 -6358
- package/dist/chunks/nativeModuleRunner.BIakptoF.js +0 -36
- package/dist/chunks/node.COQbm6gK.js +0 -14
- package/dist/chunks/plugin.d.DwFIiJ7i.d.ts +0 -38
- package/dist/chunks/rpc.MzXet3jl.js +0 -144
- package/dist/chunks/rpc.d.B_8sPU0w.d.ts +0 -30
- package/dist/chunks/setup-common.DYx3LtFI.js +0 -60
- package/dist/chunks/startVitestModuleRunner.DB-7oCpn.js +0 -740
- package/dist/chunks/suite.d.udJtyAgw.d.ts +0 -10
- package/dist/chunks/test.DNmyFkvJ.js +0 -4430
- package/dist/chunks/traces.d.D2T_R8rx.d.ts +0 -60
- package/dist/chunks/vm.CXMd5FHa.js +0 -785
- package/dist/coverage.d.ts +0 -123
- package/dist/coverage.js +0 -27
- package/dist/environments.d.ts +0 -22
- package/dist/environments.js +0 -5
- package/dist/reporters.d.ts +0 -27
- package/dist/reporters.js +0 -26
- package/dist/runners.d.ts +0 -70
- package/dist/runners.js +0 -19
- package/dist/snapshot.d.ts +0 -9
- package/dist/snapshot.js +0 -6
- package/dist/suite.d.ts +0 -5
- package/dist/suite.js +0 -8
|
@@ -2,11 +2,10 @@ import module$1, { isBuiltin } from 'node:module';
|
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
3
|
import { MessageChannel } from 'node:worker_threads';
|
|
4
4
|
import { initSyntaxLexers, hoistMocks } from '@vitest/mocker/transforms';
|
|
5
|
-
import { cleanUrl } from '
|
|
6
|
-
import { p as parse } from './acorn.
|
|
5
|
+
import { c as cleanUrl, r as resolve } from './pathe.M-eThtNZ.zq3TM4NT.js';
|
|
6
|
+
import { p as parse } from './acorn.C1kjbUFw.js';
|
|
7
7
|
import MagicString from 'magic-string';
|
|
8
|
-
import {
|
|
9
|
-
import c from 'tinyrainbow';
|
|
8
|
+
import { $ } from './tinyrainbow.Do8I1XRG.js';
|
|
10
9
|
import { distDir } from '../path.js';
|
|
11
10
|
import { t as toBuiltin } from './modules.BJuCwlRJ.js';
|
|
12
11
|
import 'node:path';
|
|
@@ -14,40 +13,53 @@ import 'node:path';
|
|
|
14
13
|
const NOW_LENGTH = Date.now().toString().length;
|
|
15
14
|
const REGEXP_VITEST = new RegExp(`%3Fvitest=\\d{${NOW_LENGTH}}`);
|
|
16
15
|
const REGEXP_MOCK_ACTUAL = /\?mock=actual/;
|
|
17
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Returns `true` if `onImport` is called for every import, the hooks of `module.register` run in another thread
|
|
18
|
+
* and cannot report imports before the test file finishes.
|
|
19
|
+
*/
|
|
20
|
+
async function setupNodeLoaderHooks(worker, onImport) {
|
|
18
21
|
if (module$1.setSourceMapsSupport) module$1.setSourceMapsSupport(true);
|
|
19
22
|
else if (process.setSourceMapsEnabled) process.setSourceMapsEnabled(true);
|
|
20
23
|
if (worker.config.experimental.nodeLoader !== false) await initSyntaxLexers();
|
|
21
|
-
if (typeof module$1.registerHooks === "function")
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
24
|
+
if (typeof module$1.registerHooks === "function") {
|
|
25
|
+
module$1.registerHooks({
|
|
26
|
+
resolve(specifier, context, nextResolve) {
|
|
27
|
+
if (specifier.includes("mock=actual")) {
|
|
28
|
+
// url is already resolved by `importActual`
|
|
29
|
+
const moduleId = specifier.replace(REGEXP_MOCK_ACTUAL, "");
|
|
30
|
+
const builtin = isBuiltin(moduleId);
|
|
31
|
+
return {
|
|
32
|
+
url: builtin ? toBuiltin(moduleId) : moduleId,
|
|
33
|
+
format: builtin ? "builtin" : void 0,
|
|
34
|
+
shortCircuit: true
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const isVitest = specifier.includes("%3Fvitest=");
|
|
38
|
+
const result = nextResolve(isVitest ? specifier.replace(REGEXP_VITEST, "") : specifier, context);
|
|
39
|
+
// avoid tracking /node_modules/ module graph for performance reasons
|
|
40
|
+
if (context.parentURL && result.url && !result.url.includes("/node_modules/")) {
|
|
41
|
+
worker.rpc.ensureModuleGraphEntry(result.url, context.parentURL).catch(() => {
|
|
42
|
+
// ignore errors
|
|
43
|
+
});
|
|
44
|
+
onImport?.(result.url, context.parentURL);
|
|
45
|
+
}
|
|
46
|
+
// this is require for in-source tests to be invalidated if
|
|
47
|
+
// one of the files already imported it in --maxWorkers=1 --no-isolate
|
|
48
|
+
if (isVitest) result.url = `${result.url}?vitest=${Date.now()}`;
|
|
49
|
+
if (worker.config.experimental.nodeLoader === false || !context.parentURL || result.url.includes(distDir) || context.parentURL?.toString().includes(distDir)) return result;
|
|
50
|
+
const mockedResult = getNativeMocker()?.resolveMockedModule(result.url, context.parentURL);
|
|
51
|
+
if (mockedResult != null) {
|
|
52
|
+
// a `__mocks__` file is loaded in place of the module
|
|
53
|
+
onImport?.(mockedResult.url, context.parentURL);
|
|
54
|
+
return mockedResult;
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
},
|
|
58
|
+
load: worker.config.experimental.nodeLoader === false ? void 0 : createLoadHook()
|
|
59
|
+
});
|
|
60
|
+
return true;
|
|
61
|
+
} else if (module$1.register) {
|
|
62
|
+
if (worker.config.experimental.nodeLoader !== false) console.warn(`${$.bgYellow(" WARNING ")} "module.registerHooks" is not supported in Node.js ${process.version}. This means that some features like module mocking or in-source testing are not supported. Upgrade your Node.js version to at least 22.15 or disable "experimental.nodeLoader" flag manually.\n`);
|
|
51
63
|
const { port1, port2 } = new MessageChannel();
|
|
52
64
|
port1.unref();
|
|
53
65
|
port2.unref();
|
|
@@ -69,12 +81,13 @@ async function setupNodeLoaderHooks(worker) {
|
|
|
69
81
|
transferList: [port2]
|
|
70
82
|
});
|
|
71
83
|
} else if (!process.versions.deno && !process.versions.bun) console.warn("\"module.registerHooks\" and \"module.register\" are not supported. Some Vitest features may not work. Please, use Node.js 18.19.0 or higher.");
|
|
84
|
+
return false;
|
|
72
85
|
}
|
|
73
86
|
function replaceInSourceMarker(url, source, ms) {
|
|
74
87
|
const re = /import\.meta\.vitest/g;
|
|
75
88
|
let match;
|
|
76
89
|
let overridden = false;
|
|
77
|
-
//
|
|
90
|
+
// oxlint-disable-next-line no-cond-assign
|
|
78
91
|
while (match = re.exec(source)) {
|
|
79
92
|
const { index, "0": code } = match;
|
|
80
93
|
overridden = true;
|
|
@@ -87,7 +100,7 @@ function replaceInSourceMarker(url, source, ms) {
|
|
|
87
100
|
ms().append(`;\nfunction IMPORT_META_TEST() { return typeof __vitest_worker__ !== 'undefined' && __vitest_worker__.filepath === "${filename.replace(/"/g, "\\\"")}" ? __vitest_index__ : undefined; }`);
|
|
88
101
|
}
|
|
89
102
|
}
|
|
90
|
-
const ignoreFormats = new Set([
|
|
103
|
+
const ignoreFormats = /* @__PURE__ */ new Set([
|
|
91
104
|
"addon",
|
|
92
105
|
"builtin",
|
|
93
106
|
"wasm"
|
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
import module$1, { isBuiltin } from 'node:module';
|
|
2
2
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
3
3
|
import { automockModule, createManualModuleSource, collectModuleExports } from '@vitest/mocker/transforms';
|
|
4
|
-
import { cleanUrl, createDefer } from '
|
|
5
|
-
import { p as parse } from './acorn.
|
|
6
|
-
import { isAbsolute } from 'pathe';
|
|
4
|
+
import { c as cleanUrl, e as createDefer, i as isAbsolute } from './pathe.M-eThtNZ.zq3TM4NT.js';
|
|
5
|
+
import { p as parse } from './acorn.C1kjbUFw.js';
|
|
7
6
|
import { t as toBuiltin } from './modules.BJuCwlRJ.js';
|
|
8
|
-
import { B as BareModuleMocker, n as normalizeModuleId } from './
|
|
7
|
+
import { B as BareModuleMocker, n as normalizeModuleId } from './index.Bi0NXYPf.js';
|
|
9
8
|
import 'node:fs';
|
|
10
|
-
import './utils.
|
|
11
|
-
import '@vitest/utils/timers';
|
|
9
|
+
import './utils.DYj33du9.js';
|
|
12
10
|
import '../path.js';
|
|
13
11
|
import 'node:path';
|
|
14
12
|
import '../module-evaluator.js';
|
|
15
13
|
import 'node:vm';
|
|
16
14
|
import 'vite/module-runner';
|
|
17
|
-
import '
|
|
15
|
+
import '../traces.js';
|
|
16
|
+
import 'node:async_hooks';
|
|
18
17
|
import '@vitest/mocker';
|
|
19
18
|
import '@vitest/mocker/redirect';
|
|
19
|
+
import 'node:console';
|
|
20
20
|
|
|
21
21
|
class NativeModuleMocker extends BareModuleMocker {
|
|
22
22
|
wrapDynamicImport(moduleFactory) {
|
|
23
|
-
if (typeof moduleFactory
|
|
23
|
+
if (typeof moduleFactory !== "function") return moduleFactory;
|
|
24
|
+
// Node.js can evaluate the module outside of the importer's async chain,
|
|
25
|
+
// so the stack of an import error would not point at the import
|
|
26
|
+
const importer = /* @__PURE__ */ new Error("import location");
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
24
28
|
this.resolveMocks().finally(() => {
|
|
25
|
-
moduleFactory().then(resolve, reject);
|
|
29
|
+
moduleFactory().then(resolve, (error) => reject(addImporterFrame(error, importer)));
|
|
26
30
|
});
|
|
27
31
|
});
|
|
28
|
-
return moduleFactory;
|
|
29
32
|
}
|
|
30
33
|
resolveMockedModule(url, parentURL) {
|
|
31
34
|
// don't mock modules inside of packages because there is
|
|
@@ -33,7 +36,8 @@ class NativeModuleMocker extends BareModuleMocker {
|
|
|
33
36
|
// because we use top-level await in "manual" mocks.
|
|
34
37
|
// for the sake of consistency we don't support mocking anything at all
|
|
35
38
|
if (parentURL.includes("/node_modules/")) return;
|
|
36
|
-
const
|
|
39
|
+
const filename = url.startsWith("file://") ? fileURLToPath(url) : url;
|
|
40
|
+
const moduleId = normalizeModuleId(filename);
|
|
37
41
|
const mockedModule = this.getDependencyMock(moduleId);
|
|
38
42
|
if (!mockedModule) return;
|
|
39
43
|
if (mockedModule.type === "redirect") return {
|
|
@@ -50,15 +54,16 @@ class NativeModuleMocker extends BareModuleMocker {
|
|
|
50
54
|
};
|
|
51
55
|
}
|
|
52
56
|
loadAutomock(url, result) {
|
|
53
|
-
const
|
|
57
|
+
const filename = url.startsWith("file://") ? fileURLToPath(url) : url;
|
|
58
|
+
const moduleId = cleanUrl(normalizeModuleId(filename));
|
|
54
59
|
let source;
|
|
55
60
|
if (isBuiltin(moduleId)) {
|
|
56
61
|
const builtinModule = getBuiltinModule(moduleId);
|
|
57
|
-
const exports
|
|
62
|
+
const exports = Object.keys(builtinModule);
|
|
58
63
|
source = `
|
|
59
64
|
import * as builtinModule from '${toBuiltin(moduleId)}?mock=actual'
|
|
60
65
|
|
|
61
|
-
${exports
|
|
66
|
+
${exports.map((key, index) => {
|
|
62
67
|
return `
|
|
63
68
|
const __${index} = builtinModule["${key}"]
|
|
64
69
|
export { __${index} as "${key}" }
|
|
@@ -86,7 +91,8 @@ export { __${index} as "${key}" }
|
|
|
86
91
|
}
|
|
87
92
|
}
|
|
88
93
|
loadManualMock(url, result) {
|
|
89
|
-
const
|
|
94
|
+
const filename = url.startsWith("file://") ? fileURLToPath(url) : url;
|
|
95
|
+
const moduleId = cleanUrl(normalizeModuleId(filename));
|
|
90
96
|
// should not be possible
|
|
91
97
|
if (this.getDependencyMock(moduleId)?.type !== "manual") {
|
|
92
98
|
console.warn(`Vitest detected unregistered manual mock ${moduleId}. This is a bug in Vitest. Please, open a new issue with reproduction.`);
|
|
@@ -94,9 +100,10 @@ export { __${index} as "${key}" }
|
|
|
94
100
|
}
|
|
95
101
|
if (isBuiltin(moduleId)) {
|
|
96
102
|
const builtinModule = getBuiltinModule(toBuiltin(moduleId));
|
|
103
|
+
const exports = Object.keys(builtinModule);
|
|
97
104
|
return {
|
|
98
105
|
format: "module",
|
|
99
|
-
source: createManualModuleSource(moduleId,
|
|
106
|
+
source: createManualModuleSource(moduleId, exports),
|
|
100
107
|
shortCircuit: true
|
|
101
108
|
};
|
|
102
109
|
}
|
|
@@ -105,9 +112,12 @@ export { __${index} as "${key}" }
|
|
|
105
112
|
if (transformedCode == null) return;
|
|
106
113
|
const format = result.format?.startsWith("module") ? "module" : "commonjs";
|
|
107
114
|
try {
|
|
115
|
+
// we parse the module with es/cjs-module-lexer to find the original exports -- we assume the same ones are returned from the factory
|
|
116
|
+
// injecting new keys is not supported (and should not be advised anyway)
|
|
117
|
+
const exports = collectModuleExports(moduleId, transformedCode, format);
|
|
108
118
|
return {
|
|
109
119
|
format: "module",
|
|
110
|
-
source: createManualModuleSource(moduleId,
|
|
120
|
+
source: createManualModuleSource(moduleId, exports),
|
|
111
121
|
shortCircuit: true
|
|
112
122
|
};
|
|
113
123
|
} catch (cause) {
|
|
@@ -116,7 +126,8 @@ export { __${index} as "${key}" }
|
|
|
116
126
|
}
|
|
117
127
|
processedModules = /* @__PURE__ */ new Map();
|
|
118
128
|
checkCircularManualMock(url) {
|
|
119
|
-
const
|
|
129
|
+
const filename = url.startsWith("file://") ? fileURLToPath(url) : url;
|
|
130
|
+
const id = cleanUrl(normalizeModuleId(filename));
|
|
120
131
|
this.processedModules.set(id, (this.processedModules.get(id) ?? 0) + 1);
|
|
121
132
|
// the module is mocked and requested a second time, let's resolve
|
|
122
133
|
// the factory function that will redefine the exports later
|
|
@@ -174,7 +185,7 @@ export { __${index} as "${key}" }
|
|
|
174
185
|
const filename = fileURLToPath(resolvedId);
|
|
175
186
|
const external = !isAbsolute(filename) || this.isModuleDirectory(resolvedId) ? normalizeModuleId(rawId) : null;
|
|
176
187
|
// file is not mocked, automock or redirect it
|
|
177
|
-
const redirect = this.findMockRedirect(filename, external);
|
|
188
|
+
const redirect = this.findMockRedirect(filename, external, rawId);
|
|
178
189
|
if (redirect) return import(pathToFileURL(redirect).toString());
|
|
179
190
|
const url = new URL(resolvedId);
|
|
180
191
|
url.searchParams.set("mock", "automock");
|
|
@@ -202,5 +213,23 @@ function transformCode(code, format, filename) {
|
|
|
202
213
|
}
|
|
203
214
|
return code;
|
|
204
215
|
}
|
|
216
|
+
function addImporterFrame(error, importer) {
|
|
217
|
+
const lines = importer.stack?.split("\n") || [];
|
|
218
|
+
const index = lines.findIndex((line) => line.includes("wrapDynamicImport"));
|
|
219
|
+
const frame = index === -1 ? void 0 : lines[index + 1];
|
|
220
|
+
const file = frame?.match(/\(?([^()\s]+):\d+:\d+\)?$/)?.[1];
|
|
221
|
+
if (!frame || !file || frame.includes("node:")) return error;
|
|
222
|
+
const asyncFrame = frame.replace(/^(\s*)at /, "$1at async ");
|
|
223
|
+
const seen = /* @__PURE__ */ new Set();
|
|
224
|
+
let current = error;
|
|
225
|
+
while (current instanceof Error && !seen.has(current)) {
|
|
226
|
+
seen.add(current);
|
|
227
|
+
const stack = current.stack;
|
|
228
|
+
// older Node.js versions keep the async frame of the import themselves
|
|
229
|
+
if (typeof stack === "string" && !stack.split("\n").some((line) => line.includes("at async ") && line.includes(file))) current.stack = `${stack}\n${asyncFrame}`;
|
|
230
|
+
current = current.cause;
|
|
231
|
+
}
|
|
232
|
+
return error;
|
|
233
|
+
}
|
|
205
234
|
|
|
206
235
|
export { NativeModuleMocker };
|