vitest-native 0.8.0 → 0.10.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 +1063 -0
- package/README.md +47 -2
- package/dist/cli.cjs +642 -0
- package/dist/cli.d.cts +4 -0
- package/dist/cli.d.mts +4 -0
- package/dist/cli.mjs +638 -0
- package/dist/ecosystem-CO5vqExO.mjs +318 -0
- package/dist/ecosystem-IuZ-Gv0R.cjs +356 -0
- package/dist/errors-Dm5yLNTF.mjs +64 -0
- package/dist/errors-flXDIeWk.cjs +75 -0
- package/dist/errors.d.mts +46 -0
- package/dist/errors.mjs +78 -0
- package/dist/helpers.cjs +6 -23
- package/dist/helpers.d.cts +0 -6
- package/dist/helpers.d.mts +0 -6
- package/dist/helpers.mjs +6 -23
- package/dist/{index-vlK4zxuK.d.mts → index-BJL4cQ-l.d.cts} +22 -7
- package/dist/{index-YjjYELG7.d.cts → index-CvCU0cLl.d.mts} +20 -9
- package/dist/index.d.mts +8 -6
- package/dist/index.mjs +563 -232
- package/dist/jest-compat/interop.mjs +15 -0
- package/dist/jest-compat/setup.mjs +164 -5
- package/dist/matchers.cjs +16 -7
- package/dist/matchers.mjs +16 -7
- 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 +151 -4
- package/dist/native/loader.mjs +61 -12
- package/dist/native/match.mjs +107 -8
- package/dist/native/module-reset.mjs +63 -0
- package/dist/native/registry.mjs +540 -0
- package/dist/native/reset.mjs +18 -12
- package/dist/native/resolve.d.mts +22 -0
- package/dist/native/resolve.mjs +21 -13
- package/dist/native/runner.mjs +34 -7
- package/dist/native/setup.mjs +19 -3
- package/dist/native/transform.mjs +195 -21
- package/dist/native/worker.mjs +69 -17
- package/dist/pool-Bcxp4Qrg.mjs +118 -0
- package/dist/pool-CFND7plG.cjs +122 -0
- package/dist/{preset-map-DCWaM08s.mjs → preset-map-BpDG_D5m.mjs} +1 -1
- package/dist/{preset-map-BY9zk9Pj.cjs → preset-map-DxilQ4pI.cjs} +1 -1
- package/dist/{presets-7eMfp7o2.cjs → presets-BGTqVH6C.cjs} +227 -52
- package/dist/{presets-23FJ76O4.mjs → presets-CK85x8ey.mjs} +222 -53
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/rntl-matchers.d.ts +35 -0
- package/dist/serializer.cjs +33 -3
- package/dist/serializer.mjs +33 -3
- package/dist/setup.mjs +544 -193
- 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 +24 -6
- package/docs/versioning.md +133 -0
- package/package.json +40 -38
- package/dist/index.cjs +0 -643
- package/dist/index.d.cts +0 -14
- package/dist/pool-B7LBfcMQ.mjs +0 -67
- package/dist/pool-p54QV1dQ.cjs +0 -69
- package/dist/presets.cjs +0 -17
- package/dist/presets.d.cts +0 -2
- package/dist/setup.cjs +0 -2514
- package/dist/setup.d.cts +0 -1
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { n as VitestNativeTypeError } from "./errors-Dm5yLNTF.mjs";
|
|
2
|
+
import { t as AUTO_DETECT_PRESETS } from "./preset-map-BpDG_D5m.mjs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
//#region src/validate.ts
|
|
7
|
+
const KNOWN_OPTIONS = [
|
|
8
|
+
"platform",
|
|
9
|
+
"presets",
|
|
10
|
+
"mocks",
|
|
11
|
+
"diagnostics",
|
|
12
|
+
"assetExts",
|
|
13
|
+
"engine",
|
|
14
|
+
"transform",
|
|
15
|
+
"hotRuntime"
|
|
16
|
+
];
|
|
17
|
+
const KNOWN_HOT_RUNTIME_OPTIONS = [
|
|
18
|
+
"recycleAfterFiles",
|
|
19
|
+
"memoryLimit",
|
|
20
|
+
"preserveGlobals"
|
|
21
|
+
];
|
|
22
|
+
function assertStringArray(value, option) {
|
|
23
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry.length === 0)) throw new VitestNativeTypeError("INVALID_OPTION", `"${option}" must be an array of non-empty strings.`);
|
|
24
|
+
}
|
|
25
|
+
function assertNonNegativeInteger(value, option) {
|
|
26
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) throw new VitestNativeTypeError("INVALID_OPTION", `"${option}" must be a non-negative safe integer.`);
|
|
27
|
+
}
|
|
28
|
+
function validateOptions(options) {
|
|
29
|
+
if (options.engine !== void 0 && options.engine !== "auto" && options.engine !== "mock" && options.engine !== "native") throw new VitestNativeTypeError("INVALID_OPTION", `"engine" must be "auto", "mock", or "native".`);
|
|
30
|
+
if (options.platform !== void 0 && options.platform !== "ios" && options.platform !== "android") throw new VitestNativeTypeError("INVALID_OPTION", `"platform" must be "ios" or "android".`);
|
|
31
|
+
if (options.diagnostics !== void 0 && typeof options.diagnostics !== "boolean") throw new VitestNativeTypeError("INVALID_OPTION", `"diagnostics" must be a boolean.`);
|
|
32
|
+
if (options.assetExts !== void 0) assertStringArray(options.assetExts, "assetExts");
|
|
33
|
+
if (options.transform !== void 0) assertStringArray(options.transform, "transform");
|
|
34
|
+
if (options.presets !== void 0 && !Array.isArray(options.presets)) {
|
|
35
|
+
if (!(options.presets !== null && typeof options.presets === "object" && Object.values(options.presets).every((v) => typeof v === "boolean"))) throw new VitestNativeTypeError("INVALID_OPTION", "\"presets\" must be an array of presets, or an object of booleans to switch auto-detected presets off (for example { navigation: false }).");
|
|
36
|
+
}
|
|
37
|
+
if (options.mocks !== void 0 && (options.mocks === null || Array.isArray(options.mocks) || typeof options.mocks !== "object")) throw new VitestNativeTypeError("INVALID_OPTION", `"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 VitestNativeTypeError("INVALID_OPTION", `"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)) {
|
|
43
|
+
const suggestion = findClosest(key, KNOWN_HOT_RUNTIME_OPTIONS);
|
|
44
|
+
throw new VitestNativeTypeError("UNKNOWN_OPTION", `Unknown hotRuntime option "${key}".` + (suggestion ? ` Did you mean '${suggestion}'?` : "") + ` Valid options: ${KNOWN_HOT_RUNTIME_OPTIONS.join(", ")}.`);
|
|
45
|
+
}
|
|
46
|
+
if (hotOptions.recycleAfterFiles !== void 0) assertNonNegativeInteger(hotOptions.recycleAfterFiles, "hotRuntime.recycleAfterFiles");
|
|
47
|
+
if (hotOptions.memoryLimit !== void 0) assertNonNegativeInteger(hotOptions.memoryLimit, "hotRuntime.memoryLimit");
|
|
48
|
+
if (hotOptions.preserveGlobals !== void 0) assertStringArray(hotOptions.preserveGlobals, "hotRuntime.preserveGlobals");
|
|
49
|
+
}
|
|
50
|
+
function satisfiesMinimum(version, minimum) {
|
|
51
|
+
const parse = (v) => v.replace(/^[^0-9]*/, "").split(/[-+]/)[0].split(".").map(Number);
|
|
52
|
+
const [aMaj, aMin = 0, aPat = 0] = parse(version);
|
|
53
|
+
const [bMaj, bMin = 0, bPat = 0] = parse(minimum);
|
|
54
|
+
if (aMaj !== bMaj) return aMaj > bMaj;
|
|
55
|
+
if (aMin !== bMin) return aMin > bMin;
|
|
56
|
+
return aPat >= bPat;
|
|
57
|
+
}
|
|
58
|
+
function validatePeerDependency(pkgName, minimumVersion, projectRoot, maximumMajorExclusive, minimumByMajor) {
|
|
59
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
60
|
+
try {
|
|
61
|
+
const { version } = req(req.resolve(`${pkgName}/package.json`));
|
|
62
|
+
const major = Number(version.replace(/^[^0-9]*/, "").split(".")[0]);
|
|
63
|
+
const requiredMinimum = minimumByMajor?.[major] ?? minimumVersion;
|
|
64
|
+
if (!satisfiesMinimum(version, requiredMinimum)) return `vitest-native requires ${pkgName} >= ${requiredMinimum} for ${major}.x, but found ${version}. Please upgrade.`;
|
|
65
|
+
if (maximumMajorExclusive !== void 0 && major >= maximumMajorExclusive) return `vitest-native supports ${pkgName} >= ${minimumVersion} and < ${maximumMajorExclusive}, but found ${version}.`;
|
|
66
|
+
return null;
|
|
67
|
+
} catch {
|
|
68
|
+
return `vitest-native requires ${pkgName} >= ${minimumVersion}, but it was not found. Please install it.`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function warnUnknownOptions(options) {
|
|
72
|
+
for (const key of Object.keys(options)) if (!KNOWN_OPTIONS.includes(key)) {
|
|
73
|
+
const suggestion = findClosest(key, KNOWN_OPTIONS);
|
|
74
|
+
const hint = suggestion ? ` Did you mean '${suggestion}'?` : "";
|
|
75
|
+
console.warn(`[vitest-native] Unknown option '${key}'.${hint}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function findClosest(input, candidates) {
|
|
79
|
+
let best = null;
|
|
80
|
+
let bestDist = Infinity;
|
|
81
|
+
for (const c of candidates) {
|
|
82
|
+
const d = levenshtein(input.toLowerCase(), c.toLowerCase());
|
|
83
|
+
if (d < bestDist && d <= 3) {
|
|
84
|
+
bestDist = d;
|
|
85
|
+
best = c;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return best;
|
|
89
|
+
}
|
|
90
|
+
function levenshtein(a, b) {
|
|
91
|
+
const m = a.length;
|
|
92
|
+
const n = b.length;
|
|
93
|
+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
|
94
|
+
for (let i = 0; i <= m; i++) dp[i][0] = i;
|
|
95
|
+
for (let j = 0; j <= n; j++) dp[0][j] = j;
|
|
96
|
+
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]);
|
|
97
|
+
return dp[m][n];
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/peer-requirements.ts
|
|
101
|
+
const PEER_REQUIREMENTS = [
|
|
102
|
+
{
|
|
103
|
+
name: "vitest",
|
|
104
|
+
minimum: "4.0.0",
|
|
105
|
+
maximumMajor: 6
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: "vite",
|
|
109
|
+
minimum: "6.4.2",
|
|
110
|
+
maximumMajor: 9,
|
|
111
|
+
minimumByMajor: {
|
|
112
|
+
6: "6.4.2",
|
|
113
|
+
7: "7.3.2",
|
|
114
|
+
8: "8.0.5"
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: "react",
|
|
119
|
+
minimum: "18.0.0"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
name: "@testing-library/react-native",
|
|
123
|
+
minimum: "12.0.0",
|
|
124
|
+
maximumMajor: 15,
|
|
125
|
+
optional: true
|
|
126
|
+
}
|
|
127
|
+
];
|
|
128
|
+
/** Can this project run the native engine? (Both transform deps resolvable.) */
|
|
129
|
+
function isNativeCapable(projectRoot) {
|
|
130
|
+
try {
|
|
131
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
132
|
+
req.resolve("@react-native/babel-preset");
|
|
133
|
+
req.resolve("@babel/core");
|
|
134
|
+
return true;
|
|
135
|
+
} catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** Resolve the concrete engine for a run. Pure; never throws. */
|
|
140
|
+
function detectEngine(requested, projectRoot, opts) {
|
|
141
|
+
const nativeAvailable = isNativeCapable(projectRoot);
|
|
142
|
+
if (requested === "native") return {
|
|
143
|
+
engine: "native",
|
|
144
|
+
nativeAvailable,
|
|
145
|
+
notice: null
|
|
146
|
+
};
|
|
147
|
+
if (requested === "mock") return {
|
|
148
|
+
engine: "mock",
|
|
149
|
+
nativeAvailable,
|
|
150
|
+
notice: null
|
|
151
|
+
};
|
|
152
|
+
const prefersNative = opts?.autoPrefersNative ?? true;
|
|
153
|
+
if (prefersNative && nativeAvailable) return {
|
|
154
|
+
engine: "native",
|
|
155
|
+
nativeAvailable,
|
|
156
|
+
notice: null
|
|
157
|
+
};
|
|
158
|
+
if (prefersNative) return {
|
|
159
|
+
engine: "mock",
|
|
160
|
+
nativeAvailable: false,
|
|
161
|
+
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."
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
engine: "mock",
|
|
165
|
+
nativeAvailable,
|
|
166
|
+
notice: null
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/native/ecosystem.ts
|
|
171
|
+
/**
|
|
172
|
+
* Packages that must never be auto-inlined even though they depend on React
|
|
173
|
+
* Native. The test library and the renderers underneath it are infrastructure the
|
|
174
|
+
* engine itself wires up; pulling a second copy of them into the module graph has
|
|
175
|
+
* corrupted rendering before, in ways that surface as unrelated act() and
|
|
176
|
+
* host-component failures.
|
|
177
|
+
*/
|
|
178
|
+
const NEVER_INLINE = new Set([
|
|
179
|
+
"@testing-library/react-native",
|
|
180
|
+
"react-test-renderer",
|
|
181
|
+
"test-renderer",
|
|
182
|
+
"react-native"
|
|
183
|
+
]);
|
|
184
|
+
/** Manifest fields that make a package part of the React Native ecosystem. */
|
|
185
|
+
function dependsOnReactNative(manifest) {
|
|
186
|
+
for (const field of ["dependencies", "peerDependencies"]) {
|
|
187
|
+
const deps = manifest[field];
|
|
188
|
+
if (deps && typeof deps === "object" && "react-native" in deps) return true;
|
|
189
|
+
}
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Every package manifest from the project up to the filesystem root.
|
|
194
|
+
*
|
|
195
|
+
* A workspace does not always declare its dependencies where the tests run: an app
|
|
196
|
+
* package may inherit React Native libraries from the repository root, and a Vitest
|
|
197
|
+
* `root` can point at a directory above or below the package that owns them. Reading
|
|
198
|
+
* only the project's own manifest silently found nothing in those layouts and sent
|
|
199
|
+
* the user back to maintaining `transform: [...]` by hand.
|
|
200
|
+
*
|
|
201
|
+
* Over-collecting is safe and under-collecting is not: a package that is listed but
|
|
202
|
+
* never imported costs nothing, because inlining only applies to files that are
|
|
203
|
+
* actually loaded.
|
|
204
|
+
*/
|
|
205
|
+
/**
|
|
206
|
+
* Directories of the workspace members a manifest declares, plus any listed in a
|
|
207
|
+
* sibling pnpm-workspace.yaml.
|
|
208
|
+
*
|
|
209
|
+
* Needed because `manifestsFrom` only walks UP. In a workspace the run root is
|
|
210
|
+
* frequently ABOVE the package under test — Nx invokes tasks from the workspace
|
|
211
|
+
* root, and Vitest's root defaults to the working directory — so the app's own
|
|
212
|
+
* dependencies are declared in a manifest that walking up never reaches. The
|
|
213
|
+
* package then misses auto-detection and stays in Vite's graph while Node loads it
|
|
214
|
+
* too, which is the dual-ownership failure reproducing from nothing but a different
|
|
215
|
+
* working directory.
|
|
216
|
+
*
|
|
217
|
+
* Only the two glob shapes workspace fields actually use are expanded (`dir/*` and a
|
|
218
|
+
* literal path). Over-collecting is safe here — an unimported package costs nothing —
|
|
219
|
+
* so a shape that is not understood is skipped rather than approximated.
|
|
220
|
+
*/
|
|
221
|
+
function workspaceMemberDirs(dir, manifest) {
|
|
222
|
+
const patterns = [];
|
|
223
|
+
const declared = manifest.workspaces;
|
|
224
|
+
if (Array.isArray(declared)) patterns.push(...declared.filter((p) => typeof p === "string"));
|
|
225
|
+
else if (declared && typeof declared === "object" && Array.isArray(declared.packages)) patterns.push(...declared.packages.filter((p) => typeof p === "string"));
|
|
226
|
+
try {
|
|
227
|
+
const yaml = fs.readFileSync(path.join(dir, "pnpm-workspace.yaml"), "utf8");
|
|
228
|
+
for (const line of yaml.split(/\r?\n/)) {
|
|
229
|
+
const entry = /^\s*-\s*["']?([^"'#]+?)["']?\s*$/.exec(line);
|
|
230
|
+
if (entry) patterns.push(entry[1]);
|
|
231
|
+
}
|
|
232
|
+
} catch {}
|
|
233
|
+
const dirs = [];
|
|
234
|
+
for (const pattern of patterns) if (pattern.endsWith("/*")) {
|
|
235
|
+
const parent = path.join(dir, pattern.slice(0, -2));
|
|
236
|
+
try {
|
|
237
|
+
for (const entry of fs.readdirSync(parent, { withFileTypes: true })) if (entry.isDirectory()) dirs.push(path.join(parent, entry.name));
|
|
238
|
+
} catch {}
|
|
239
|
+
} else if (!pattern.includes("*")) dirs.push(path.join(dir, pattern));
|
|
240
|
+
return dirs;
|
|
241
|
+
}
|
|
242
|
+
function manifestsFrom(projectRoot) {
|
|
243
|
+
const manifests = [];
|
|
244
|
+
let dir = path.resolve(projectRoot);
|
|
245
|
+
for (;;) {
|
|
246
|
+
try {
|
|
247
|
+
const raw = fs.readFileSync(path.join(dir, "package.json"), "utf8");
|
|
248
|
+
const manifest = JSON.parse(raw);
|
|
249
|
+
manifests.push({
|
|
250
|
+
dir,
|
|
251
|
+
manifest
|
|
252
|
+
});
|
|
253
|
+
for (const member of workspaceMemberDirs(dir, manifest)) try {
|
|
254
|
+
manifests.push({
|
|
255
|
+
dir: member,
|
|
256
|
+
manifest: JSON.parse(fs.readFileSync(path.join(member, "package.json"), "utf8"))
|
|
257
|
+
});
|
|
258
|
+
} catch {}
|
|
259
|
+
} catch {}
|
|
260
|
+
const parent = path.dirname(dir);
|
|
261
|
+
if (parent === dir) return manifests;
|
|
262
|
+
dir = parent;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Packages in the project that declare React Native in their own manifest.
|
|
267
|
+
*
|
|
268
|
+
* These are the ones published the way the React Native ecosystem publishes:
|
|
269
|
+
* untranspiled JSX, Flow, or TypeScript, on the assumption that Metro will compile
|
|
270
|
+
* them. Node cannot run that source, which is why they previously had to be listed
|
|
271
|
+
* by hand in `transform: [...]` — a list every project had to discover the hard way,
|
|
272
|
+
* one `SyntaxError: Unexpected token '<'` at a time.
|
|
273
|
+
*
|
|
274
|
+
* A package's own manifest is the authority here rather than a heuristic on its
|
|
275
|
+
* name: `react-native-*` misses `@gorhom/bottom-sheet` and would wrongly claim
|
|
276
|
+
* `react-native` itself, while the manifest states the relationship exactly.
|
|
277
|
+
*
|
|
278
|
+
* Declared dependencies are read from the project and every manifest above it, so a
|
|
279
|
+
* workspace that declares its React Native libraries at the repository root is covered
|
|
280
|
+
* too. Resolving each name is cheap and package-manager-agnostic, where walking the
|
|
281
|
+
* installed tree depends on the store layout. Transitive dependencies that nothing
|
|
282
|
+
* declares stay available through `transform: [...]`.
|
|
283
|
+
*
|
|
284
|
+
* Excluded: packages a preset already shadows (their real source never loads), the
|
|
285
|
+
* test infrastructure in NEVER_INLINE, and anything the consumer listed explicitly
|
|
286
|
+
* in `transform` — that option keeps its existing meaning and takes precedence.
|
|
287
|
+
*/
|
|
288
|
+
function detectEcosystemPackages(projectRoot, explicit = []) {
|
|
289
|
+
const roots = (Array.isArray(projectRoot) ? projectRoot : [projectRoot]).filter(Boolean);
|
|
290
|
+
const skip = new Set([
|
|
291
|
+
...NEVER_INLINE,
|
|
292
|
+
...Object.keys(AUTO_DETECT_PRESETS),
|
|
293
|
+
...explicit
|
|
294
|
+
]);
|
|
295
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
296
|
+
const found = roots.flatMap((root) => manifestsFrom(root));
|
|
297
|
+
const requires = [...new Set(found.map((entry) => entry.dir))].map((dir) => createRequire(path.join(dir, "package.json")));
|
|
298
|
+
for (const { manifest } of found) for (const field of [
|
|
299
|
+
"dependencies",
|
|
300
|
+
"devDependencies",
|
|
301
|
+
"optionalDependencies"
|
|
302
|
+
]) {
|
|
303
|
+
const deps = manifest[field];
|
|
304
|
+
if (deps && typeof deps === "object") for (const name of Object.keys(deps)) candidates.add(name);
|
|
305
|
+
}
|
|
306
|
+
if (candidates.size === 0) return [];
|
|
307
|
+
const detected = [];
|
|
308
|
+
for (const name of candidates) {
|
|
309
|
+
if (skip.has(name) || name.startsWith("@react-native/")) continue;
|
|
310
|
+
for (const req of requires) try {
|
|
311
|
+
if (dependsOnReactNative(req(`${name}/package.json`))) detected.push(name);
|
|
312
|
+
break;
|
|
313
|
+
} catch {}
|
|
314
|
+
}
|
|
315
|
+
return detected.sort();
|
|
316
|
+
}
|
|
317
|
+
//#endregion
|
|
318
|
+
export { validatePeerDependency as a, validateOptions as i, detectEngine as n, warnUnknownOptions as o, PEER_REQUIREMENTS as r, detectEcosystemPackages as t };
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
const require_chunk = require("./chunk-CZWwpsFl.cjs");
|
|
2
|
+
const require_errors = require("./errors-flXDIeWk.cjs");
|
|
3
|
+
const require_preset_map = require("./preset-map-DxilQ4pI.cjs");
|
|
4
|
+
let node_fs = require("node:fs");
|
|
5
|
+
node_fs = require_chunk.__toESM(node_fs, 1);
|
|
6
|
+
let node_path = require("node:path");
|
|
7
|
+
node_path = require_chunk.__toESM(node_path, 1);
|
|
8
|
+
let node_module = require("node:module");
|
|
9
|
+
//#region src/validate.ts
|
|
10
|
+
const KNOWN_OPTIONS = [
|
|
11
|
+
"platform",
|
|
12
|
+
"presets",
|
|
13
|
+
"mocks",
|
|
14
|
+
"diagnostics",
|
|
15
|
+
"assetExts",
|
|
16
|
+
"engine",
|
|
17
|
+
"transform",
|
|
18
|
+
"hotRuntime"
|
|
19
|
+
];
|
|
20
|
+
const KNOWN_HOT_RUNTIME_OPTIONS = [
|
|
21
|
+
"recycleAfterFiles",
|
|
22
|
+
"memoryLimit",
|
|
23
|
+
"preserveGlobals"
|
|
24
|
+
];
|
|
25
|
+
function assertStringArray(value, option) {
|
|
26
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry.length === 0)) throw new require_errors.VitestNativeTypeError("INVALID_OPTION", `"${option}" must be an array of non-empty strings.`);
|
|
27
|
+
}
|
|
28
|
+
function assertNonNegativeInteger(value, option) {
|
|
29
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) throw new require_errors.VitestNativeTypeError("INVALID_OPTION", `"${option}" must be a non-negative safe integer.`);
|
|
30
|
+
}
|
|
31
|
+
function validateOptions(options) {
|
|
32
|
+
if (options.engine !== void 0 && options.engine !== "auto" && options.engine !== "mock" && options.engine !== "native") throw new require_errors.VitestNativeTypeError("INVALID_OPTION", `"engine" must be "auto", "mock", or "native".`);
|
|
33
|
+
if (options.platform !== void 0 && options.platform !== "ios" && options.platform !== "android") throw new require_errors.VitestNativeTypeError("INVALID_OPTION", `"platform" must be "ios" or "android".`);
|
|
34
|
+
if (options.diagnostics !== void 0 && typeof options.diagnostics !== "boolean") throw new require_errors.VitestNativeTypeError("INVALID_OPTION", `"diagnostics" must be a boolean.`);
|
|
35
|
+
if (options.assetExts !== void 0) assertStringArray(options.assetExts, "assetExts");
|
|
36
|
+
if (options.transform !== void 0) assertStringArray(options.transform, "transform");
|
|
37
|
+
if (options.presets !== void 0 && !Array.isArray(options.presets)) {
|
|
38
|
+
if (!(options.presets !== null && typeof options.presets === "object" && Object.values(options.presets).every((v) => typeof v === "boolean"))) throw new require_errors.VitestNativeTypeError("INVALID_OPTION", "\"presets\" must be an array of presets, or an object of booleans to switch auto-detected presets off (for example { navigation: false }).");
|
|
39
|
+
}
|
|
40
|
+
if (options.mocks !== void 0 && (options.mocks === null || Array.isArray(options.mocks) || typeof options.mocks !== "object")) throw new require_errors.VitestNativeTypeError("INVALID_OPTION", `"mocks" must be a plain object.`);
|
|
41
|
+
const hotRuntime = options.hotRuntime;
|
|
42
|
+
if (hotRuntime === void 0 || typeof hotRuntime === "boolean") return;
|
|
43
|
+
if (hotRuntime === null || Array.isArray(hotRuntime) || typeof hotRuntime !== "object") throw new require_errors.VitestNativeTypeError("INVALID_OPTION", `"hotRuntime" must be a boolean or an options object.`);
|
|
44
|
+
const hotOptions = hotRuntime;
|
|
45
|
+
for (const key of Object.keys(hotOptions)) if (!KNOWN_HOT_RUNTIME_OPTIONS.includes(key)) {
|
|
46
|
+
const suggestion = findClosest(key, KNOWN_HOT_RUNTIME_OPTIONS);
|
|
47
|
+
throw new require_errors.VitestNativeTypeError("UNKNOWN_OPTION", `Unknown hotRuntime option "${key}".` + (suggestion ? ` Did you mean '${suggestion}'?` : "") + ` Valid options: ${KNOWN_HOT_RUNTIME_OPTIONS.join(", ")}.`);
|
|
48
|
+
}
|
|
49
|
+
if (hotOptions.recycleAfterFiles !== void 0) assertNonNegativeInteger(hotOptions.recycleAfterFiles, "hotRuntime.recycleAfterFiles");
|
|
50
|
+
if (hotOptions.memoryLimit !== void 0) assertNonNegativeInteger(hotOptions.memoryLimit, "hotRuntime.memoryLimit");
|
|
51
|
+
if (hotOptions.preserveGlobals !== void 0) assertStringArray(hotOptions.preserveGlobals, "hotRuntime.preserveGlobals");
|
|
52
|
+
}
|
|
53
|
+
function satisfiesMinimum(version, minimum) {
|
|
54
|
+
const parse = (v) => v.replace(/^[^0-9]*/, "").split(/[-+]/)[0].split(".").map(Number);
|
|
55
|
+
const [aMaj, aMin = 0, aPat = 0] = parse(version);
|
|
56
|
+
const [bMaj, bMin = 0, bPat = 0] = parse(minimum);
|
|
57
|
+
if (aMaj !== bMaj) return aMaj > bMaj;
|
|
58
|
+
if (aMin !== bMin) return aMin > bMin;
|
|
59
|
+
return aPat >= bPat;
|
|
60
|
+
}
|
|
61
|
+
function validatePeerDependency(pkgName, minimumVersion, projectRoot, maximumMajorExclusive, minimumByMajor) {
|
|
62
|
+
const req = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
63
|
+
try {
|
|
64
|
+
const { version } = req(req.resolve(`${pkgName}/package.json`));
|
|
65
|
+
const major = Number(version.replace(/^[^0-9]*/, "").split(".")[0]);
|
|
66
|
+
const requiredMinimum = minimumByMajor?.[major] ?? minimumVersion;
|
|
67
|
+
if (!satisfiesMinimum(version, requiredMinimum)) return `vitest-native requires ${pkgName} >= ${requiredMinimum} for ${major}.x, but found ${version}. Please upgrade.`;
|
|
68
|
+
if (maximumMajorExclusive !== void 0 && major >= maximumMajorExclusive) return `vitest-native supports ${pkgName} >= ${minimumVersion} and < ${maximumMajorExclusive}, but found ${version}.`;
|
|
69
|
+
return null;
|
|
70
|
+
} catch {
|
|
71
|
+
return `vitest-native requires ${pkgName} >= ${minimumVersion}, but it was not found. Please install it.`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function warnUnknownOptions(options) {
|
|
75
|
+
for (const key of Object.keys(options)) if (!KNOWN_OPTIONS.includes(key)) {
|
|
76
|
+
const suggestion = findClosest(key, KNOWN_OPTIONS);
|
|
77
|
+
const hint = suggestion ? ` Did you mean '${suggestion}'?` : "";
|
|
78
|
+
console.warn(`[vitest-native] Unknown option '${key}'.${hint}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function findClosest(input, candidates) {
|
|
82
|
+
let best = null;
|
|
83
|
+
let bestDist = Infinity;
|
|
84
|
+
for (const c of candidates) {
|
|
85
|
+
const d = levenshtein(input.toLowerCase(), c.toLowerCase());
|
|
86
|
+
if (d < bestDist && d <= 3) {
|
|
87
|
+
bestDist = d;
|
|
88
|
+
best = c;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return best;
|
|
92
|
+
}
|
|
93
|
+
function levenshtein(a, b) {
|
|
94
|
+
const m = a.length;
|
|
95
|
+
const n = b.length;
|
|
96
|
+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
|
97
|
+
for (let i = 0; i <= m; i++) dp[i][0] = i;
|
|
98
|
+
for (let j = 0; j <= n; j++) dp[0][j] = j;
|
|
99
|
+
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]);
|
|
100
|
+
return dp[m][n];
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/peer-requirements.ts
|
|
104
|
+
const PEER_REQUIREMENTS = [
|
|
105
|
+
{
|
|
106
|
+
name: "vitest",
|
|
107
|
+
minimum: "4.0.0",
|
|
108
|
+
maximumMajor: 6
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "vite",
|
|
112
|
+
minimum: "6.4.2",
|
|
113
|
+
maximumMajor: 9,
|
|
114
|
+
minimumByMajor: {
|
|
115
|
+
6: "6.4.2",
|
|
116
|
+
7: "7.3.2",
|
|
117
|
+
8: "8.0.5"
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: "react",
|
|
122
|
+
minimum: "18.0.0"
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "@testing-library/react-native",
|
|
126
|
+
minimum: "12.0.0",
|
|
127
|
+
maximumMajor: 15,
|
|
128
|
+
optional: true
|
|
129
|
+
}
|
|
130
|
+
];
|
|
131
|
+
/** Can this project run the native engine? (Both transform deps resolvable.) */
|
|
132
|
+
function isNativeCapable(projectRoot) {
|
|
133
|
+
try {
|
|
134
|
+
const req = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
135
|
+
req.resolve("@react-native/babel-preset");
|
|
136
|
+
req.resolve("@babel/core");
|
|
137
|
+
return true;
|
|
138
|
+
} catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** Resolve the concrete engine for a run. Pure; never throws. */
|
|
143
|
+
function detectEngine(requested, projectRoot, opts) {
|
|
144
|
+
const nativeAvailable = isNativeCapable(projectRoot);
|
|
145
|
+
if (requested === "native") return {
|
|
146
|
+
engine: "native",
|
|
147
|
+
nativeAvailable,
|
|
148
|
+
notice: null
|
|
149
|
+
};
|
|
150
|
+
if (requested === "mock") return {
|
|
151
|
+
engine: "mock",
|
|
152
|
+
nativeAvailable,
|
|
153
|
+
notice: null
|
|
154
|
+
};
|
|
155
|
+
const prefersNative = opts?.autoPrefersNative ?? true;
|
|
156
|
+
if (prefersNative && nativeAvailable) return {
|
|
157
|
+
engine: "native",
|
|
158
|
+
nativeAvailable,
|
|
159
|
+
notice: null
|
|
160
|
+
};
|
|
161
|
+
if (prefersNative) return {
|
|
162
|
+
engine: "mock",
|
|
163
|
+
nativeAvailable: false,
|
|
164
|
+
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."
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
engine: "mock",
|
|
168
|
+
nativeAvailable,
|
|
169
|
+
notice: null
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/native/ecosystem.ts
|
|
174
|
+
/**
|
|
175
|
+
* Packages that must never be auto-inlined even though they depend on React
|
|
176
|
+
* Native. The test library and the renderers underneath it are infrastructure the
|
|
177
|
+
* engine itself wires up; pulling a second copy of them into the module graph has
|
|
178
|
+
* corrupted rendering before, in ways that surface as unrelated act() and
|
|
179
|
+
* host-component failures.
|
|
180
|
+
*/
|
|
181
|
+
const NEVER_INLINE = new Set([
|
|
182
|
+
"@testing-library/react-native",
|
|
183
|
+
"react-test-renderer",
|
|
184
|
+
"test-renderer",
|
|
185
|
+
"react-native"
|
|
186
|
+
]);
|
|
187
|
+
/** Manifest fields that make a package part of the React Native ecosystem. */
|
|
188
|
+
function dependsOnReactNative(manifest) {
|
|
189
|
+
for (const field of ["dependencies", "peerDependencies"]) {
|
|
190
|
+
const deps = manifest[field];
|
|
191
|
+
if (deps && typeof deps === "object" && "react-native" in deps) return true;
|
|
192
|
+
}
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Every package manifest from the project up to the filesystem root.
|
|
197
|
+
*
|
|
198
|
+
* A workspace does not always declare its dependencies where the tests run: an app
|
|
199
|
+
* package may inherit React Native libraries from the repository root, and a Vitest
|
|
200
|
+
* `root` can point at a directory above or below the package that owns them. Reading
|
|
201
|
+
* only the project's own manifest silently found nothing in those layouts and sent
|
|
202
|
+
* the user back to maintaining `transform: [...]` by hand.
|
|
203
|
+
*
|
|
204
|
+
* Over-collecting is safe and under-collecting is not: a package that is listed but
|
|
205
|
+
* never imported costs nothing, because inlining only applies to files that are
|
|
206
|
+
* actually loaded.
|
|
207
|
+
*/
|
|
208
|
+
/**
|
|
209
|
+
* Directories of the workspace members a manifest declares, plus any listed in a
|
|
210
|
+
* sibling pnpm-workspace.yaml.
|
|
211
|
+
*
|
|
212
|
+
* Needed because `manifestsFrom` only walks UP. In a workspace the run root is
|
|
213
|
+
* frequently ABOVE the package under test — Nx invokes tasks from the workspace
|
|
214
|
+
* root, and Vitest's root defaults to the working directory — so the app's own
|
|
215
|
+
* dependencies are declared in a manifest that walking up never reaches. The
|
|
216
|
+
* package then misses auto-detection and stays in Vite's graph while Node loads it
|
|
217
|
+
* too, which is the dual-ownership failure reproducing from nothing but a different
|
|
218
|
+
* working directory.
|
|
219
|
+
*
|
|
220
|
+
* Only the two glob shapes workspace fields actually use are expanded (`dir/*` and a
|
|
221
|
+
* literal path). Over-collecting is safe here — an unimported package costs nothing —
|
|
222
|
+
* so a shape that is not understood is skipped rather than approximated.
|
|
223
|
+
*/
|
|
224
|
+
function workspaceMemberDirs(dir, manifest) {
|
|
225
|
+
const patterns = [];
|
|
226
|
+
const declared = manifest.workspaces;
|
|
227
|
+
if (Array.isArray(declared)) patterns.push(...declared.filter((p) => typeof p === "string"));
|
|
228
|
+
else if (declared && typeof declared === "object" && Array.isArray(declared.packages)) patterns.push(...declared.packages.filter((p) => typeof p === "string"));
|
|
229
|
+
try {
|
|
230
|
+
const yaml = node_fs.default.readFileSync(node_path.default.join(dir, "pnpm-workspace.yaml"), "utf8");
|
|
231
|
+
for (const line of yaml.split(/\r?\n/)) {
|
|
232
|
+
const entry = /^\s*-\s*["']?([^"'#]+?)["']?\s*$/.exec(line);
|
|
233
|
+
if (entry) patterns.push(entry[1]);
|
|
234
|
+
}
|
|
235
|
+
} catch {}
|
|
236
|
+
const dirs = [];
|
|
237
|
+
for (const pattern of patterns) if (pattern.endsWith("/*")) {
|
|
238
|
+
const parent = node_path.default.join(dir, pattern.slice(0, -2));
|
|
239
|
+
try {
|
|
240
|
+
for (const entry of node_fs.default.readdirSync(parent, { withFileTypes: true })) if (entry.isDirectory()) dirs.push(node_path.default.join(parent, entry.name));
|
|
241
|
+
} catch {}
|
|
242
|
+
} else if (!pattern.includes("*")) dirs.push(node_path.default.join(dir, pattern));
|
|
243
|
+
return dirs;
|
|
244
|
+
}
|
|
245
|
+
function manifestsFrom(projectRoot) {
|
|
246
|
+
const manifests = [];
|
|
247
|
+
let dir = node_path.default.resolve(projectRoot);
|
|
248
|
+
for (;;) {
|
|
249
|
+
try {
|
|
250
|
+
const raw = node_fs.default.readFileSync(node_path.default.join(dir, "package.json"), "utf8");
|
|
251
|
+
const manifest = JSON.parse(raw);
|
|
252
|
+
manifests.push({
|
|
253
|
+
dir,
|
|
254
|
+
manifest
|
|
255
|
+
});
|
|
256
|
+
for (const member of workspaceMemberDirs(dir, manifest)) try {
|
|
257
|
+
manifests.push({
|
|
258
|
+
dir: member,
|
|
259
|
+
manifest: JSON.parse(node_fs.default.readFileSync(node_path.default.join(member, "package.json"), "utf8"))
|
|
260
|
+
});
|
|
261
|
+
} catch {}
|
|
262
|
+
} catch {}
|
|
263
|
+
const parent = node_path.default.dirname(dir);
|
|
264
|
+
if (parent === dir) return manifests;
|
|
265
|
+
dir = parent;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Packages in the project that declare React Native in their own manifest.
|
|
270
|
+
*
|
|
271
|
+
* These are the ones published the way the React Native ecosystem publishes:
|
|
272
|
+
* untranspiled JSX, Flow, or TypeScript, on the assumption that Metro will compile
|
|
273
|
+
* them. Node cannot run that source, which is why they previously had to be listed
|
|
274
|
+
* by hand in `transform: [...]` — a list every project had to discover the hard way,
|
|
275
|
+
* one `SyntaxError: Unexpected token '<'` at a time.
|
|
276
|
+
*
|
|
277
|
+
* A package's own manifest is the authority here rather than a heuristic on its
|
|
278
|
+
* name: `react-native-*` misses `@gorhom/bottom-sheet` and would wrongly claim
|
|
279
|
+
* `react-native` itself, while the manifest states the relationship exactly.
|
|
280
|
+
*
|
|
281
|
+
* Declared dependencies are read from the project and every manifest above it, so a
|
|
282
|
+
* workspace that declares its React Native libraries at the repository root is covered
|
|
283
|
+
* too. Resolving each name is cheap and package-manager-agnostic, where walking the
|
|
284
|
+
* installed tree depends on the store layout. Transitive dependencies that nothing
|
|
285
|
+
* declares stay available through `transform: [...]`.
|
|
286
|
+
*
|
|
287
|
+
* Excluded: packages a preset already shadows (their real source never loads), the
|
|
288
|
+
* test infrastructure in NEVER_INLINE, and anything the consumer listed explicitly
|
|
289
|
+
* in `transform` — that option keeps its existing meaning and takes precedence.
|
|
290
|
+
*/
|
|
291
|
+
function detectEcosystemPackages(projectRoot, explicit = []) {
|
|
292
|
+
const roots = (Array.isArray(projectRoot) ? projectRoot : [projectRoot]).filter(Boolean);
|
|
293
|
+
const skip = new Set([
|
|
294
|
+
...NEVER_INLINE,
|
|
295
|
+
...Object.keys(require_preset_map.AUTO_DETECT_PRESETS),
|
|
296
|
+
...explicit
|
|
297
|
+
]);
|
|
298
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
299
|
+
const found = roots.flatMap((root) => manifestsFrom(root));
|
|
300
|
+
const requires = [...new Set(found.map((entry) => entry.dir))].map((dir) => (0, node_module.createRequire)(node_path.default.join(dir, "package.json")));
|
|
301
|
+
for (const { manifest } of found) for (const field of [
|
|
302
|
+
"dependencies",
|
|
303
|
+
"devDependencies",
|
|
304
|
+
"optionalDependencies"
|
|
305
|
+
]) {
|
|
306
|
+
const deps = manifest[field];
|
|
307
|
+
if (deps && typeof deps === "object") for (const name of Object.keys(deps)) candidates.add(name);
|
|
308
|
+
}
|
|
309
|
+
if (candidates.size === 0) return [];
|
|
310
|
+
const detected = [];
|
|
311
|
+
for (const name of candidates) {
|
|
312
|
+
if (skip.has(name) || name.startsWith("@react-native/")) continue;
|
|
313
|
+
for (const req of requires) try {
|
|
314
|
+
if (dependsOnReactNative(req(`${name}/package.json`))) detected.push(name);
|
|
315
|
+
break;
|
|
316
|
+
} catch {}
|
|
317
|
+
}
|
|
318
|
+
return detected.sort();
|
|
319
|
+
}
|
|
320
|
+
//#endregion
|
|
321
|
+
Object.defineProperty(exports, "PEER_REQUIREMENTS", {
|
|
322
|
+
enumerable: true,
|
|
323
|
+
get: function() {
|
|
324
|
+
return PEER_REQUIREMENTS;
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
Object.defineProperty(exports, "detectEcosystemPackages", {
|
|
328
|
+
enumerable: true,
|
|
329
|
+
get: function() {
|
|
330
|
+
return detectEcosystemPackages;
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
Object.defineProperty(exports, "detectEngine", {
|
|
334
|
+
enumerable: true,
|
|
335
|
+
get: function() {
|
|
336
|
+
return detectEngine;
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
Object.defineProperty(exports, "validateOptions", {
|
|
340
|
+
enumerable: true,
|
|
341
|
+
get: function() {
|
|
342
|
+
return validateOptions;
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
Object.defineProperty(exports, "validatePeerDependency", {
|
|
346
|
+
enumerable: true,
|
|
347
|
+
get: function() {
|
|
348
|
+
return validatePeerDependency;
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
Object.defineProperty(exports, "warnUnknownOptions", {
|
|
352
|
+
enumerable: true,
|
|
353
|
+
get: function() {
|
|
354
|
+
return warnUnknownOptions;
|
|
355
|
+
}
|
|
356
|
+
});
|