vitest-native 0.8.0 → 0.9.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/CHANGELOG.md +332 -0
- package/README.md +15 -1
- package/dist/cli.cjs +506 -0
- package/dist/cli.d.cts +4 -0
- package/dist/cli.d.mts +4 -0
- package/dist/cli.mjs +502 -0
- package/dist/ecosystem-BIWaVwYL.cjs +294 -0
- package/dist/ecosystem-EFtHnKS9.mjs +256 -0
- package/dist/{index-vlK4zxuK.d.mts → index-CEgRDdhd.d.cts} +13 -3
- package/dist/{index-YjjYELG7.d.cts → index-j-0tueBr.d.mts} +11 -5
- package/dist/index.cjs +345 -200
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +341 -196
- package/dist/jest-compat/setup.mjs +65 -1
- package/dist/native/boundary.mjs +108 -30
- package/dist/native/compile-cache.mjs +10 -5
- package/dist/native/explain.mjs +69 -0
- package/dist/native/hooks.mjs +76 -3
- package/dist/native/loader.mjs +35 -6
- package/dist/native/match.mjs +107 -8
- package/dist/native/registry.mjs +471 -0
- package/dist/native/reset.mjs +18 -12
- package/dist/native/runner.mjs +13 -7
- package/dist/native/setup.mjs +15 -1
- package/dist/native/transform.mjs +88 -19
- package/dist/native/worker.mjs +10 -1
- package/dist/pool-CJmjRgsB.mjs +117 -0
- package/dist/pool-DzsEo9vv.cjs +121 -0
- package/dist/{preset-map-BY9zk9Pj.cjs → preset-map-CG4FMtsl.cjs} +1 -0
- package/dist/{preset-map-DCWaM08s.mjs → preset-map-CNsZKfvu.mjs} +1 -0
- package/dist/{presets-7eMfp7o2.cjs → presets-C4faM3ds.cjs} +133 -2
- package/dist/{presets-23FJ76O4.mjs → presets-CuDrNA_W.mjs} +128 -3
- package/dist/presets.cjs +2 -1
- package/dist/presets.d.cts +2 -2
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/setup.cjs +318 -130
- package/dist/setup.mjs +318 -130
- package/docs/hot-default-design.md +129 -0
- package/docs/migrating-from-jest.md +43 -7
- package/docs/migrating-from-vitest-react-native.md +1 -1
- package/docs/release-readiness.md +2 -1
- package/package.json +17 -5
- package/dist/pool-B7LBfcMQ.mjs +0 -67
- package/dist/pool-p54QV1dQ.cjs +0 -69
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
const require_chunk = require("./chunk-CZWwpsFl.cjs");
|
|
2
|
+
const require_preset_map = require("./preset-map-CG4FMtsl.cjs");
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
node_path = require_chunk.__toESM(node_path, 1);
|
|
5
|
+
let node_fs = require("node:fs");
|
|
6
|
+
node_fs = require_chunk.__toESM(node_fs, 1);
|
|
7
|
+
let node_module = require("node:module");
|
|
8
|
+
//#region src/validate.ts
|
|
9
|
+
const KNOWN_OPTIONS = [
|
|
10
|
+
"platform",
|
|
11
|
+
"presets",
|
|
12
|
+
"mocks",
|
|
13
|
+
"diagnostics",
|
|
14
|
+
"assetExts",
|
|
15
|
+
"engine",
|
|
16
|
+
"transform",
|
|
17
|
+
"hotRuntime"
|
|
18
|
+
];
|
|
19
|
+
const KNOWN_HOT_RUNTIME_OPTIONS = [
|
|
20
|
+
"recycleAfterFiles",
|
|
21
|
+
"memoryLimit",
|
|
22
|
+
"preserveGlobals"
|
|
23
|
+
];
|
|
24
|
+
function assertStringArray(value, option) {
|
|
25
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry.length === 0)) throw new TypeError(`[vitest-native] "${option}" must be an array of non-empty strings.`);
|
|
26
|
+
}
|
|
27
|
+
function assertNonNegativeInteger(value, option) {
|
|
28
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) throw new TypeError(`[vitest-native] "${option}" must be a non-negative safe integer.`);
|
|
29
|
+
}
|
|
30
|
+
function validateOptions(options) {
|
|
31
|
+
if (options.engine !== void 0 && options.engine !== "auto" && options.engine !== "mock" && options.engine !== "native") throw new TypeError(`[vitest-native] "engine" must be "auto", "mock", or "native".`);
|
|
32
|
+
if (options.platform !== void 0 && options.platform !== "ios" && options.platform !== "android") throw new TypeError(`[vitest-native] "platform" must be "ios" or "android".`);
|
|
33
|
+
if (options.diagnostics !== void 0 && typeof options.diagnostics !== "boolean") throw new TypeError(`[vitest-native] "diagnostics" must be a boolean.`);
|
|
34
|
+
if (options.assetExts !== void 0) assertStringArray(options.assetExts, "assetExts");
|
|
35
|
+
if (options.transform !== void 0) assertStringArray(options.transform, "transform");
|
|
36
|
+
if (options.presets !== void 0 && !Array.isArray(options.presets)) throw new TypeError(`[vitest-native] "presets" must be an array.`);
|
|
37
|
+
if (options.mocks !== void 0 && (options.mocks === null || Array.isArray(options.mocks) || typeof options.mocks !== "object")) throw new TypeError(`[vitest-native] "mocks" must be a plain object.`);
|
|
38
|
+
const hotRuntime = options.hotRuntime;
|
|
39
|
+
if (hotRuntime === void 0 || typeof hotRuntime === "boolean") return;
|
|
40
|
+
if (hotRuntime === null || Array.isArray(hotRuntime) || typeof hotRuntime !== "object") throw new TypeError(`[vitest-native] "hotRuntime" must be a boolean or an options object.`);
|
|
41
|
+
const hotOptions = hotRuntime;
|
|
42
|
+
for (const key of Object.keys(hotOptions)) if (!KNOWN_HOT_RUNTIME_OPTIONS.includes(key)) throw new TypeError(`[vitest-native] Unknown hotRuntime option "${key}".`);
|
|
43
|
+
if (hotOptions.recycleAfterFiles !== void 0) assertNonNegativeInteger(hotOptions.recycleAfterFiles, "hotRuntime.recycleAfterFiles");
|
|
44
|
+
if (hotOptions.memoryLimit !== void 0) assertNonNegativeInteger(hotOptions.memoryLimit, "hotRuntime.memoryLimit");
|
|
45
|
+
if (hotOptions.preserveGlobals !== void 0) assertStringArray(hotOptions.preserveGlobals, "hotRuntime.preserveGlobals");
|
|
46
|
+
}
|
|
47
|
+
function satisfiesMinimum(version, minimum) {
|
|
48
|
+
const parse = (v) => v.replace(/^[^0-9]*/, "").split(/[-+]/)[0].split(".").map(Number);
|
|
49
|
+
const [aMaj, aMin = 0, aPat = 0] = parse(version);
|
|
50
|
+
const [bMaj, bMin = 0, bPat = 0] = parse(minimum);
|
|
51
|
+
if (aMaj !== bMaj) return aMaj > bMaj;
|
|
52
|
+
if (aMin !== bMin) return aMin > bMin;
|
|
53
|
+
return aPat >= bPat;
|
|
54
|
+
}
|
|
55
|
+
function validatePeerDependency(pkgName, minimumVersion, projectRoot, maximumMajorExclusive, minimumByMajor) {
|
|
56
|
+
const req = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
57
|
+
try {
|
|
58
|
+
const { version } = req(req.resolve(`${pkgName}/package.json`));
|
|
59
|
+
const major = Number(version.replace(/^[^0-9]*/, "").split(".")[0]);
|
|
60
|
+
const requiredMinimum = minimumByMajor?.[major] ?? minimumVersion;
|
|
61
|
+
if (!satisfiesMinimum(version, requiredMinimum)) return `vitest-native requires ${pkgName} >= ${requiredMinimum} for ${major}.x, but found ${version}. Please upgrade.`;
|
|
62
|
+
if (maximumMajorExclusive !== void 0 && major >= maximumMajorExclusive) return `vitest-native supports ${pkgName} >= ${minimumVersion} and < ${maximumMajorExclusive}, but found ${version}.`;
|
|
63
|
+
return null;
|
|
64
|
+
} catch {
|
|
65
|
+
return `vitest-native requires ${pkgName} >= ${minimumVersion}, but it was not found. Please install it.`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function warnUnknownOptions(options) {
|
|
69
|
+
for (const key of Object.keys(options)) if (!KNOWN_OPTIONS.includes(key)) {
|
|
70
|
+
const suggestion = findClosest(key, KNOWN_OPTIONS);
|
|
71
|
+
const hint = suggestion ? ` Did you mean '${suggestion}'?` : "";
|
|
72
|
+
console.warn(`[vitest-native] Unknown option '${key}'.${hint}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function findClosest(input, candidates) {
|
|
76
|
+
let best = null;
|
|
77
|
+
let bestDist = Infinity;
|
|
78
|
+
for (const c of candidates) {
|
|
79
|
+
const d = levenshtein(input.toLowerCase(), c.toLowerCase());
|
|
80
|
+
if (d < bestDist && d <= 3) {
|
|
81
|
+
bestDist = d;
|
|
82
|
+
best = c;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return best;
|
|
86
|
+
}
|
|
87
|
+
function levenshtein(a, b) {
|
|
88
|
+
const m = a.length;
|
|
89
|
+
const n = b.length;
|
|
90
|
+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
|
91
|
+
for (let i = 0; i <= m; i++) dp[i][0] = i;
|
|
92
|
+
for (let j = 0; j <= n; j++) dp[0][j] = j;
|
|
93
|
+
for (let i = 1; i <= m; i++) for (let j = 1; j <= n; j++) dp[i][j] = a[i - 1] === b[j - 1] ? dp[i - 1][j - 1] : 1 + Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]);
|
|
94
|
+
return dp[m][n];
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/peer-requirements.ts
|
|
98
|
+
const PEER_REQUIREMENTS = [
|
|
99
|
+
{
|
|
100
|
+
name: "vitest",
|
|
101
|
+
minimum: "4.0.0",
|
|
102
|
+
maximumMajor: 6
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: "vite",
|
|
106
|
+
minimum: "6.4.2",
|
|
107
|
+
maximumMajor: 9,
|
|
108
|
+
minimumByMajor: {
|
|
109
|
+
6: "6.4.2",
|
|
110
|
+
7: "7.3.2",
|
|
111
|
+
8: "8.0.5"
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
name: "react",
|
|
116
|
+
minimum: "18.0.0"
|
|
117
|
+
}
|
|
118
|
+
];
|
|
119
|
+
/** Can this project run the native engine? (Both transform deps resolvable.) */
|
|
120
|
+
function isNativeCapable(projectRoot) {
|
|
121
|
+
try {
|
|
122
|
+
const req = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
123
|
+
req.resolve("@react-native/babel-preset");
|
|
124
|
+
req.resolve("@babel/core");
|
|
125
|
+
return true;
|
|
126
|
+
} catch {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Resolve the concrete engine for a run. Pure; never throws. */
|
|
131
|
+
function detectEngine(requested, projectRoot, opts) {
|
|
132
|
+
const nativeAvailable = isNativeCapable(projectRoot);
|
|
133
|
+
if (requested === "native") return {
|
|
134
|
+
engine: "native",
|
|
135
|
+
nativeAvailable,
|
|
136
|
+
notice: null
|
|
137
|
+
};
|
|
138
|
+
if (requested === "mock") return {
|
|
139
|
+
engine: "mock",
|
|
140
|
+
nativeAvailable,
|
|
141
|
+
notice: null
|
|
142
|
+
};
|
|
143
|
+
const prefersNative = opts?.autoPrefersNative ?? true;
|
|
144
|
+
if (prefersNative && nativeAvailable) return {
|
|
145
|
+
engine: "native",
|
|
146
|
+
nativeAvailable,
|
|
147
|
+
notice: null
|
|
148
|
+
};
|
|
149
|
+
if (prefersNative) return {
|
|
150
|
+
engine: "mock",
|
|
151
|
+
nativeAvailable: false,
|
|
152
|
+
notice: "[vitest-native] @react-native/babel-preset not found — using the mock engine. Install it (and @babel/core) to run real React Native, or set engine:'mock' to silence this."
|
|
153
|
+
};
|
|
154
|
+
return {
|
|
155
|
+
engine: "mock",
|
|
156
|
+
nativeAvailable,
|
|
157
|
+
notice: null
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/native/ecosystem.ts
|
|
162
|
+
/**
|
|
163
|
+
* Packages that must never be auto-inlined even though they depend on React
|
|
164
|
+
* Native. The test library and the renderers underneath it are infrastructure the
|
|
165
|
+
* engine itself wires up; pulling a second copy of them into the module graph has
|
|
166
|
+
* corrupted rendering before, in ways that surface as unrelated act() and
|
|
167
|
+
* host-component failures.
|
|
168
|
+
*/
|
|
169
|
+
const NEVER_INLINE = new Set([
|
|
170
|
+
"@testing-library/react-native",
|
|
171
|
+
"react-test-renderer",
|
|
172
|
+
"test-renderer",
|
|
173
|
+
"react-native"
|
|
174
|
+
]);
|
|
175
|
+
/** Manifest fields that make a package part of the React Native ecosystem. */
|
|
176
|
+
function dependsOnReactNative(manifest) {
|
|
177
|
+
for (const field of ["dependencies", "peerDependencies"]) {
|
|
178
|
+
const deps = manifest[field];
|
|
179
|
+
if (deps && typeof deps === "object" && "react-native" in deps) return true;
|
|
180
|
+
}
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Every package manifest from the project up to the filesystem root.
|
|
185
|
+
*
|
|
186
|
+
* A workspace does not always declare its dependencies where the tests run: an app
|
|
187
|
+
* package may inherit React Native libraries from the repository root, and a Vitest
|
|
188
|
+
* `root` can point at a directory above or below the package that owns them. Reading
|
|
189
|
+
* only the project's own manifest silently found nothing in those layouts and sent
|
|
190
|
+
* the user back to maintaining `transform: [...]` by hand.
|
|
191
|
+
*
|
|
192
|
+
* Over-collecting is safe and under-collecting is not: a package that is listed but
|
|
193
|
+
* never imported costs nothing, because inlining only applies to files that are
|
|
194
|
+
* actually loaded.
|
|
195
|
+
*/
|
|
196
|
+
function manifestsFrom(projectRoot) {
|
|
197
|
+
const manifests = [];
|
|
198
|
+
let dir = node_path.default.resolve(projectRoot);
|
|
199
|
+
for (;;) {
|
|
200
|
+
try {
|
|
201
|
+
const raw = node_fs.default.readFileSync(node_path.default.join(dir, "package.json"), "utf8");
|
|
202
|
+
manifests.push(JSON.parse(raw));
|
|
203
|
+
} catch {}
|
|
204
|
+
const parent = node_path.default.dirname(dir);
|
|
205
|
+
if (parent === dir) return manifests;
|
|
206
|
+
dir = parent;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Packages in the project that declare React Native in their own manifest.
|
|
211
|
+
*
|
|
212
|
+
* These are the ones published the way the React Native ecosystem publishes:
|
|
213
|
+
* untranspiled JSX, Flow, or TypeScript, on the assumption that Metro will compile
|
|
214
|
+
* them. Node cannot run that source, which is why they previously had to be listed
|
|
215
|
+
* by hand in `transform: [...]` — a list every project had to discover the hard way,
|
|
216
|
+
* one `SyntaxError: Unexpected token '<'` at a time.
|
|
217
|
+
*
|
|
218
|
+
* A package's own manifest is the authority here rather than a heuristic on its
|
|
219
|
+
* name: `react-native-*` misses `@gorhom/bottom-sheet` and would wrongly claim
|
|
220
|
+
* `react-native` itself, while the manifest states the relationship exactly.
|
|
221
|
+
*
|
|
222
|
+
* Declared dependencies are read from the project and every manifest above it, so a
|
|
223
|
+
* workspace that declares its React Native libraries at the repository root is covered
|
|
224
|
+
* too. Resolving each name is cheap and package-manager-agnostic, where walking the
|
|
225
|
+
* installed tree depends on the store layout. Transitive dependencies that nothing
|
|
226
|
+
* declares stay available through `transform: [...]`.
|
|
227
|
+
*
|
|
228
|
+
* Excluded: packages a preset already shadows (their real source never loads), the
|
|
229
|
+
* test infrastructure in NEVER_INLINE, and anything the consumer listed explicitly
|
|
230
|
+
* in `transform` — that option keeps its existing meaning and takes precedence.
|
|
231
|
+
*/
|
|
232
|
+
function detectEcosystemPackages(projectRoot, explicit = []) {
|
|
233
|
+
const req = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
234
|
+
const skip = new Set([
|
|
235
|
+
...NEVER_INLINE,
|
|
236
|
+
...Object.keys(require_preset_map.AUTO_DETECT_PRESETS),
|
|
237
|
+
...explicit
|
|
238
|
+
]);
|
|
239
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
240
|
+
for (const manifest of manifestsFrom(projectRoot)) for (const field of [
|
|
241
|
+
"dependencies",
|
|
242
|
+
"devDependencies",
|
|
243
|
+
"optionalDependencies"
|
|
244
|
+
]) {
|
|
245
|
+
const deps = manifest[field];
|
|
246
|
+
if (deps && typeof deps === "object") for (const name of Object.keys(deps)) candidates.add(name);
|
|
247
|
+
}
|
|
248
|
+
if (candidates.size === 0) return [];
|
|
249
|
+
const detected = [];
|
|
250
|
+
for (const name of candidates) {
|
|
251
|
+
if (skip.has(name) || name.startsWith("@react-native/")) continue;
|
|
252
|
+
try {
|
|
253
|
+
if (dependsOnReactNative(req(`${name}/package.json`))) detected.push(name);
|
|
254
|
+
} catch {}
|
|
255
|
+
}
|
|
256
|
+
return detected.sort();
|
|
257
|
+
}
|
|
258
|
+
//#endregion
|
|
259
|
+
Object.defineProperty(exports, "PEER_REQUIREMENTS", {
|
|
260
|
+
enumerable: true,
|
|
261
|
+
get: function() {
|
|
262
|
+
return PEER_REQUIREMENTS;
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
Object.defineProperty(exports, "detectEcosystemPackages", {
|
|
266
|
+
enumerable: true,
|
|
267
|
+
get: function() {
|
|
268
|
+
return detectEcosystemPackages;
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
Object.defineProperty(exports, "detectEngine", {
|
|
272
|
+
enumerable: true,
|
|
273
|
+
get: function() {
|
|
274
|
+
return detectEngine;
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
Object.defineProperty(exports, "validateOptions", {
|
|
278
|
+
enumerable: true,
|
|
279
|
+
get: function() {
|
|
280
|
+
return validateOptions;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
Object.defineProperty(exports, "validatePeerDependency", {
|
|
284
|
+
enumerable: true,
|
|
285
|
+
get: function() {
|
|
286
|
+
return validatePeerDependency;
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
Object.defineProperty(exports, "warnUnknownOptions", {
|
|
290
|
+
enumerable: true,
|
|
291
|
+
get: function() {
|
|
292
|
+
return warnUnknownOptions;
|
|
293
|
+
}
|
|
294
|
+
});
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { t as AUTO_DETECT_PRESETS } from "./preset-map-CNsZKfvu.mjs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
//#region src/validate.ts
|
|
6
|
+
const KNOWN_OPTIONS = [
|
|
7
|
+
"platform",
|
|
8
|
+
"presets",
|
|
9
|
+
"mocks",
|
|
10
|
+
"diagnostics",
|
|
11
|
+
"assetExts",
|
|
12
|
+
"engine",
|
|
13
|
+
"transform",
|
|
14
|
+
"hotRuntime"
|
|
15
|
+
];
|
|
16
|
+
const KNOWN_HOT_RUNTIME_OPTIONS = [
|
|
17
|
+
"recycleAfterFiles",
|
|
18
|
+
"memoryLimit",
|
|
19
|
+
"preserveGlobals"
|
|
20
|
+
];
|
|
21
|
+
function assertStringArray(value, option) {
|
|
22
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry.length === 0)) throw new TypeError(`[vitest-native] "${option}" must be an array of non-empty strings.`);
|
|
23
|
+
}
|
|
24
|
+
function assertNonNegativeInteger(value, option) {
|
|
25
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) throw new TypeError(`[vitest-native] "${option}" must be a non-negative safe integer.`);
|
|
26
|
+
}
|
|
27
|
+
function validateOptions(options) {
|
|
28
|
+
if (options.engine !== void 0 && options.engine !== "auto" && options.engine !== "mock" && options.engine !== "native") throw new TypeError(`[vitest-native] "engine" must be "auto", "mock", or "native".`);
|
|
29
|
+
if (options.platform !== void 0 && options.platform !== "ios" && options.platform !== "android") throw new TypeError(`[vitest-native] "platform" must be "ios" or "android".`);
|
|
30
|
+
if (options.diagnostics !== void 0 && typeof options.diagnostics !== "boolean") throw new TypeError(`[vitest-native] "diagnostics" must be a boolean.`);
|
|
31
|
+
if (options.assetExts !== void 0) assertStringArray(options.assetExts, "assetExts");
|
|
32
|
+
if (options.transform !== void 0) assertStringArray(options.transform, "transform");
|
|
33
|
+
if (options.presets !== void 0 && !Array.isArray(options.presets)) throw new TypeError(`[vitest-native] "presets" must be an array.`);
|
|
34
|
+
if (options.mocks !== void 0 && (options.mocks === null || Array.isArray(options.mocks) || typeof options.mocks !== "object")) throw new TypeError(`[vitest-native] "mocks" must be a plain object.`);
|
|
35
|
+
const hotRuntime = options.hotRuntime;
|
|
36
|
+
if (hotRuntime === void 0 || typeof hotRuntime === "boolean") return;
|
|
37
|
+
if (hotRuntime === null || Array.isArray(hotRuntime) || typeof hotRuntime !== "object") throw new TypeError(`[vitest-native] "hotRuntime" must be a boolean or an options object.`);
|
|
38
|
+
const hotOptions = hotRuntime;
|
|
39
|
+
for (const key of Object.keys(hotOptions)) if (!KNOWN_HOT_RUNTIME_OPTIONS.includes(key)) throw new TypeError(`[vitest-native] Unknown hotRuntime option "${key}".`);
|
|
40
|
+
if (hotOptions.recycleAfterFiles !== void 0) assertNonNegativeInteger(hotOptions.recycleAfterFiles, "hotRuntime.recycleAfterFiles");
|
|
41
|
+
if (hotOptions.memoryLimit !== void 0) assertNonNegativeInteger(hotOptions.memoryLimit, "hotRuntime.memoryLimit");
|
|
42
|
+
if (hotOptions.preserveGlobals !== void 0) assertStringArray(hotOptions.preserveGlobals, "hotRuntime.preserveGlobals");
|
|
43
|
+
}
|
|
44
|
+
function satisfiesMinimum(version, minimum) {
|
|
45
|
+
const parse = (v) => v.replace(/^[^0-9]*/, "").split(/[-+]/)[0].split(".").map(Number);
|
|
46
|
+
const [aMaj, aMin = 0, aPat = 0] = parse(version);
|
|
47
|
+
const [bMaj, bMin = 0, bPat = 0] = parse(minimum);
|
|
48
|
+
if (aMaj !== bMaj) return aMaj > bMaj;
|
|
49
|
+
if (aMin !== bMin) return aMin > bMin;
|
|
50
|
+
return aPat >= bPat;
|
|
51
|
+
}
|
|
52
|
+
function validatePeerDependency(pkgName, minimumVersion, projectRoot, maximumMajorExclusive, minimumByMajor) {
|
|
53
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
54
|
+
try {
|
|
55
|
+
const { version } = req(req.resolve(`${pkgName}/package.json`));
|
|
56
|
+
const major = Number(version.replace(/^[^0-9]*/, "").split(".")[0]);
|
|
57
|
+
const requiredMinimum = minimumByMajor?.[major] ?? minimumVersion;
|
|
58
|
+
if (!satisfiesMinimum(version, requiredMinimum)) return `vitest-native requires ${pkgName} >= ${requiredMinimum} for ${major}.x, but found ${version}. Please upgrade.`;
|
|
59
|
+
if (maximumMajorExclusive !== void 0 && major >= maximumMajorExclusive) return `vitest-native supports ${pkgName} >= ${minimumVersion} and < ${maximumMajorExclusive}, but found ${version}.`;
|
|
60
|
+
return null;
|
|
61
|
+
} catch {
|
|
62
|
+
return `vitest-native requires ${pkgName} >= ${minimumVersion}, but it was not found. Please install it.`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function warnUnknownOptions(options) {
|
|
66
|
+
for (const key of Object.keys(options)) if (!KNOWN_OPTIONS.includes(key)) {
|
|
67
|
+
const suggestion = findClosest(key, KNOWN_OPTIONS);
|
|
68
|
+
const hint = suggestion ? ` Did you mean '${suggestion}'?` : "";
|
|
69
|
+
console.warn(`[vitest-native] Unknown option '${key}'.${hint}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function findClosest(input, candidates) {
|
|
73
|
+
let best = null;
|
|
74
|
+
let bestDist = Infinity;
|
|
75
|
+
for (const c of candidates) {
|
|
76
|
+
const d = levenshtein(input.toLowerCase(), c.toLowerCase());
|
|
77
|
+
if (d < bestDist && d <= 3) {
|
|
78
|
+
bestDist = d;
|
|
79
|
+
best = c;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return best;
|
|
83
|
+
}
|
|
84
|
+
function levenshtein(a, b) {
|
|
85
|
+
const m = a.length;
|
|
86
|
+
const n = b.length;
|
|
87
|
+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
|
88
|
+
for (let i = 0; i <= m; i++) dp[i][0] = i;
|
|
89
|
+
for (let j = 0; j <= n; j++) dp[0][j] = j;
|
|
90
|
+
for (let i = 1; i <= m; i++) for (let j = 1; j <= n; j++) dp[i][j] = a[i - 1] === b[j - 1] ? dp[i - 1][j - 1] : 1 + Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]);
|
|
91
|
+
return dp[m][n];
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/peer-requirements.ts
|
|
95
|
+
const PEER_REQUIREMENTS = [
|
|
96
|
+
{
|
|
97
|
+
name: "vitest",
|
|
98
|
+
minimum: "4.0.0",
|
|
99
|
+
maximumMajor: 6
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: "vite",
|
|
103
|
+
minimum: "6.4.2",
|
|
104
|
+
maximumMajor: 9,
|
|
105
|
+
minimumByMajor: {
|
|
106
|
+
6: "6.4.2",
|
|
107
|
+
7: "7.3.2",
|
|
108
|
+
8: "8.0.5"
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: "react",
|
|
113
|
+
minimum: "18.0.0"
|
|
114
|
+
}
|
|
115
|
+
];
|
|
116
|
+
/** Can this project run the native engine? (Both transform deps resolvable.) */
|
|
117
|
+
function isNativeCapable(projectRoot) {
|
|
118
|
+
try {
|
|
119
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
120
|
+
req.resolve("@react-native/babel-preset");
|
|
121
|
+
req.resolve("@babel/core");
|
|
122
|
+
return true;
|
|
123
|
+
} catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Resolve the concrete engine for a run. Pure; never throws. */
|
|
128
|
+
function detectEngine(requested, projectRoot, opts) {
|
|
129
|
+
const nativeAvailable = isNativeCapable(projectRoot);
|
|
130
|
+
if (requested === "native") return {
|
|
131
|
+
engine: "native",
|
|
132
|
+
nativeAvailable,
|
|
133
|
+
notice: null
|
|
134
|
+
};
|
|
135
|
+
if (requested === "mock") return {
|
|
136
|
+
engine: "mock",
|
|
137
|
+
nativeAvailable,
|
|
138
|
+
notice: null
|
|
139
|
+
};
|
|
140
|
+
const prefersNative = opts?.autoPrefersNative ?? true;
|
|
141
|
+
if (prefersNative && nativeAvailable) return {
|
|
142
|
+
engine: "native",
|
|
143
|
+
nativeAvailable,
|
|
144
|
+
notice: null
|
|
145
|
+
};
|
|
146
|
+
if (prefersNative) return {
|
|
147
|
+
engine: "mock",
|
|
148
|
+
nativeAvailable: false,
|
|
149
|
+
notice: "[vitest-native] @react-native/babel-preset not found — using the mock engine. Install it (and @babel/core) to run real React Native, or set engine:'mock' to silence this."
|
|
150
|
+
};
|
|
151
|
+
return {
|
|
152
|
+
engine: "mock",
|
|
153
|
+
nativeAvailable,
|
|
154
|
+
notice: null
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region src/native/ecosystem.ts
|
|
159
|
+
/**
|
|
160
|
+
* Packages that must never be auto-inlined even though they depend on React
|
|
161
|
+
* Native. The test library and the renderers underneath it are infrastructure the
|
|
162
|
+
* engine itself wires up; pulling a second copy of them into the module graph has
|
|
163
|
+
* corrupted rendering before, in ways that surface as unrelated act() and
|
|
164
|
+
* host-component failures.
|
|
165
|
+
*/
|
|
166
|
+
const NEVER_INLINE = new Set([
|
|
167
|
+
"@testing-library/react-native",
|
|
168
|
+
"react-test-renderer",
|
|
169
|
+
"test-renderer",
|
|
170
|
+
"react-native"
|
|
171
|
+
]);
|
|
172
|
+
/** Manifest fields that make a package part of the React Native ecosystem. */
|
|
173
|
+
function dependsOnReactNative(manifest) {
|
|
174
|
+
for (const field of ["dependencies", "peerDependencies"]) {
|
|
175
|
+
const deps = manifest[field];
|
|
176
|
+
if (deps && typeof deps === "object" && "react-native" in deps) return true;
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Every package manifest from the project up to the filesystem root.
|
|
182
|
+
*
|
|
183
|
+
* A workspace does not always declare its dependencies where the tests run: an app
|
|
184
|
+
* package may inherit React Native libraries from the repository root, and a Vitest
|
|
185
|
+
* `root` can point at a directory above or below the package that owns them. Reading
|
|
186
|
+
* only the project's own manifest silently found nothing in those layouts and sent
|
|
187
|
+
* the user back to maintaining `transform: [...]` by hand.
|
|
188
|
+
*
|
|
189
|
+
* Over-collecting is safe and under-collecting is not: a package that is listed but
|
|
190
|
+
* never imported costs nothing, because inlining only applies to files that are
|
|
191
|
+
* actually loaded.
|
|
192
|
+
*/
|
|
193
|
+
function manifestsFrom(projectRoot) {
|
|
194
|
+
const manifests = [];
|
|
195
|
+
let dir = path.resolve(projectRoot);
|
|
196
|
+
for (;;) {
|
|
197
|
+
try {
|
|
198
|
+
const raw = fs.readFileSync(path.join(dir, "package.json"), "utf8");
|
|
199
|
+
manifests.push(JSON.parse(raw));
|
|
200
|
+
} catch {}
|
|
201
|
+
const parent = path.dirname(dir);
|
|
202
|
+
if (parent === dir) return manifests;
|
|
203
|
+
dir = parent;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Packages in the project that declare React Native in their own manifest.
|
|
208
|
+
*
|
|
209
|
+
* These are the ones published the way the React Native ecosystem publishes:
|
|
210
|
+
* untranspiled JSX, Flow, or TypeScript, on the assumption that Metro will compile
|
|
211
|
+
* them. Node cannot run that source, which is why they previously had to be listed
|
|
212
|
+
* by hand in `transform: [...]` — a list every project had to discover the hard way,
|
|
213
|
+
* one `SyntaxError: Unexpected token '<'` at a time.
|
|
214
|
+
*
|
|
215
|
+
* A package's own manifest is the authority here rather than a heuristic on its
|
|
216
|
+
* name: `react-native-*` misses `@gorhom/bottom-sheet` and would wrongly claim
|
|
217
|
+
* `react-native` itself, while the manifest states the relationship exactly.
|
|
218
|
+
*
|
|
219
|
+
* Declared dependencies are read from the project and every manifest above it, so a
|
|
220
|
+
* workspace that declares its React Native libraries at the repository root is covered
|
|
221
|
+
* too. Resolving each name is cheap and package-manager-agnostic, where walking the
|
|
222
|
+
* installed tree depends on the store layout. Transitive dependencies that nothing
|
|
223
|
+
* declares stay available through `transform: [...]`.
|
|
224
|
+
*
|
|
225
|
+
* Excluded: packages a preset already shadows (their real source never loads), the
|
|
226
|
+
* test infrastructure in NEVER_INLINE, and anything the consumer listed explicitly
|
|
227
|
+
* in `transform` — that option keeps its existing meaning and takes precedence.
|
|
228
|
+
*/
|
|
229
|
+
function detectEcosystemPackages(projectRoot, explicit = []) {
|
|
230
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
231
|
+
const skip = new Set([
|
|
232
|
+
...NEVER_INLINE,
|
|
233
|
+
...Object.keys(AUTO_DETECT_PRESETS),
|
|
234
|
+
...explicit
|
|
235
|
+
]);
|
|
236
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
237
|
+
for (const manifest of manifestsFrom(projectRoot)) for (const field of [
|
|
238
|
+
"dependencies",
|
|
239
|
+
"devDependencies",
|
|
240
|
+
"optionalDependencies"
|
|
241
|
+
]) {
|
|
242
|
+
const deps = manifest[field];
|
|
243
|
+
if (deps && typeof deps === "object") for (const name of Object.keys(deps)) candidates.add(name);
|
|
244
|
+
}
|
|
245
|
+
if (candidates.size === 0) return [];
|
|
246
|
+
const detected = [];
|
|
247
|
+
for (const name of candidates) {
|
|
248
|
+
if (skip.has(name) || name.startsWith("@react-native/")) continue;
|
|
249
|
+
try {
|
|
250
|
+
if (dependsOnReactNative(req(`${name}/package.json`))) detected.push(name);
|
|
251
|
+
} catch {}
|
|
252
|
+
}
|
|
253
|
+
return detected.sort();
|
|
254
|
+
}
|
|
255
|
+
//#endregion
|
|
256
|
+
export { validatePeerDependency as a, validateOptions as i, detectEngine as n, warnUnknownOptions as o, PEER_REQUIREMENTS as r, detectEcosystemPackages as t };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Mock } from "vitest";
|
|
2
2
|
|
|
3
|
+
//#region \0rolldown/runtime.js
|
|
4
|
+
//#endregion
|
|
3
5
|
//#region src/types.d.ts
|
|
4
6
|
/** Static methods available on the Image component mock. */
|
|
5
7
|
interface ImageStatic {
|
|
@@ -301,7 +303,12 @@ interface VitestNativeOptions {
|
|
|
301
303
|
* is enforced between Vitest scheduler tasks; a single task containing
|
|
302
304
|
* multiple files cannot be interrupted.
|
|
303
305
|
* - `memoryLimit`: retire a worker when its reported JS heap (bytes) meets
|
|
304
|
-
* or exceeds this value, also between scheduler tasks.
|
|
306
|
+
* or exceeds this value, also between scheduler tasks. When hot is enabled
|
|
307
|
+
* and you set neither `memoryLimit` nor `recycleAfterFiles`, a default
|
|
308
|
+
* per-worker bound of `clamp(totalmem * 0.25, 768MB, 1.5GB)` is applied so
|
|
309
|
+
* memory stays bounded out of the box (multi-worker runs recycle a worker
|
|
310
|
+
* once it crosses the ceiling). Single-worker hot can't recycle, so the
|
|
311
|
+
* bound is inert there — run with `maxWorkers >= 2` for bounded hot memory.
|
|
305
312
|
* - `preserveGlobals`: exact globalThis property names intentionally owned
|
|
306
313
|
* by resident external libraries. App/test globals are otherwise removed
|
|
307
314
|
* between files. Storybook's preview registry is preserved automatically.
|
|
@@ -359,8 +366,11 @@ declare function bottomSheet(): Preset;
|
|
|
359
366
|
//#endregion
|
|
360
367
|
//#region src/presets/keyboard-controller.d.ts
|
|
361
368
|
declare function keyboardController(): Preset;
|
|
369
|
+
//#endregion
|
|
370
|
+
//#region src/presets/worklets.d.ts
|
|
371
|
+
declare function worklets(): Preset;
|
|
362
372
|
declare namespace index_d_exports {
|
|
363
|
-
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview };
|
|
373
|
+
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
|
364
374
|
}
|
|
365
375
|
//#endregion
|
|
366
|
-
export {
|
|
376
|
+
export { reanimated as _, flashList as a, VitestNativeOptions as b, svg as c, expo as d, screens as f, gestureHandler as g, safeAreaContext as h, bottomSheet as i, mmkv as l, navigation as m, worklets as n, vectorIcons as o, asyncStorage as p, keyboardController as r, webview as s, index_d_exports as t, deviceInfo as u, Preset as v, ReactNativeMock as y };
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Mock } from "vitest";
|
|
2
2
|
|
|
3
|
-
//#region \0rolldown/runtime.js
|
|
4
|
-
//#endregion
|
|
5
3
|
//#region src/types.d.ts
|
|
6
4
|
/** Static methods available on the Image component mock. */
|
|
7
5
|
interface ImageStatic {
|
|
@@ -303,7 +301,12 @@ interface VitestNativeOptions {
|
|
|
303
301
|
* is enforced between Vitest scheduler tasks; a single task containing
|
|
304
302
|
* multiple files cannot be interrupted.
|
|
305
303
|
* - `memoryLimit`: retire a worker when its reported JS heap (bytes) meets
|
|
306
|
-
* or exceeds this value, also between scheduler tasks.
|
|
304
|
+
* or exceeds this value, also between scheduler tasks. When hot is enabled
|
|
305
|
+
* and you set neither `memoryLimit` nor `recycleAfterFiles`, a default
|
|
306
|
+
* per-worker bound of `clamp(totalmem * 0.25, 768MB, 1.5GB)` is applied so
|
|
307
|
+
* memory stays bounded out of the box (multi-worker runs recycle a worker
|
|
308
|
+
* once it crosses the ceiling). Single-worker hot can't recycle, so the
|
|
309
|
+
* bound is inert there — run with `maxWorkers >= 2` for bounded hot memory.
|
|
307
310
|
* - `preserveGlobals`: exact globalThis property names intentionally owned
|
|
308
311
|
* by resident external libraries. App/test globals are otherwise removed
|
|
309
312
|
* between files. Storybook's preview registry is preserved automatically.
|
|
@@ -361,8 +364,11 @@ declare function bottomSheet(): Preset;
|
|
|
361
364
|
//#endregion
|
|
362
365
|
//#region src/presets/keyboard-controller.d.ts
|
|
363
366
|
declare function keyboardController(): Preset;
|
|
367
|
+
//#endregion
|
|
368
|
+
//#region src/presets/worklets.d.ts
|
|
369
|
+
declare function worklets(): Preset;
|
|
364
370
|
declare namespace index_d_exports {
|
|
365
|
-
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview };
|
|
371
|
+
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
|
366
372
|
}
|
|
367
373
|
//#endregion
|
|
368
|
-
export {
|
|
374
|
+
export { reanimated as _, flashList as a, VitestNativeOptions as b, svg as c, expo as d, screens as f, gestureHandler as g, safeAreaContext as h, bottomSheet as i, mmkv as l, navigation as m, worklets as n, vectorIcons as o, asyncStorage as p, keyboardController as r, webview as s, index_d_exports as t, deviceInfo as u, Preset as v, ReactNativeMock as y };
|