@voidzero-dev/vite-plus-test 0.1.9 → 0.1.10
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 +181 -1
- package/dist/@vitest/browser/client/.vite/manifest.json +6 -6
- package/dist/@vitest/browser/client/__vitest__/assets/index-Da0hb3oU.css +1 -0
- package/dist/@vitest/browser/client/__vitest__/assets/index-Di71CKDo.js +63 -0
- package/dist/@vitest/browser/client/__vitest__/favicon.ico +0 -0
- package/dist/@vitest/browser/client/__vitest__/favicon.svg +49 -4
- package/dist/@vitest/browser/client/__vitest__/index.html +2 -2
- package/dist/@vitest/browser/client/__vitest_browser__/{orchestrator-S_3e_uzt.js → orchestrator-CXs6qrFe.js} +70 -28
- package/dist/@vitest/browser/client/__vitest_browser__/{tester-k74mgIRa.js → tester-K5NNxh1O.js} +167 -58
- package/dist/@vitest/browser/client/__vitest_browser__/{utils-uxqdqUz8.js → utils-C2ISqq1C.js} +2 -2
- package/dist/@vitest/browser/client/favicon.svg +49 -4
- package/dist/@vitest/browser/client/orchestrator.html +2 -2
- package/dist/@vitest/browser/client/tester/tester.html +2 -2
- package/dist/@vitest/browser/client.js +20 -13
- package/dist/@vitest/browser/context.d.ts +160 -10
- package/dist/@vitest/browser/context.js +108 -22
- package/dist/@vitest/browser/expect-element.js +23 -28
- package/dist/@vitest/browser/index-5Pe7X7sp.js +7 -0
- package/dist/@vitest/browser/index.d.ts +20 -2
- package/dist/@vitest/browser/index.js +5706 -159
- package/dist/@vitest/browser/locators.d.ts +14 -3
- package/dist/@vitest/browser/locators.js +1 -1
- package/dist/@vitest/browser-playwright/index.d.ts +22 -5
- package/dist/@vitest/browser-playwright/index.js +169 -61
- package/dist/@vitest/browser-preview/index.d.ts +14 -1
- package/dist/@vitest/browser-preview/locators.js +31 -18
- package/dist/@vitest/browser-webdriverio/index.d.ts +17 -3
- package/dist/@vitest/browser-webdriverio/index.js +22 -2
- package/dist/@vitest/browser-webdriverio/locators.js +84 -7
- package/dist/@vitest/expect/index.d.ts +172 -54
- package/dist/@vitest/expect/index.js +124 -67
- package/dist/@vitest/mocker/auto-register.js +1 -0
- package/dist/@vitest/mocker/automock.d.ts +1 -0
- package/dist/@vitest/mocker/automock.js +5 -0
- package/dist/@vitest/mocker/browser.d.ts +4 -4
- package/dist/@vitest/mocker/browser.js +1 -0
- package/dist/@vitest/mocker/chunk-automock.js +182 -14
- package/dist/@vitest/mocker/chunk-helpers.js +44 -0
- package/dist/@vitest/mocker/chunk-hoistMocks.js +659 -0
- package/dist/@vitest/mocker/chunk-mocker.js +41 -30
- package/dist/@vitest/mocker/chunk-registry.js +21 -7
- package/dist/@vitest/mocker/chunk-utils.js +18 -7
- package/dist/@vitest/mocker/hoistMocks.d-w2ILr1dG.d.ts +739 -0
- package/dist/@vitest/mocker/{index.d-C-sLYZi-.d.ts → index.d-B41z0AuW.d.ts} +1 -1
- package/dist/@vitest/mocker/index.d.ts +2 -2
- package/dist/@vitest/mocker/index.js +18 -3
- package/dist/@vitest/mocker/{mocker.d-TnKRhz7N.d.ts → mocker.d-QEntlm6J.d.ts} +10 -5
- package/dist/@vitest/mocker/node.d.ts +5 -734
- package/dist/@vitest/mocker/node.js +29 -587
- package/dist/@vitest/mocker/redirect.js +4 -4
- package/dist/@vitest/mocker/register.d.ts +3 -3
- package/dist/@vitest/mocker/register.js +1 -0
- package/dist/@vitest/mocker/transforms.d.ts +6 -0
- package/dist/@vitest/mocker/transforms.js +8 -0
- package/dist/@vitest/mocker/{types.d-B8CCKmHt.d.ts → types.d-BjI5eAwu.d.ts} +23 -7
- package/dist/@vitest/pretty-format/index.d.ts +11 -1
- package/dist/@vitest/pretty-format/index.js +33 -4
- package/dist/@vitest/runner/chunk-tasks.js +305 -37
- package/dist/@vitest/runner/index.d.ts +5 -6
- package/dist/@vitest/runner/index.js +1146 -455
- package/dist/@vitest/runner/{tasks.d-C7UxawJ9.d.ts → tasks.d-D2GKpdwQ.d.ts} +726 -55
- package/dist/@vitest/runner/types.d.ts +2 -182
- package/dist/@vitest/runner/utils.d.ts +16 -8
- package/dist/@vitest/runner/utils.js +1 -1
- package/dist/@vitest/snapshot/{environment.d-DHdQ1Csl.d.ts → environment.d-DOJxxZV9.d.ts} +2 -7
- package/dist/@vitest/snapshot/environment.d.ts +2 -1
- package/dist/@vitest/snapshot/environment.js +1 -1
- package/dist/@vitest/snapshot/index.d.ts +4 -3
- package/dist/@vitest/snapshot/index.js +21 -550
- package/dist/@vitest/snapshot/manager.d.ts +3 -2
- package/dist/@vitest/snapshot/manager.js +1 -1
- package/dist/@vitest/snapshot/{rawSnapshot.d-lFsMJFUd.d.ts → rawSnapshot.d-U2kJUxDr.d.ts} +1 -1
- package/dist/@vitest/spy/index.d.ts +34 -4
- package/dist/@vitest/spy/index.js +69 -19
- package/dist/@vitest/utils/diff.js +11 -9
- package/dist/@vitest/utils/display.d.ts +2 -1
- package/dist/@vitest/utils/display.js +38 -5
- package/dist/@vitest/utils/error.d.ts +2 -1
- package/dist/@vitest/utils/error.js +1 -2
- package/dist/@vitest/utils/helpers.d.ts +4 -1
- package/dist/@vitest/utils/helpers.js +43 -1
- package/dist/@vitest/utils/resolver.js +1 -2
- package/dist/@vitest/utils/serialize.js +6 -6
- package/dist/@vitest/utils/source-map/node.d.ts +6 -0
- package/dist/@vitest/utils/source-map/node.js +23 -0
- package/dist/@vitest/utils/source-map.js +15 -5
- package/dist/browser.d.ts +3 -2
- package/dist/browser.js +2 -2
- package/dist/chunks/acorn.B2iPLyUM.js +5958 -0
- package/dist/chunks/{base.CJ0Y4ePK.js → base.DM0-RqVb.js} +54 -16
- package/dist/chunks/{benchmark.B3N2zMcH.js → benchmark.D0SlKNbZ.js} +1 -1
- package/dist/chunks/{browser.d.ChKACdzH.d.ts → browser.d.X3SXoOCV.d.ts} +4 -1
- package/dist/chunks/{cac.DVeoLl0M.js → cac.CWGDZnXT.js} +979 -20
- package/dist/chunks/{cli-api.B7PN_QUv.js → cli-api.DuT9iuvY.js} +8764 -7898
- package/dist/chunks/{config.d.Cy95HiCx.d.ts → config.d.EJLVE3es.d.ts} +30 -15
- package/dist/chunks/{console.Cf-YriPC.js → console.3WNpx0tS.js} +3 -2
- package/dist/chunks/{constants.D_Q9UYh-.js → constants.CPYnjOGj.js} +4 -2
- package/dist/chunks/coverage.Bri33R1t.js +1050 -0
- package/dist/chunks/{creator.DAmOKTvJ.js → creator.DgVhQm5q.js} +35 -4
- package/dist/chunks/{defaults.BOqNVLsY.js → defaults.CdU2lD-q.js} +4 -3
- package/dist/chunks/{global.d.B15mdLcR.d.ts → global.d.x-ILCfAE.d.ts} +1 -2
- package/dist/chunks/{globals.DOayXfHP.js → globals.BXNGLnTL.js} +11 -10
- package/dist/chunks/{coverage.AVPTjMgw.js → index.BCY_7LL2.js} +5 -959
- package/dist/chunks/{index.C5r1PdPD.js → index.CEzQDJGb.js} +1 -1
- package/dist/chunks/{index.D3XRDfWc.js → index.CMESou6r.js} +26 -1
- package/dist/chunks/{index.6Qv1eEA6.js → index.DGNSnENe.js} +95 -9
- package/dist/chunks/{index.M8mOzt4Y.js → index.DXMFO5MJ.js} +3279 -2914
- package/dist/chunks/{index.Z5E_ObnR.js → index.DlDSLQD3.js} +7 -3
- package/dist/chunks/{index.CyBMJtT7.js → index.EY6TCHpo.js} +10 -8
- package/dist/chunks/{index.D4KonVSU.js → index.og1WyBLx.js} +18 -3
- package/dist/chunks/{init-forks._y3TW739.js → init-forks.DeArv0jT.js} +1 -1
- package/dist/chunks/{init-threads.DBO2kn-p.js → init-threads.-2OUl4Nn.js} +1 -1
- package/dist/chunks/{init.B6MLFIaN.js → init.DICorXCo.js} +52 -13
- package/dist/chunks/native.DPzPHdi5.js +148 -0
- package/dist/chunks/nativeModuleMocker.DndvSdL6.js +206 -0
- package/dist/chunks/nativeModuleRunner.BIakptoF.js +36 -0
- package/dist/chunks/{node.Ce0vMQM7.js → node.COQbm6gK.js} +1 -1
- package/dist/chunks/{plugin.d.CtqpEehP.d.ts → plugin.d.BuW-flqo.d.ts} +1 -1
- package/dist/chunks/{reporters.d.CWXNI2jG.d.ts → reporters.d.DVUYHHhe.d.ts} +328 -79
- package/dist/chunks/rpc.MzXet3jl.js +144 -0
- package/dist/chunks/{rpc.d.RH3apGEf.d.ts → rpc.d.BFMWpdph.d.ts} +10 -2
- package/dist/chunks/{setup-common.Cm-kSBVi.js → setup-common.B41N_kPE.js} +3 -3
- package/dist/chunks/{startModuleRunner.DEj0jb3e.js → startVitestModuleRunner.C3ZR-4J3.js} +265 -405
- package/dist/chunks/{suite.d.BJWk38HB.d.ts → suite.d.udJtyAgw.d.ts} +1 -1
- package/dist/chunks/{vi.2VT5v0um.js → test.CTcmp4Su.js} +538 -181
- package/dist/chunks/{utils.DvEY5TfP.js → utils.BX5Fg8C4.js} +8 -1
- package/dist/chunks/{vm.D3epNOPZ.js → vm.Dh2rTtmP.js} +48 -8
- package/dist/chunks/{worker.d.Dyxm8DEL.d.ts → worker.d.B84sVRy0.d.ts} +2 -2
- package/dist/cli.js +6 -5
- package/dist/client/.vite/manifest.json +6 -6
- package/dist/client/__vitest__/assets/index-Da0hb3oU.css +1 -0
- package/dist/client/__vitest__/assets/index-Di71CKDo.js +63 -0
- package/dist/client/__vitest__/favicon.ico +0 -0
- package/dist/client/__vitest__/favicon.svg +49 -4
- package/dist/client/__vitest__/index.html +2 -2
- package/dist/client/__vitest_browser__/{orchestrator-S_3e_uzt.js → orchestrator-CXs6qrFe.js} +70 -28
- package/dist/client/__vitest_browser__/{tester-k74mgIRa.js → tester-K5NNxh1O.js} +167 -58
- package/dist/client/__vitest_browser__/{utils-uxqdqUz8.js → utils-C2ISqq1C.js} +2 -2
- package/dist/client/favicon.svg +49 -4
- package/dist/client/orchestrator.html +2 -2
- package/dist/client/tester/tester.html +2 -2
- package/dist/client.js +20 -13
- package/dist/config.cjs +3 -2
- package/dist/config.d.ts +13 -12
- package/dist/config.js +2 -2
- package/dist/context.js +108 -22
- package/dist/coverage.d.ts +12 -8
- package/dist/coverage.js +8 -5
- package/dist/environments.js +3 -1
- package/dist/expect-element.js +23 -23
- package/dist/index-5Pe7X7sp.js +7 -0
- package/dist/index.d.ts +66 -27
- package/dist/index.js +10 -9
- package/dist/locators.d.ts +14 -3
- package/dist/locators.js +1 -1
- package/dist/module-evaluator.d.ts +11 -1
- package/dist/module-evaluator.js +43 -26
- package/dist/node.d.ts +28 -14
- package/dist/node.js +42 -40
- package/dist/nodejs-worker-loader.js +41 -0
- package/dist/plugins/mocker-transforms.mjs +2 -0
- package/dist/plugins/utils-source-map-node.mjs +2 -0
- package/dist/reporters.d.ts +8 -8
- package/dist/reporters.js +7 -5
- package/dist/runners.d.ts +24 -5
- package/dist/runners.js +6 -6
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.js +35 -0
- package/dist/snapshot.js +4 -2
- package/dist/suite.d.ts +1 -1
- package/dist/suite.js +4 -2
- package/dist/vendor/blazediff_core.d.mts +1 -0
- package/dist/vendor/blazediff_core.mjs +117 -0
- package/dist/vendor/chai.mjs +4 -249
- package/dist/vendor/convert-source-map.d.mts +1 -0
- package/dist/vendor/convert-source-map.mjs +150 -0
- package/dist/vendor/expect-type.d.mts +14 -7
- package/dist/vendor/expect-type.mjs +5 -5
- package/dist/vendor/std-env.d.mts +131 -40
- package/dist/vendor/std-env.mjs +114 -117
- package/dist/worker.d.ts +6 -6
- package/dist/worker.js +27 -21
- package/dist/workers/forks.js +23 -17
- package/dist/workers/runVmTests.js +18 -16
- package/dist/workers/threads.js +23 -17
- package/dist/workers/vmForks.js +15 -12
- package/dist/workers/vmThreads.js +15 -12
- package/globals.d.ts +2 -0
- package/package.json +35 -26
- package/suppress-warnings.cjs +1 -0
- package/dist/@vitest/browser/client/__vitest__/assets/index-BUCFJtth.js +0 -57
- package/dist/@vitest/browser/client/__vitest__/assets/index-DlhE0rqZ.css +0 -1
- package/dist/@vitest/browser/index-D6m36C6U.js +0 -11
- package/dist/@vitest/utils/chunk-_commonjsHelpers.js +0 -5
- package/dist/@vitest/utils/highlight.d.ts +0 -9
- package/dist/@vitest/utils/highlight.js +0 -538
- package/dist/chunks/date.Bq6ZW5rf.js +0 -73
- package/dist/chunks/rpc.BoxB0q7B.js +0 -76
- package/dist/chunks/test.B8ej_ZHS.js +0 -254
- package/dist/client/__vitest__/assets/index-BUCFJtth.js +0 -57
- package/dist/client/__vitest__/assets/index-DlhE0rqZ.css +0 -1
- package/dist/index-D6m36C6U.js +0 -6
- package/dist/mocker.d.ts +0 -1
- package/dist/mocker.js +0 -1
- package/dist/module-runner.js +0 -17
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
import MagicString from '../../vendor/magic-string.mjs';
|
|
2
|
+
import { e as esmWalker } from './chunk-automock.js';
|
|
3
|
+
|
|
4
|
+
// AST walker module for ESTree compatible trees
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function makeTest(test) {
|
|
8
|
+
if (typeof test === "string")
|
|
9
|
+
{ return function (type) { return type === test; } }
|
|
10
|
+
else if (!test)
|
|
11
|
+
{ return function () { return true; } }
|
|
12
|
+
else
|
|
13
|
+
{ return test }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
var Found = function Found(node, state) { this.node = node; this.state = state; };
|
|
17
|
+
|
|
18
|
+
// Find the innermost node of a given type that contains the given
|
|
19
|
+
// position. Interface similar to findNodeAt.
|
|
20
|
+
function findNodeAround(node, pos, test, baseVisitor, state) {
|
|
21
|
+
test = makeTest(test);
|
|
22
|
+
if (!baseVisitor) { baseVisitor = base; }
|
|
23
|
+
try {
|
|
24
|
+
(function c(node, st, override) {
|
|
25
|
+
var type = override || node.type;
|
|
26
|
+
if (node.start > pos || node.end < pos) { return }
|
|
27
|
+
visitNode(baseVisitor, type, node, st, c);
|
|
28
|
+
if (test(type, node)) { throw new Found(node, st) }
|
|
29
|
+
})(node, state);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (e instanceof Found) { return e }
|
|
32
|
+
throw e
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function skipThrough(node, st, c) { c(node, st); }
|
|
37
|
+
function ignore(_node, _st, _c) {}
|
|
38
|
+
|
|
39
|
+
function visitNode(baseVisitor, type, node, st, c) {
|
|
40
|
+
if (baseVisitor[type] == null) { throw new Error(("No walker function defined for node type " + type)) }
|
|
41
|
+
baseVisitor[type](node, st, c);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Node walkers.
|
|
45
|
+
|
|
46
|
+
var base = {};
|
|
47
|
+
|
|
48
|
+
base.Program = base.BlockStatement = base.StaticBlock = function (node, st, c) {
|
|
49
|
+
for (var i = 0, list = node.body; i < list.length; i += 1)
|
|
50
|
+
{
|
|
51
|
+
var stmt = list[i];
|
|
52
|
+
|
|
53
|
+
c(stmt, st, "Statement");
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
base.Statement = skipThrough;
|
|
57
|
+
base.EmptyStatement = ignore;
|
|
58
|
+
base.ExpressionStatement = base.ParenthesizedExpression = base.ChainExpression =
|
|
59
|
+
function (node, st, c) { return c(node.expression, st, "Expression"); };
|
|
60
|
+
base.IfStatement = function (node, st, c) {
|
|
61
|
+
c(node.test, st, "Expression");
|
|
62
|
+
c(node.consequent, st, "Statement");
|
|
63
|
+
if (node.alternate) { c(node.alternate, st, "Statement"); }
|
|
64
|
+
};
|
|
65
|
+
base.LabeledStatement = function (node, st, c) { return c(node.body, st, "Statement"); };
|
|
66
|
+
base.BreakStatement = base.ContinueStatement = ignore;
|
|
67
|
+
base.WithStatement = function (node, st, c) {
|
|
68
|
+
c(node.object, st, "Expression");
|
|
69
|
+
c(node.body, st, "Statement");
|
|
70
|
+
};
|
|
71
|
+
base.SwitchStatement = function (node, st, c) {
|
|
72
|
+
c(node.discriminant, st, "Expression");
|
|
73
|
+
for (var i = 0, list = node.cases; i < list.length; i += 1) {
|
|
74
|
+
var cs = list[i];
|
|
75
|
+
|
|
76
|
+
c(cs, st);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
base.SwitchCase = function (node, st, c) {
|
|
80
|
+
if (node.test) { c(node.test, st, "Expression"); }
|
|
81
|
+
for (var i = 0, list = node.consequent; i < list.length; i += 1)
|
|
82
|
+
{
|
|
83
|
+
var cons = list[i];
|
|
84
|
+
|
|
85
|
+
c(cons, st, "Statement");
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {
|
|
89
|
+
if (node.argument) { c(node.argument, st, "Expression"); }
|
|
90
|
+
};
|
|
91
|
+
base.ThrowStatement = base.SpreadElement =
|
|
92
|
+
function (node, st, c) { return c(node.argument, st, "Expression"); };
|
|
93
|
+
base.TryStatement = function (node, st, c) {
|
|
94
|
+
c(node.block, st, "Statement");
|
|
95
|
+
if (node.handler) { c(node.handler, st); }
|
|
96
|
+
if (node.finalizer) { c(node.finalizer, st, "Statement"); }
|
|
97
|
+
};
|
|
98
|
+
base.CatchClause = function (node, st, c) {
|
|
99
|
+
if (node.param) { c(node.param, st, "Pattern"); }
|
|
100
|
+
c(node.body, st, "Statement");
|
|
101
|
+
};
|
|
102
|
+
base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
|
|
103
|
+
c(node.test, st, "Expression");
|
|
104
|
+
c(node.body, st, "Statement");
|
|
105
|
+
};
|
|
106
|
+
base.ForStatement = function (node, st, c) {
|
|
107
|
+
if (node.init) { c(node.init, st, "ForInit"); }
|
|
108
|
+
if (node.test) { c(node.test, st, "Expression"); }
|
|
109
|
+
if (node.update) { c(node.update, st, "Expression"); }
|
|
110
|
+
c(node.body, st, "Statement");
|
|
111
|
+
};
|
|
112
|
+
base.ForInStatement = base.ForOfStatement = function (node, st, c) {
|
|
113
|
+
c(node.left, st, "ForInit");
|
|
114
|
+
c(node.right, st, "Expression");
|
|
115
|
+
c(node.body, st, "Statement");
|
|
116
|
+
};
|
|
117
|
+
base.ForInit = function (node, st, c) {
|
|
118
|
+
if (node.type === "VariableDeclaration") { c(node, st); }
|
|
119
|
+
else { c(node, st, "Expression"); }
|
|
120
|
+
};
|
|
121
|
+
base.DebuggerStatement = ignore;
|
|
122
|
+
|
|
123
|
+
base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };
|
|
124
|
+
base.VariableDeclaration = function (node, st, c) {
|
|
125
|
+
for (var i = 0, list = node.declarations; i < list.length; i += 1)
|
|
126
|
+
{
|
|
127
|
+
var decl = list[i];
|
|
128
|
+
|
|
129
|
+
c(decl, st);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
base.VariableDeclarator = function (node, st, c) {
|
|
133
|
+
c(node.id, st, "Pattern");
|
|
134
|
+
if (node.init) { c(node.init, st, "Expression"); }
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
base.Function = function (node, st, c) {
|
|
138
|
+
if (node.id) { c(node.id, st, "Pattern"); }
|
|
139
|
+
for (var i = 0, list = node.params; i < list.length; i += 1)
|
|
140
|
+
{
|
|
141
|
+
var param = list[i];
|
|
142
|
+
|
|
143
|
+
c(param, st, "Pattern");
|
|
144
|
+
}
|
|
145
|
+
c(node.body, st, node.expression ? "Expression" : "Statement");
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
base.Pattern = function (node, st, c) {
|
|
149
|
+
if (node.type === "Identifier")
|
|
150
|
+
{ c(node, st, "VariablePattern"); }
|
|
151
|
+
else if (node.type === "MemberExpression")
|
|
152
|
+
{ c(node, st, "MemberPattern"); }
|
|
153
|
+
else
|
|
154
|
+
{ c(node, st); }
|
|
155
|
+
};
|
|
156
|
+
base.VariablePattern = ignore;
|
|
157
|
+
base.MemberPattern = skipThrough;
|
|
158
|
+
base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
|
|
159
|
+
base.ArrayPattern = function (node, st, c) {
|
|
160
|
+
for (var i = 0, list = node.elements; i < list.length; i += 1) {
|
|
161
|
+
var elt = list[i];
|
|
162
|
+
|
|
163
|
+
if (elt) { c(elt, st, "Pattern"); }
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
base.ObjectPattern = function (node, st, c) {
|
|
167
|
+
for (var i = 0, list = node.properties; i < list.length; i += 1) {
|
|
168
|
+
var prop = list[i];
|
|
169
|
+
|
|
170
|
+
if (prop.type === "Property") {
|
|
171
|
+
if (prop.computed) { c(prop.key, st, "Expression"); }
|
|
172
|
+
c(prop.value, st, "Pattern");
|
|
173
|
+
} else if (prop.type === "RestElement") {
|
|
174
|
+
c(prop.argument, st, "Pattern");
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
base.Expression = skipThrough;
|
|
180
|
+
base.ThisExpression = base.Super = base.MetaProperty = ignore;
|
|
181
|
+
base.ArrayExpression = function (node, st, c) {
|
|
182
|
+
for (var i = 0, list = node.elements; i < list.length; i += 1) {
|
|
183
|
+
var elt = list[i];
|
|
184
|
+
|
|
185
|
+
if (elt) { c(elt, st, "Expression"); }
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
base.ObjectExpression = function (node, st, c) {
|
|
189
|
+
for (var i = 0, list = node.properties; i < list.length; i += 1)
|
|
190
|
+
{
|
|
191
|
+
var prop = list[i];
|
|
192
|
+
|
|
193
|
+
c(prop, st);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
|
|
197
|
+
base.SequenceExpression = function (node, st, c) {
|
|
198
|
+
for (var i = 0, list = node.expressions; i < list.length; i += 1)
|
|
199
|
+
{
|
|
200
|
+
var expr = list[i];
|
|
201
|
+
|
|
202
|
+
c(expr, st, "Expression");
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
base.TemplateLiteral = function (node, st, c) {
|
|
206
|
+
for (var i = 0, list = node.quasis; i < list.length; i += 1)
|
|
207
|
+
{
|
|
208
|
+
var quasi = list[i];
|
|
209
|
+
|
|
210
|
+
c(quasi, st);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
for (var i$1 = 0, list$1 = node.expressions; i$1 < list$1.length; i$1 += 1)
|
|
214
|
+
{
|
|
215
|
+
var expr = list$1[i$1];
|
|
216
|
+
|
|
217
|
+
c(expr, st, "Expression");
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
base.TemplateElement = ignore;
|
|
221
|
+
base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
|
|
222
|
+
c(node.argument, st, "Expression");
|
|
223
|
+
};
|
|
224
|
+
base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
|
|
225
|
+
c(node.left, st, "Expression");
|
|
226
|
+
c(node.right, st, "Expression");
|
|
227
|
+
};
|
|
228
|
+
base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
|
|
229
|
+
c(node.left, st, "Pattern");
|
|
230
|
+
c(node.right, st, "Expression");
|
|
231
|
+
};
|
|
232
|
+
base.ConditionalExpression = function (node, st, c) {
|
|
233
|
+
c(node.test, st, "Expression");
|
|
234
|
+
c(node.consequent, st, "Expression");
|
|
235
|
+
c(node.alternate, st, "Expression");
|
|
236
|
+
};
|
|
237
|
+
base.NewExpression = base.CallExpression = function (node, st, c) {
|
|
238
|
+
c(node.callee, st, "Expression");
|
|
239
|
+
if (node.arguments)
|
|
240
|
+
{ for (var i = 0, list = node.arguments; i < list.length; i += 1)
|
|
241
|
+
{
|
|
242
|
+
var arg = list[i];
|
|
243
|
+
|
|
244
|
+
c(arg, st, "Expression");
|
|
245
|
+
} }
|
|
246
|
+
};
|
|
247
|
+
base.MemberExpression = function (node, st, c) {
|
|
248
|
+
c(node.object, st, "Expression");
|
|
249
|
+
if (node.computed) { c(node.property, st, "Expression"); }
|
|
250
|
+
};
|
|
251
|
+
base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
|
|
252
|
+
if (node.declaration)
|
|
253
|
+
{ c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
|
|
254
|
+
if (node.source) { c(node.source, st, "Expression"); }
|
|
255
|
+
if (node.attributes)
|
|
256
|
+
{ for (var i = 0, list = node.attributes; i < list.length; i += 1)
|
|
257
|
+
{
|
|
258
|
+
var attr = list[i];
|
|
259
|
+
|
|
260
|
+
c(attr, st);
|
|
261
|
+
} }
|
|
262
|
+
};
|
|
263
|
+
base.ExportAllDeclaration = function (node, st, c) {
|
|
264
|
+
if (node.exported)
|
|
265
|
+
{ c(node.exported, st); }
|
|
266
|
+
c(node.source, st, "Expression");
|
|
267
|
+
if (node.attributes)
|
|
268
|
+
{ for (var i = 0, list = node.attributes; i < list.length; i += 1)
|
|
269
|
+
{
|
|
270
|
+
var attr = list[i];
|
|
271
|
+
|
|
272
|
+
c(attr, st);
|
|
273
|
+
} }
|
|
274
|
+
};
|
|
275
|
+
base.ImportAttribute = function (node, st, c) {
|
|
276
|
+
c(node.value, st, "Expression");
|
|
277
|
+
};
|
|
278
|
+
base.ImportDeclaration = function (node, st, c) {
|
|
279
|
+
for (var i = 0, list = node.specifiers; i < list.length; i += 1)
|
|
280
|
+
{
|
|
281
|
+
var spec = list[i];
|
|
282
|
+
|
|
283
|
+
c(spec, st);
|
|
284
|
+
}
|
|
285
|
+
c(node.source, st, "Expression");
|
|
286
|
+
if (node.attributes)
|
|
287
|
+
{ for (var i$1 = 0, list$1 = node.attributes; i$1 < list$1.length; i$1 += 1)
|
|
288
|
+
{
|
|
289
|
+
var attr = list$1[i$1];
|
|
290
|
+
|
|
291
|
+
c(attr, st);
|
|
292
|
+
} }
|
|
293
|
+
};
|
|
294
|
+
base.ImportExpression = function (node, st, c) {
|
|
295
|
+
c(node.source, st, "Expression");
|
|
296
|
+
if (node.options) { c(node.options, st, "Expression"); }
|
|
297
|
+
};
|
|
298
|
+
base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.PrivateIdentifier = base.Literal = ignore;
|
|
299
|
+
|
|
300
|
+
base.TaggedTemplateExpression = function (node, st, c) {
|
|
301
|
+
c(node.tag, st, "Expression");
|
|
302
|
+
c(node.quasi, st, "Expression");
|
|
303
|
+
};
|
|
304
|
+
base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
|
|
305
|
+
base.Class = function (node, st, c) {
|
|
306
|
+
if (node.id) { c(node.id, st, "Pattern"); }
|
|
307
|
+
if (node.superClass) { c(node.superClass, st, "Expression"); }
|
|
308
|
+
c(node.body, st);
|
|
309
|
+
};
|
|
310
|
+
base.ClassBody = function (node, st, c) {
|
|
311
|
+
for (var i = 0, list = node.body; i < list.length; i += 1)
|
|
312
|
+
{
|
|
313
|
+
var elt = list[i];
|
|
314
|
+
|
|
315
|
+
c(elt, st);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
base.MethodDefinition = base.PropertyDefinition = base.Property = function (node, st, c) {
|
|
319
|
+
if (node.computed) { c(node.key, st, "Expression"); }
|
|
320
|
+
if (node.value) { c(node.value, st, "Expression"); }
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const API_NOT_FOUND_ERROR = `There are some problems in resolving the mocks API.
|
|
324
|
+
You may encounter this issue when importing the mocks API from another module other than 'vitest'.
|
|
325
|
+
To fix this issue you can either:
|
|
326
|
+
- import the mocks API directly from 'vitest'
|
|
327
|
+
- enable the 'globals' option`;
|
|
328
|
+
function API_NOT_FOUND_CHECK(names) {
|
|
329
|
+
return `\nif (${names.map((name) => `typeof globalThis["${name}"] === "undefined"`).join(" && ")}) ` + `{ throw new Error(${JSON.stringify(API_NOT_FOUND_ERROR)}) }\n`;
|
|
330
|
+
}
|
|
331
|
+
function isIdentifier(node) {
|
|
332
|
+
return node.type === "Identifier";
|
|
333
|
+
}
|
|
334
|
+
function getNodeTail(code, node) {
|
|
335
|
+
let end = node.end;
|
|
336
|
+
if (code[node.end] === ";") {
|
|
337
|
+
end += 1;
|
|
338
|
+
}
|
|
339
|
+
if (code[node.end] === "\n") {
|
|
340
|
+
return end + 1;
|
|
341
|
+
}
|
|
342
|
+
if (code[node.end + 1] === "\n") {
|
|
343
|
+
end += 1;
|
|
344
|
+
}
|
|
345
|
+
return end;
|
|
346
|
+
}
|
|
347
|
+
const regexpHoistable = /\b(?:vi|vitest)\s*\.\s*(?:mock|unmock|hoisted|doMock|doUnmock)\s*\(/;
|
|
348
|
+
const hashbangRE = /^#!.*\n/;
|
|
349
|
+
// this is a fork of Vite SSR transform
|
|
350
|
+
function hoistMocks(code, id, parse, options = {}) {
|
|
351
|
+
const needHoisting = (options.regexpHoistable || regexpHoistable).test(code);
|
|
352
|
+
if (!needHoisting) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const s = options.magicString?.() || new MagicString(code);
|
|
356
|
+
let ast;
|
|
357
|
+
try {
|
|
358
|
+
ast = parse(code);
|
|
359
|
+
} catch (err) {
|
|
360
|
+
console.error(`Cannot parse ${id}:\n${err.message}.`);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const { hoistableMockMethodNames = ["mock", "unmock"], dynamicImportMockMethodNames = [
|
|
364
|
+
"mock",
|
|
365
|
+
"unmock",
|
|
366
|
+
"doMock",
|
|
367
|
+
"doUnmock"
|
|
368
|
+
], hoistedMethodNames = ["hoisted"], utilsObjectNames = ["vi", "vitest"], hoistedModule = "vitest" } = options;
|
|
369
|
+
// hoist at the start of the file, after the hashbang
|
|
370
|
+
const hashbangEnd = hashbangRE.exec(code)?.[0].length ?? 0;
|
|
371
|
+
let hoistIndex = hashbangEnd;
|
|
372
|
+
let hoistedModuleImported = false;
|
|
373
|
+
let uid = 0;
|
|
374
|
+
const idToImportMap = new Map();
|
|
375
|
+
const imports = [];
|
|
376
|
+
// this will transform import statements into dynamic ones, if there are imports
|
|
377
|
+
// it will keep the import as is, if we don't need to mock anything
|
|
378
|
+
// in browser environment it will wrap the module value with "vitest_wrap_module" function
|
|
379
|
+
// that returns a proxy to the module so that named exports can be mocked
|
|
380
|
+
function defineImport(importNode) {
|
|
381
|
+
const source = importNode.source.value;
|
|
382
|
+
// always hoist vitest import to top of the file, so
|
|
383
|
+
// "vi" helpers can access it
|
|
384
|
+
if (hoistedModule === source || source === "vite-plus/test" || source === "@voidzero-dev/vite-plus-test") {
|
|
385
|
+
hoistedModuleImported = true;
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const importId = `__vi_import_${uid++}__`;
|
|
389
|
+
imports.push({
|
|
390
|
+
id: importId,
|
|
391
|
+
node: importNode
|
|
392
|
+
});
|
|
393
|
+
return importId;
|
|
394
|
+
}
|
|
395
|
+
// 1. check all import statements and record id -> importName map
|
|
396
|
+
for (const node of ast.body) {
|
|
397
|
+
// import foo from 'foo' --> foo -> __import_foo__.default
|
|
398
|
+
// import { baz } from 'foo' --> baz -> __import_foo__.baz
|
|
399
|
+
// import * as ok from 'foo' --> ok -> __import_foo__
|
|
400
|
+
if (node.type === "ImportDeclaration") {
|
|
401
|
+
const importId = defineImport(node);
|
|
402
|
+
if (!importId) {
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
for (const spec of node.specifiers) {
|
|
406
|
+
if (spec.type === "ImportSpecifier") {
|
|
407
|
+
if (spec.imported.type === "Identifier") {
|
|
408
|
+
idToImportMap.set(spec.local.name, `${importId}.${spec.imported.name}`);
|
|
409
|
+
} else {
|
|
410
|
+
idToImportMap.set(spec.local.name, `${importId}[${JSON.stringify(spec.imported.value)}]`);
|
|
411
|
+
}
|
|
412
|
+
} else if (spec.type === "ImportDefaultSpecifier") {
|
|
413
|
+
idToImportMap.set(spec.local.name, `${importId}.default`);
|
|
414
|
+
} else {
|
|
415
|
+
// namespace specifier
|
|
416
|
+
idToImportMap.set(spec.local.name, importId);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const declaredConst = new Set();
|
|
422
|
+
const hoistedNodes = new Set();
|
|
423
|
+
function createSyntaxError(node, message) {
|
|
424
|
+
const _error = new SyntaxError(message);
|
|
425
|
+
Error.captureStackTrace(_error, createSyntaxError);
|
|
426
|
+
const serializedError = {
|
|
427
|
+
name: "SyntaxError",
|
|
428
|
+
message: _error.message,
|
|
429
|
+
stack: _error.stack
|
|
430
|
+
};
|
|
431
|
+
if (options.codeFrameGenerator) {
|
|
432
|
+
serializedError.frame = options.codeFrameGenerator(node, id, code);
|
|
433
|
+
}
|
|
434
|
+
return serializedError;
|
|
435
|
+
}
|
|
436
|
+
function assertNotDefaultExport(node, error) {
|
|
437
|
+
const defaultExport = findNodeAround(ast, node.start, "ExportDefaultDeclaration")?.node;
|
|
438
|
+
if (defaultExport?.declaration === node || defaultExport?.declaration.type === "AwaitExpression" && defaultExport.declaration.argument === node) {
|
|
439
|
+
throw createSyntaxError(defaultExport, error);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
function assertNotNamedExport(node, error) {
|
|
443
|
+
const nodeExported = findNodeAround(ast, node.start, "ExportNamedDeclaration")?.node;
|
|
444
|
+
if (nodeExported?.declaration === node) {
|
|
445
|
+
throw createSyntaxError(nodeExported, error);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function getVariableDeclaration(node) {
|
|
449
|
+
const declarationNode = findNodeAround(ast, node.start, "VariableDeclaration")?.node;
|
|
450
|
+
const init = declarationNode?.declarations[0]?.init;
|
|
451
|
+
if (init && (init === node || init.type === "AwaitExpression" && init.argument === node)) {
|
|
452
|
+
return declarationNode;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
const usedUtilityExports = new Set();
|
|
456
|
+
let hasImportMetaVitest = false;
|
|
457
|
+
esmWalker(ast, {
|
|
458
|
+
onImportMeta(node) {
|
|
459
|
+
const property = code.slice(node.end, node.end + 7);
|
|
460
|
+
if (property === ".vitest") {
|
|
461
|
+
hasImportMetaVitest = true;
|
|
462
|
+
}
|
|
463
|
+
},
|
|
464
|
+
onIdentifier(id, info, parentStack) {
|
|
465
|
+
const binding = idToImportMap.get(id.name);
|
|
466
|
+
if (!binding) {
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
if (info.hasBindingShortcut) {
|
|
470
|
+
s.appendLeft(id.end, `: ${binding}`);
|
|
471
|
+
} else if (info.classDeclaration) {
|
|
472
|
+
if (!declaredConst.has(id.name)) {
|
|
473
|
+
declaredConst.add(id.name);
|
|
474
|
+
// locate the top-most node containing the class declaration
|
|
475
|
+
const topNode = parentStack[parentStack.length - 2];
|
|
476
|
+
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`);
|
|
477
|
+
}
|
|
478
|
+
} else if (!info.classExpression) {
|
|
479
|
+
s.update(id.start, id.end, binding);
|
|
480
|
+
}
|
|
481
|
+
},
|
|
482
|
+
onDynamicImport(_node) {
|
|
483
|
+
// TODO: vi.mock(import) breaks it, and vi.mock('', () => import) also does,
|
|
484
|
+
// only move imports that are outside of vi.mock
|
|
485
|
+
// backwards compat, don't do if not passed
|
|
486
|
+
// if (!options.globalThisAccessor) {
|
|
487
|
+
// return
|
|
488
|
+
// }
|
|
489
|
+
// const globalThisAccessor = options.globalThisAccessor
|
|
490
|
+
// const replaceString = `globalThis[${globalThisAccessor}].wrapDynamicImport(() => import(`
|
|
491
|
+
// const importSubstring = code.substring(node.start, node.end)
|
|
492
|
+
// const hasIgnore = importSubstring.includes('/* @vite-ignore */')
|
|
493
|
+
// s.overwrite(
|
|
494
|
+
// node.start,
|
|
495
|
+
// (node.source as Positioned<Expression>).start,
|
|
496
|
+
// replaceString + (hasIgnore ? '/* @vite-ignore */ ' : ''),
|
|
497
|
+
// )
|
|
498
|
+
// s.overwrite(node.end - 1, node.end, '))')
|
|
499
|
+
},
|
|
500
|
+
onCallExpression(node) {
|
|
501
|
+
if (node.callee.type === "MemberExpression" && isIdentifier(node.callee.object) && utilsObjectNames.includes(node.callee.object.name) && isIdentifier(node.callee.property)) {
|
|
502
|
+
const methodName = node.callee.property.name;
|
|
503
|
+
usedUtilityExports.add(node.callee.object.name);
|
|
504
|
+
if (hoistableMockMethodNames.includes(methodName)) {
|
|
505
|
+
const method = `${node.callee.object.name}.${methodName}`;
|
|
506
|
+
assertNotDefaultExport(node, `Cannot export the result of "${method}". Remove export declaration because "${method}" doesn\'t return anything.`);
|
|
507
|
+
const declarationNode = getVariableDeclaration(node);
|
|
508
|
+
if (declarationNode) {
|
|
509
|
+
assertNotNamedExport(declarationNode, `Cannot export the result of "${method}". Remove export declaration because "${method}" doesn\'t return anything.`);
|
|
510
|
+
}
|
|
511
|
+
// rewrite vi.mock(import('..')) into vi.mock('..')
|
|
512
|
+
if (node.type === "CallExpression" && node.callee.type === "MemberExpression" && dynamicImportMockMethodNames.includes(node.callee.property.name)) {
|
|
513
|
+
const moduleInfo = node.arguments[0];
|
|
514
|
+
// vi.mock(import('./path')) -> vi.mock('./path')
|
|
515
|
+
if (moduleInfo.type === "ImportExpression") {
|
|
516
|
+
const source = moduleInfo.source;
|
|
517
|
+
s.overwrite(moduleInfo.start, moduleInfo.end, s.slice(source.start, source.end));
|
|
518
|
+
}
|
|
519
|
+
// vi.mock(await import('./path')) -> vi.mock('./path')
|
|
520
|
+
if (moduleInfo.type === "AwaitExpression" && moduleInfo.argument.type === "ImportExpression") {
|
|
521
|
+
const source = moduleInfo.argument.source;
|
|
522
|
+
s.overwrite(moduleInfo.start, moduleInfo.end, s.slice(source.start, source.end));
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
hoistedNodes.add(node);
|
|
526
|
+
} else if (dynamicImportMockMethodNames.includes(methodName)) {
|
|
527
|
+
const moduleInfo = node.arguments[0];
|
|
528
|
+
let source = null;
|
|
529
|
+
if (moduleInfo.type === "ImportExpression") {
|
|
530
|
+
source = moduleInfo.source;
|
|
531
|
+
}
|
|
532
|
+
if (moduleInfo.type === "AwaitExpression" && moduleInfo.argument.type === "ImportExpression") {
|
|
533
|
+
source = moduleInfo.argument.source;
|
|
534
|
+
}
|
|
535
|
+
if (source) {
|
|
536
|
+
s.overwrite(moduleInfo.start, moduleInfo.end, s.slice(source.start, source.end));
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (hoistedMethodNames.includes(methodName)) {
|
|
540
|
+
assertNotDefaultExport(node, "Cannot export hoisted variable. You can control hoisting behavior by placing the import from this file first.");
|
|
541
|
+
const declarationNode = getVariableDeclaration(node);
|
|
542
|
+
if (declarationNode) {
|
|
543
|
+
assertNotNamedExport(declarationNode, "Cannot export hoisted variable. You can control hoisting behavior by placing the import from this file first.");
|
|
544
|
+
// hoist "const variable = vi.hoisted(() => {})"
|
|
545
|
+
hoistedNodes.add(declarationNode);
|
|
546
|
+
} else {
|
|
547
|
+
const awaitedExpression = findNodeAround(ast, node.start, "AwaitExpression")?.node;
|
|
548
|
+
// hoist "await vi.hoisted(async () => {})" or "vi.hoisted(() => {})"
|
|
549
|
+
const moveNode = awaitedExpression?.argument === node ? awaitedExpression : node;
|
|
550
|
+
hoistedNodes.add(moveNode);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
function getNodeName(node) {
|
|
557
|
+
const callee = node.callee || {};
|
|
558
|
+
if (callee.type === "MemberExpression" && isIdentifier(callee.property) && isIdentifier(callee.object)) {
|
|
559
|
+
const argument = node.arguments[0];
|
|
560
|
+
const argStr = argument.type === "Literal" || argument.type === "ImportExpression" ? code.slice(argument.start, argument.end) : "";
|
|
561
|
+
return `${callee.object.name}.${callee.property.name}(${argStr})`;
|
|
562
|
+
}
|
|
563
|
+
return "\"hoisted method\"";
|
|
564
|
+
}
|
|
565
|
+
function getNodeCall(node) {
|
|
566
|
+
if (node.type === "CallExpression") {
|
|
567
|
+
return node;
|
|
568
|
+
}
|
|
569
|
+
if (node.type === "VariableDeclaration") {
|
|
570
|
+
const { declarations } = node;
|
|
571
|
+
const init = declarations[0].init;
|
|
572
|
+
if (init) {
|
|
573
|
+
return getNodeCall(init);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (node.type === "AwaitExpression") {
|
|
577
|
+
const { argument } = node;
|
|
578
|
+
if (argument.type === "CallExpression") {
|
|
579
|
+
return getNodeCall(argument);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return node;
|
|
583
|
+
}
|
|
584
|
+
function createError(outsideNode, insideNode) {
|
|
585
|
+
const outsideCall = getNodeCall(outsideNode);
|
|
586
|
+
const insideCall = getNodeCall(insideNode);
|
|
587
|
+
throw createSyntaxError(insideCall, `Cannot call ${getNodeName(insideCall)} inside ${getNodeName(outsideCall)}: both methods are hoisted to the top of the file and not actually called inside each other.`);
|
|
588
|
+
}
|
|
589
|
+
// validate hoistedNodes doesn't have nodes inside other nodes
|
|
590
|
+
const arrayNodes = Array.from(hoistedNodes);
|
|
591
|
+
for (let i = 0; i < arrayNodes.length; i++) {
|
|
592
|
+
const node = arrayNodes[i];
|
|
593
|
+
for (let j = i + 1; j < arrayNodes.length; j++) {
|
|
594
|
+
const otherNode = arrayNodes[j];
|
|
595
|
+
if (node.start >= otherNode.start && node.end <= otherNode.end) {
|
|
596
|
+
throw createError(otherNode, node);
|
|
597
|
+
}
|
|
598
|
+
if (otherNode.start >= node.start && otherNode.end <= node.end) {
|
|
599
|
+
throw createError(node, otherNode);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
// validate that hoisted nodes are defined on the top level
|
|
604
|
+
// ignore `import.meta.vitest` because it needs to be inside an IfStatement
|
|
605
|
+
// and it can be used anywhere in the code (inside methods too)
|
|
606
|
+
if (!hasImportMetaVitest) {
|
|
607
|
+
for (const node of ast.body) {
|
|
608
|
+
hoistedNodes.delete(node);
|
|
609
|
+
if (node.type === "ExpressionStatement") {
|
|
610
|
+
hoistedNodes.delete(node.expression);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
for (const invalidNode of hoistedNodes) {
|
|
614
|
+
console.warn(`Warning: A ${getNodeName(getNodeCall(invalidNode))} call in "${id}" is not at the top level of the module. ` + `Although it appears nested, it will be hoisted and executed before any tests run. ` + `Move it to the top level to reflect its actual execution order. This will become an error in a future version.\n` + `See: https://vitest.dev/guide/mocking/modules#how-it-works`);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
// hoist vi.mock/vi.hoisted
|
|
618
|
+
for (const node of arrayNodes) {
|
|
619
|
+
const end = getNodeTail(code, node);
|
|
620
|
+
// don't hoist into itself if it's already at the top
|
|
621
|
+
if (hoistIndex === end || hoistIndex === node.start) {
|
|
622
|
+
hoistIndex = end;
|
|
623
|
+
} else {
|
|
624
|
+
s.move(node.start, end, hoistIndex);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
// hoist actual dynamic imports last so they are inserted after all hoisted mocks
|
|
628
|
+
for (const { node: importNode, id: importId } of imports) {
|
|
629
|
+
const source = importNode.source.value;
|
|
630
|
+
const sourceString = JSON.stringify(source);
|
|
631
|
+
let importLine = `const ${importId} = await `;
|
|
632
|
+
if (options.globalThisAccessor) {
|
|
633
|
+
importLine += `globalThis[${options.globalThisAccessor}].wrapDynamicImport(() => import(${sourceString}));\n`;
|
|
634
|
+
} else {
|
|
635
|
+
importLine += `import(${sourceString});\n`;
|
|
636
|
+
}
|
|
637
|
+
s.update(importNode.start, importNode.end, importLine);
|
|
638
|
+
if (importNode.start === hoistIndex) {
|
|
639
|
+
// no need to hoist, but update hoistIndex to keep the order
|
|
640
|
+
hoistIndex = importNode.end;
|
|
641
|
+
} else {
|
|
642
|
+
// There will be an error if the module is called before it is imported,
|
|
643
|
+
// so the module import statement is hoisted to the top
|
|
644
|
+
s.move(importNode.start, importNode.end, hoistIndex);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
if (!hoistedModuleImported && arrayNodes.length > 0) {
|
|
648
|
+
const utilityImports = [...usedUtilityExports];
|
|
649
|
+
// "vi" or "vitest" is imported from a module other than "vitest"
|
|
650
|
+
if (utilityImports.some((name) => idToImportMap.has(name))) {
|
|
651
|
+
s.appendLeft(hashbangEnd, API_NOT_FOUND_CHECK(utilityImports));
|
|
652
|
+
} else if (utilityImports.length) {
|
|
653
|
+
s.appendLeft(hashbangEnd, `import { ${[...usedUtilityExports].join(", ")} } from ${JSON.stringify(hoistedModule)}\n`);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return s;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
export { hoistMocks as h };
|