vitest-native 0.14.0-rc.9 → 0.14.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/README.md +76 -60
- package/dist/cli.d.mts +1 -3
- package/dist/cli.mjs +175 -63
- package/dist/context.d.mts +45 -0
- package/dist/context.mjs +243 -0
- package/dist/errors.d.mts +3 -0
- package/dist/helpers.cjs +53 -4
- package/dist/helpers.d.cts +1 -3
- package/dist/helpers.d.mts +1 -3
- package/dist/helpers.mjs +2 -3
- package/dist/host-5o48Ujzs.mjs +939 -0
- package/dist/{index-CzQazgp9.d.cts → index-DqU4YO3D.d.mts} +27 -49
- package/dist/index.d.mts +1 -3
- package/dist/index.mjs +463 -595
- package/dist/jest-compat/error-equality.mjs +127 -0
- package/dist/jest-compat/jest-object.mjs +149 -4
- package/dist/jest-compat/node-registry.mjs +233 -58
- package/dist/jest-compat/setup.mjs +28 -4
- package/dist/jest-compat.cjs +227 -52
- package/dist/jest-compat.d.cts +5 -6
- package/dist/jest-compat.d.mts +5 -6
- package/dist/jest-compat.mjs +204 -52
- package/dist/matchers.cjs +1 -2
- package/dist/matchers.d.cts +0 -2
- package/dist/matchers.d.mts +0 -2
- package/dist/matchers.mjs +1 -2
- package/dist/{memory-FYbP0_gg.mjs → memory-Couw0Esx.mjs} +31 -6
- package/dist/native/assets.d.mts +2 -0
- package/dist/native/assets.mjs +34 -0
- package/dist/native/babel-toolchain.mjs +190 -0
- package/dist/native/babel-worker.mjs +47 -0
- package/dist/native/boundary.mjs +23 -8
- package/dist/native/compile-cache.mjs +21 -9
- package/dist/native/coverage-v8.mjs +287 -0
- package/dist/native/esm-interop.mjs +216 -0
- package/dist/native/expo-native-modules.mjs +7 -1
- package/dist/native/globals.mjs +104 -6
- package/dist/native/hooks.mjs +271 -60
- package/dist/native/loader.mjs +378 -20
- package/dist/native/match.d.mts +24 -2
- package/dist/native/match.mjs +117 -12
- package/dist/native/metro-profile-compiler.mjs +2 -31
- package/dist/native/module-reset.mjs +37 -5
- package/dist/native/native-modules.d.mts +2 -3
- package/dist/native/native-modules.mjs +22 -21
- package/dist/native/ownership.d.mts +10 -0
- package/dist/native/ownership.mjs +96 -9
- package/dist/native/registry-compiler.mjs +6 -0
- package/dist/native/registry.mjs +138 -119
- package/dist/native/resident-health.mjs +133 -0
- package/dist/native/resolve.d.mts +6 -0
- package/dist/native/resolve.mjs +100 -9
- package/dist/native/rntl-hooks.mjs +157 -6
- package/dist/native/rntl-text-host.mjs +64 -0
- package/dist/native/runner.mjs +73 -0
- package/dist/native/setup.mjs +96 -70
- package/dist/native/state-manifest.mjs +14 -8
- package/dist/native/transform.d.mts +2 -0
- package/dist/native/transform.mjs +476 -59
- package/dist/native/worker.mjs +65 -36
- package/dist/{pool-DjpOjOf3.mjs → pool-CFSpNfyw.mjs} +37 -11
- package/dist/{preset-map-BlBriTA0.mjs → preset-map-CogjYmSw.mjs} +4 -19
- package/dist/preset-modules.mjs +2 -11
- package/dist/{presets--zrEWQC2.mjs → presets-CXDyg51l.mjs} +65 -82
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/report-error.d.mts +20 -0
- package/dist/report-error.mjs +145 -0
- package/dist/rntl-support.d.mts +12 -0
- package/dist/rntl-support.mjs +189 -0
- package/dist/{rolldown-runtime-BhDjJH2R.mjs → rolldown-runtime-D70C1uOS.mjs} +1 -2
- package/dist/serializer.cjs +1 -2
- package/dist/serializer.d.cts +1 -3
- package/dist/serializer.d.mts +1 -3
- package/dist/serializer.mjs +1 -2
- package/dist/setup.mjs +95 -156
- package/dist/{worker-vitest-BGPo0ek6.mjs → worker-vitest-CILPFeRX.mjs} +2 -3
- package/docs/migrating-from-jest.md +76 -12
- package/docs/release-readiness.md +1 -1
- package/package.json +119 -102
- package/CHANGELOG.md +0 -2491
- package/dist/cli.cjs +0 -1951
- package/dist/cli.d.cts +0 -3
- package/dist/ecosystem-D6e1OVOg.mjs +0 -1019
- package/dist/ecosystem-MFwkbnaC.cjs +0 -1099
- package/dist/errors-B3lJK46O.cjs +0 -75
- package/dist/errors-CxoBUfVJ.mjs +0 -64
- package/dist/index-xw43lBlv.d.mts +0 -481
- package/dist/memory-6EPHrJA1.cjs +0 -144
- package/dist/pool-B4yesGhI.cjs +0 -121
- package/dist/preset-map-jSFsNy1Q.cjs +0 -241
- package/dist/preset-modules-BoLkaeEN.cjs +0 -53
- package/dist/preset-modules-CG8-G82P.mjs +0 -36
- package/dist/presets-9f83H0WW.cjs +0 -3939
- package/dist/rolldown-runtime-C0BPl7ul.cjs +0 -43
- package/dist/worker-vitest-BfiQB8ND.cjs +0 -62
- package/docs/hot-default-design.md +0 -216
package/dist/jest-compat.cjs
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
let node_fs = require("node:fs");
|
|
24
|
+
node_fs = __toESM(node_fs, 1);
|
|
25
|
+
let node_path = require("node:path");
|
|
26
|
+
node_path = __toESM(node_path, 1);
|
|
2
27
|
let magic_string = require("magic-string");
|
|
3
|
-
|
|
28
|
+
// src/jest-compat/transform.ts
|
|
4
29
|
const HOISTABLE = /* @__PURE__ */ new Set([
|
|
5
30
|
"mock",
|
|
6
31
|
"unmock",
|
|
@@ -14,10 +39,13 @@ const HAS_JEST_MOCK = /\bjest\s*\.\s*(?:mock|unmock|doMock|doUnmock)\s*\(/;
|
|
|
14
39
|
const SETUP = "__vn_jest_compat__";
|
|
15
40
|
/** Prepended to a file whose mock factories use the setup. Exported for tests. */
|
|
16
41
|
const SETUP_ACCESSOR = `const ${SETUP} = vi.hoisted(() => (name) => {\n if (typeof globalThis[name] === "function") return globalThis[name];\n throw Object.assign(new Error("[vitest-native] jest.mock() factories need the jest-compat setup, which this test run does not load. " +\n "Add jestCompatSetup from 'vitest-native/jest-compat' to test.setupFiles, ahead of any setup file that calls jest.mock()."),\n { name: "VitestNativeError", code: "JEST_COMPAT_SETUP_MISSING" });\n});\n`;
|
|
17
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Visit every node in an ESTree AST (depth-first), calling `fn` on each. Returning
|
|
44
|
+
* `false` from `fn` skips that node's children.
|
|
45
|
+
*/
|
|
18
46
|
function walk(node, fn) {
|
|
19
47
|
if (!node || typeof node.type !== "string") return;
|
|
20
|
-
fn(node);
|
|
48
|
+
if (fn(node) === false) return;
|
|
21
49
|
for (const key in node) {
|
|
22
50
|
if (key === "type" || key === "start" || key === "end" || key === "loc") continue;
|
|
23
51
|
const child = node[key];
|
|
@@ -26,6 +54,78 @@ function walk(node, fn) {
|
|
|
26
54
|
} else if (child && typeof child.type === "string") walk(child, fn);
|
|
27
55
|
}
|
|
28
56
|
}
|
|
57
|
+
const FUNCTION = /* @__PURE__ */ new Set([
|
|
58
|
+
"FunctionExpression",
|
|
59
|
+
"ArrowFunctionExpression",
|
|
60
|
+
"FunctionDeclaration"
|
|
61
|
+
]);
|
|
62
|
+
const NODE_MODULES = /[\\/]node_modules[\\/]/;
|
|
63
|
+
const SOURCE_FILE = /\.[cm]?[jt]sx?$/;
|
|
64
|
+
/** The string specifier of a `jest.requireActual('<literal>')` call, or null. */
|
|
65
|
+
function requireActualSpec(node) {
|
|
66
|
+
if (node.type !== "CallExpression" || node.arguments.length !== 1) return null;
|
|
67
|
+
const callee = node.callee;
|
|
68
|
+
if (callee?.type !== "MemberExpression" || callee.computed) return null;
|
|
69
|
+
if (callee.object?.type !== "Identifier" || callee.object.name !== "jest") return null;
|
|
70
|
+
if (callee.property?.type !== "Identifier" || callee.property.name !== "requireActual") return null;
|
|
71
|
+
const arg = node.arguments[0];
|
|
72
|
+
if (arg.type === "Literal" && typeof arg.value === "string") return arg.value;
|
|
73
|
+
if (arg.type === "TemplateLiteral" && arg.expressions.length === 0) return arg.quasis[0].value.cooked ?? null;
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The `jest.requireActual` calls a factory makes while it runs: those in its own body,
|
|
78
|
+
* not in a nested function or a class body (where `await` is not allowed either).
|
|
79
|
+
*/
|
|
80
|
+
function ownRequireActualCalls(factory) {
|
|
81
|
+
const calls = [];
|
|
82
|
+
walk(factory.body, (node) => {
|
|
83
|
+
if (FUNCTION.has(node.type) || node.type === "ClassBody") return false;
|
|
84
|
+
const spec = requireActualSpec(node);
|
|
85
|
+
if (spec !== null) calls.push({
|
|
86
|
+
node,
|
|
87
|
+
spec
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
return calls;
|
|
91
|
+
}
|
|
92
|
+
const matchesRegExp = (patterns, file) => patterns.some((pattern) => {
|
|
93
|
+
if (pattern === true) return true;
|
|
94
|
+
if (!(pattern instanceof RegExp)) return false;
|
|
95
|
+
pattern.lastIndex = 0;
|
|
96
|
+
const hit = pattern.test(file);
|
|
97
|
+
pattern.lastIndex = 0;
|
|
98
|
+
return hit;
|
|
99
|
+
});
|
|
100
|
+
/**
|
|
101
|
+
* Whether a resolveId result is a source file Vite evaluates itself, so the real
|
|
102
|
+
* module `vi.importActual` loads is the one the app imports.
|
|
103
|
+
*
|
|
104
|
+
* The owner is the engine's ownership policy, as for every other load: a workspace
|
|
105
|
+
* package the engine detected as React Native, or one named in `transform`, resolves
|
|
106
|
+
* outside node_modules but is Node's, and loading its actual module through Vite would
|
|
107
|
+
* make the second copy this rewrite exists to avoid. `resolved.external` cannot say so:
|
|
108
|
+
* Vitest externalizes in its module runner, after resolveId, and never sets it (this
|
|
109
|
+
* change's first version relied on it and refused such a partial mock as a cycle).
|
|
110
|
+
* Where the policy defers to Vitest, or there is none (the mock engine), Vitest's own
|
|
111
|
+
* rule applies: a file outside node_modules is inlined unless a `server.deps.external`
|
|
112
|
+
* regex names it and no `server.deps.inline` regex (or `inline: true`) does. The engine also lists its
|
|
113
|
+
* Node-owned patterns there (native/apply.ts), so the two agree.
|
|
114
|
+
*/
|
|
115
|
+
function isViteOwnedSource(resolved, project) {
|
|
116
|
+
if (!resolved || resolved.external) return false;
|
|
117
|
+
const id = resolved.id.split("?")[0];
|
|
118
|
+
if (id.startsWith("\0") || !node_path.default.isAbsolute(id) || !SOURCE_FILE.test(id)) return false;
|
|
119
|
+
let file = id;
|
|
120
|
+
try {
|
|
121
|
+
file = node_fs.default.realpathSync.native(id);
|
|
122
|
+
} catch {}
|
|
123
|
+
const owner = project.ownership()?.decideFile(file).owner;
|
|
124
|
+
if (owner === "vite") return true;
|
|
125
|
+
if (owner === "node" || NODE_MODULES.test(file)) return false;
|
|
126
|
+
const normalized = file.replace(/\\/g, "/");
|
|
127
|
+
return matchesRegExp(project.inline, normalized) || !matchesRegExp(project.external, normalized);
|
|
128
|
+
}
|
|
29
129
|
/**
|
|
30
130
|
* Vite plugin that adapts a Jest suite's `jest.mock(...)` calls for Vitest:
|
|
31
131
|
*
|
|
@@ -51,8 +151,23 @@ function walk(node, fn) {
|
|
|
51
151
|
* Opt-in: add after `reactNative()`; pair with `jestCompatSetup` + `globals: true`.
|
|
52
152
|
*/
|
|
53
153
|
function jestMockTransform() {
|
|
154
|
+
let project = {
|
|
155
|
+
ownership: () => null,
|
|
156
|
+
inline: [],
|
|
157
|
+
external: []
|
|
158
|
+
};
|
|
54
159
|
return {
|
|
55
160
|
name: "vitest-native:jest-mock-hoist",
|
|
161
|
+
configResolved(config) {
|
|
162
|
+
const native = config.plugins.find((plugin) => plugin.name === "vitest-native");
|
|
163
|
+
const deps = config.test?.server?.deps;
|
|
164
|
+
const list = (value) => Array.isArray(value) ? value : value == null ? [] : [value];
|
|
165
|
+
project = {
|
|
166
|
+
ownership: () => native?.api?.nativeOwnership?.() ?? null,
|
|
167
|
+
inline: list(deps?.inline),
|
|
168
|
+
external: list(deps?.external)
|
|
169
|
+
};
|
|
170
|
+
},
|
|
56
171
|
transform(code, id) {
|
|
57
172
|
if (id.includes("/node_modules/")) return null;
|
|
58
173
|
const file = id.split("?")[0];
|
|
@@ -64,60 +179,121 @@ function jestMockTransform() {
|
|
|
64
179
|
} catch {
|
|
65
180
|
return null;
|
|
66
181
|
}
|
|
67
|
-
const
|
|
68
|
-
let changed = false;
|
|
69
|
-
const topLevel = /* @__PURE__ */ new Map();
|
|
70
|
-
for (const statement of ast.body) if (statement.type === "ExpressionStatement") topLevel.set(statement.expression, statement);
|
|
71
|
-
let usesSetup = false;
|
|
72
|
-
const replaced = [];
|
|
182
|
+
const candidates = /* @__PURE__ */ new Map();
|
|
73
183
|
walk(ast, (node) => {
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
const
|
|
77
|
-
if (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (WITH_FACTORY.has(prop.name) && node.arguments.length >= 2) {
|
|
87
|
-
const factory = node.arguments[1];
|
|
88
|
-
if (factory.type === "ArrowFunctionExpression" || factory.type === "FunctionExpression") {
|
|
89
|
-
usesSetup = true;
|
|
90
|
-
if (spec !== null) {
|
|
91
|
-
const register = `globalThis.__vnJestMock?.(import.meta.url, ${spec}, ${code.slice(factory.start, factory.end)})`;
|
|
92
|
-
const lookup = `() => ${SETUP}("__vnJestMocked")(import.meta.url, ${spec})`;
|
|
93
|
-
const statement = topLevel.get(node);
|
|
94
|
-
if (prop.name === "mock" && statement) {
|
|
95
|
-
s.prependLeft(statement.start, `vi.hoisted(() => ${register});\n`);
|
|
96
|
-
s.overwrite(factory.start, factory.end, lookup);
|
|
97
|
-
} else s.overwrite(factory.start, factory.end, `(${register}, ${lookup})`);
|
|
98
|
-
replaced.push([factory.start, factory.end]);
|
|
99
|
-
} else {
|
|
100
|
-
s.appendLeft(factory.start, `() => ${SETUP}("__vnInteropMock")((`);
|
|
101
|
-
s.appendRight(factory.end, ")())");
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
} else if (UNMOCK.has(prop.name) && spec !== null) {
|
|
105
|
-
s.appendLeft(specNode.start, `(globalThis.__vnJestUnmock?.(import.meta.url, ${spec}), `);
|
|
106
|
-
s.appendRight(specNode.end, ")");
|
|
184
|
+
const factory = mockFactoryOf(node);
|
|
185
|
+
if (!factory || factory.generator) return;
|
|
186
|
+
const calls = ownRequireActualCalls(factory);
|
|
187
|
+
if (calls.length > 0) candidates.set(factory, calls);
|
|
188
|
+
});
|
|
189
|
+
if (candidates.size === 0) return rewrite(code, ast, /* @__PURE__ */ new Map());
|
|
190
|
+
const specs = new Set([...candidates.values()].flat().map((call) => call.spec));
|
|
191
|
+
return Promise.all([...specs].map(async (spec) => {
|
|
192
|
+
try {
|
|
193
|
+
return [spec, isViteOwnedSource(await this.resolve(spec, id), project)];
|
|
194
|
+
} catch {
|
|
195
|
+
return [spec, false];
|
|
107
196
|
}
|
|
197
|
+
})).then((resolved) => {
|
|
198
|
+
const owned = new Set(resolved.filter(([, yes]) => yes).map(([spec]) => spec));
|
|
199
|
+
const toAsync = /* @__PURE__ */ new Map();
|
|
200
|
+
for (const [factory, calls] of candidates) {
|
|
201
|
+
const moved = calls.filter((call) => owned.has(call.spec)).map((call) => call.node);
|
|
202
|
+
if (moved.length > 0) toAsync.set(factory, moved);
|
|
203
|
+
}
|
|
204
|
+
return rewrite(code, ast, toAsync);
|
|
108
205
|
});
|
|
109
|
-
if (!changed) return null;
|
|
110
|
-
if (usesSetup) s.prepend(SETUP_ACCESSOR);
|
|
111
|
-
return {
|
|
112
|
-
code: s.toString(),
|
|
113
|
-
map: s.generateMap({ hires: true })
|
|
114
|
-
};
|
|
115
206
|
}
|
|
116
207
|
};
|
|
117
208
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
209
|
+
/** The factory of a `jest.mock`/`jest.doMock` call with an inline function factory. */
|
|
210
|
+
function mockFactoryOf(node) {
|
|
211
|
+
if (node.type !== "CallExpression" || node.arguments.length < 2) return null;
|
|
212
|
+
const callee = node.callee;
|
|
213
|
+
if (callee?.type !== "MemberExpression" || callee.computed) return null;
|
|
214
|
+
if (callee.object?.type !== "Identifier" || callee.object.name !== "jest") return null;
|
|
215
|
+
if (callee.property?.type !== "Identifier" || !WITH_FACTORY.has(callee.property.name)) return null;
|
|
216
|
+
const factory = node.arguments[1];
|
|
217
|
+
return factory.type === "ArrowFunctionExpression" || factory.type === "FunctionExpression" ? factory : null;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* The transform itself. `toAsync` maps a factory to the `jest.requireActual` calls that
|
|
221
|
+
* load through Vite instead (see "`jest.requireActual` of project source" above).
|
|
222
|
+
*/
|
|
223
|
+
function rewrite(code, ast, toAsync) {
|
|
224
|
+
const s = new magic_string.MagicString(code);
|
|
225
|
+
let changed = false;
|
|
226
|
+
const topLevel = /* @__PURE__ */ new Map();
|
|
227
|
+
for (const statement of ast.body) if (statement.type === "ExpressionStatement") topLevel.set(statement.expression, statement);
|
|
228
|
+
let usesSetup = false;
|
|
229
|
+
const replaced = [];
|
|
230
|
+
/** The factory's source, async, with its own project `requireActual`s via Vite. */
|
|
231
|
+
const asyncFactory = (factory, calls) => {
|
|
232
|
+
let text = "";
|
|
233
|
+
let at = factory.start;
|
|
234
|
+
for (const call of [...calls].sort((a, b) => a.start - b.start)) {
|
|
235
|
+
const arg = call.arguments[0];
|
|
236
|
+
text += code.slice(at, call.start);
|
|
237
|
+
text += `(${SETUP}("__vnActualExports")(await vi.importActual(${code.slice(arg.start, arg.end)})))`;
|
|
238
|
+
at = call.end;
|
|
239
|
+
}
|
|
240
|
+
text += code.slice(at, factory.end);
|
|
241
|
+
return factory.async ? text : `async ${text}`;
|
|
242
|
+
};
|
|
243
|
+
walk(ast, (node) => {
|
|
244
|
+
if (node.type !== "CallExpression") return;
|
|
245
|
+
if (replaced.some(([start, end]) => node.start >= start && node.end <= end)) return;
|
|
246
|
+
const callee = node.callee;
|
|
247
|
+
if (!callee || callee.type !== "MemberExpression" || callee.computed) return;
|
|
248
|
+
const obj = callee.object;
|
|
249
|
+
const prop = callee.property;
|
|
250
|
+
if (!obj || obj.type !== "Identifier" || obj.name !== "jest") return;
|
|
251
|
+
if (!prop || prop.type !== "Identifier" || !HOISTABLE.has(prop.name)) return;
|
|
252
|
+
s.overwrite(obj.start, obj.end, "vi");
|
|
253
|
+
changed = true;
|
|
254
|
+
const specNode = node.arguments[0];
|
|
255
|
+
const spec = specNode && (specNode.type === "Literal" && typeof specNode.value === "string" || specNode.type === "TemplateLiteral" && specNode.expressions.length === 0 || specNode.type === "Identifier") ? code.slice(specNode.start, specNode.end) : null;
|
|
256
|
+
if (WITH_FACTORY.has(prop.name) && node.arguments.length >= 2) {
|
|
257
|
+
const factory = node.arguments[1];
|
|
258
|
+
if (factory.type === "ArrowFunctionExpression" || factory.type === "FunctionExpression") {
|
|
259
|
+
usesSetup = true;
|
|
260
|
+
const moved = toAsync.get(factory);
|
|
261
|
+
if (spec !== null) {
|
|
262
|
+
const original = code.slice(factory.start, factory.end);
|
|
263
|
+
const register = `globalThis.__vnJestMock?.(import.meta.url, ${spec}, ${moved ? `${original}, ${asyncFactory(factory, moved)}` : original})`;
|
|
264
|
+
const lookup = `() => ${SETUP}("__vnJestMocked")(import.meta.url, ${spec})`;
|
|
265
|
+
const statement = topLevel.get(node);
|
|
266
|
+
if (prop.name === "mock" && statement) {
|
|
267
|
+
s.prependLeft(statement.start, `vi.hoisted(() => ${register});\n`);
|
|
268
|
+
s.overwrite(factory.start, factory.end, lookup);
|
|
269
|
+
} else s.overwrite(factory.start, factory.end, `(${register}, ${lookup})`);
|
|
270
|
+
replaced.push([factory.start, factory.end]);
|
|
271
|
+
} else {
|
|
272
|
+
if (moved) {
|
|
273
|
+
s.overwrite(factory.start, factory.end, asyncFactory(factory, moved));
|
|
274
|
+
replaced.push([factory.start, factory.end]);
|
|
275
|
+
}
|
|
276
|
+
s.appendLeft(factory.start, `() => ${SETUP}("__vnInteropMock")((`);
|
|
277
|
+
s.appendRight(factory.end, ")())");
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
} else if (UNMOCK.has(prop.name) && spec !== null) {
|
|
281
|
+
s.appendLeft(specNode.start, `(globalThis.__vnJestUnmock?.(import.meta.url, ${spec}), `);
|
|
282
|
+
s.appendRight(specNode.end, ")");
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
if (!changed) return null;
|
|
286
|
+
if (usesSetup) s.prepend(SETUP_ACCESSOR);
|
|
287
|
+
return {
|
|
288
|
+
code: s.toString(),
|
|
289
|
+
map: s.generateMap({ hires: true })
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
// src/jest-compat/index.ts
|
|
293
|
+
/**
|
|
294
|
+
* Setup-file specifier: gives the suite a `jest` global backed by Vitest's `vi`, with the
|
|
295
|
+
* synchronous `jest.requireActual` and `jest.requireMock`.
|
|
296
|
+
*/
|
|
121
297
|
const jestCompatSetup = "vitest-native/jest-compat/setup";
|
|
122
298
|
/**
|
|
123
299
|
* `resolve.alias` entries that redirect Jest-only modules to Vitest-backed shims:
|
|
@@ -133,7 +309,6 @@ function jestCompatAliases() {
|
|
|
133
309
|
"@testing-library/jest-native/extend-expect": "vitest-native/jest-compat/extend-expect-noop"
|
|
134
310
|
};
|
|
135
311
|
}
|
|
136
|
-
//#endregion
|
|
137
312
|
exports.jestCompatAliases = jestCompatAliases;
|
|
138
313
|
exports.jestCompatSetup = jestCompatSetup;
|
|
139
314
|
exports.jestMockTransform = jestMockTransform;
|
package/dist/jest-compat.d.cts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Plugin } from "vite";
|
|
2
|
-
//#region src/jest-compat/transform.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Vite plugin that adapts a Jest suite's `jest.mock(...)` calls for Vitest:
|
|
5
4
|
*
|
|
@@ -25,9 +24,10 @@ import { Plugin } from "vite";
|
|
|
25
24
|
* Opt-in: add after `reactNative()`; pair with `jestCompatSetup` + `globals: true`.
|
|
26
25
|
*/
|
|
27
26
|
export declare function jestMockTransform(): Plugin;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Setup-file specifier: gives the suite a `jest` global backed by Vitest's `vi`, with the
|
|
29
|
+
* synchronous `jest.requireActual` and `jest.requireMock`.
|
|
30
|
+
*/
|
|
31
31
|
export declare const jestCompatSetup = "vitest-native/jest-compat/setup";
|
|
32
32
|
/**
|
|
33
33
|
* `resolve.alias` entries that redirect Jest-only modules to Vitest-backed shims:
|
|
@@ -37,5 +37,4 @@ export declare const jestCompatSetup = "vitest-native/jest-compat/setup";
|
|
|
37
37
|
*
|
|
38
38
|
* Spread into your config's `resolve.alias`.
|
|
39
39
|
*/
|
|
40
|
-
export declare function jestCompatAliases(): Record<string, string>;
|
|
41
|
-
//#endregion
|
|
40
|
+
export declare function jestCompatAliases(): Record<string, string>;
|
package/dist/jest-compat.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Plugin } from "vite";
|
|
2
|
-
//#region src/jest-compat/transform.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Vite plugin that adapts a Jest suite's `jest.mock(...)` calls for Vitest:
|
|
5
4
|
*
|
|
@@ -25,9 +24,10 @@ import { Plugin } from "vite";
|
|
|
25
24
|
* Opt-in: add after `reactNative()`; pair with `jestCompatSetup` + `globals: true`.
|
|
26
25
|
*/
|
|
27
26
|
export declare function jestMockTransform(): Plugin;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Setup-file specifier: gives the suite a `jest` global backed by Vitest's `vi`, with the
|
|
29
|
+
* synchronous `jest.requireActual` and `jest.requireMock`.
|
|
30
|
+
*/
|
|
31
31
|
export declare const jestCompatSetup = "vitest-native/jest-compat/setup";
|
|
32
32
|
/**
|
|
33
33
|
* `resolve.alias` entries that redirect Jest-only modules to Vitest-backed shims:
|
|
@@ -37,5 +37,4 @@ export declare const jestCompatSetup = "vitest-native/jest-compat/setup";
|
|
|
37
37
|
*
|
|
38
38
|
* Spread into your config's `resolve.alias`.
|
|
39
39
|
*/
|
|
40
|
-
export declare function jestCompatAliases(): Record<string, string>;
|
|
41
|
-
//#endregion
|
|
40
|
+
export declare function jestCompatAliases(): Record<string, string>;
|