vitest-native 0.9.0 → 0.11.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 +921 -0
- package/README.md +33 -1
- package/dist/cli.cjs +185 -31
- package/dist/cli.mjs +182 -28
- package/dist/ecosystem-BJzJU3Cf.cjs +568 -0
- package/dist/ecosystem-C4qT_vl3.mjs +506 -0
- package/dist/errors-B3lJK46O.cjs +75 -0
- package/dist/errors-CxoBUfVJ.mjs +64 -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-CEgRDdhd.d.cts → index-CrMeE8rd.d.mts} +14 -9
- package/dist/{index-j-0tueBr.d.mts → index-DIMbjH8R.d.cts} +15 -7
- package/dist/index.d.mts +8 -7
- package/dist/index.mjs +307 -91
- package/dist/jest-compat/interop.mjs +15 -0
- package/dist/jest-compat/setup.mjs +102 -7
- package/dist/jest-compat.cjs +4 -4
- package/dist/jest-compat.d.cts +0 -1
- package/dist/jest-compat.d.mts +0 -1
- package/dist/jest-compat.mjs +2 -2
- package/dist/matchers.cjs +16 -7
- package/dist/matchers.mjs +16 -7
- package/dist/native/hooks.mjs +152 -13
- package/dist/native/loader.mjs +36 -11
- package/dist/native/match.d.mts +33 -0
- package/dist/native/match.mjs +94 -22
- package/dist/native/module-reset.mjs +63 -0
- package/dist/native/registry.mjs +110 -13
- package/dist/native/resolve.d.mts +22 -0
- package/dist/native/resolve.mjs +21 -13
- package/dist/native/runner.mjs +21 -0
- package/dist/native/setup.mjs +4 -2
- package/dist/native/transform.mjs +108 -3
- package/dist/native/worker.mjs +59 -16
- package/dist/{pool-DzsEo9vv.cjs → pool-BU-1ecIF.cjs} +7 -6
- package/dist/{pool-CJmjRgsB.mjs → pool-Bcxn9a3I.mjs} +4 -3
- package/dist/{preset-map-CG4FMtsl.cjs → preset-map-BoG19dLa.cjs} +1 -1
- package/dist/{preset-map-CNsZKfvu.mjs → preset-map-DuX0ocdn.mjs} +1 -1
- package/dist/{presets-C4faM3ds.cjs → presets-CwO_c4aL.cjs} +224 -55
- package/dist/{presets-CuDrNA_W.mjs → presets-IStcJEOa.mjs} +216 -53
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/rntl-matchers.d.ts +35 -0
- package/dist/{chunk-CjcI7cDX.mjs → rolldown-runtime-BhDjJH2R.mjs} +1 -1
- package/dist/{chunk-CZWwpsFl.cjs → rolldown-runtime-C0BPl7ul.cjs} +1 -1
- package/dist/serializer.cjs +34 -4
- package/dist/serializer.mjs +34 -4
- package/dist/setup.d.mts +1 -1
- package/dist/setup.mjs +237 -74
- package/docs/release-readiness.md +22 -5
- package/docs/versioning.md +133 -0
- package/package.json +38 -46
- package/dist/ecosystem-BIWaVwYL.cjs +0 -294
- package/dist/ecosystem-EFtHnKS9.mjs +0 -256
- package/dist/index.cjs +0 -788
- package/dist/index.d.cts +0 -14
- package/dist/presets.cjs +0 -18
- package/dist/presets.d.cts +0 -2
- package/dist/setup.cjs +0 -2702
- package/dist/setup.d.cts +0 -1
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
const require_rolldown_runtime = require("./rolldown-runtime-C0BPl7ul.cjs");
|
|
2
|
+
const require_errors = require("./errors-B3lJK46O.cjs");
|
|
3
|
+
const require_preset_map = require("./preset-map-BoG19dLa.cjs");
|
|
4
|
+
let node_fs = require("node:fs");
|
|
5
|
+
node_fs = require_rolldown_runtime.__toESM(node_fs, 1);
|
|
6
|
+
let node_path = require("node:path");
|
|
7
|
+
node_path = require_rolldown_runtime.__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
|
+
//#endregion
|
|
132
|
+
//#region src/native/match.mjs
|
|
133
|
+
/**
|
|
134
|
+
* Which files belong to which package — the one place that decides it.
|
|
135
|
+
*
|
|
136
|
+
* The native engine splits the module graph between Vite and Node (the rule itself is
|
|
137
|
+
* written down in the header of apply.ts). Four places used to answer "does this file
|
|
138
|
+
* belong to a Node-owned package?" independently: the externalization patterns handed
|
|
139
|
+
* to Vitest, auto-detection, and the transform matchers in the require hook and the
|
|
140
|
+
* ESM loader. They agreed by construction rather than by structure, and three of the
|
|
141
|
+
* defects in this area were one of them disagreeing with the others — a directory
|
|
142
|
+
* anchor the transform side applied and the config side did not, a `node_modules`
|
|
143
|
+
* rule that also matched any folder sharing a package's name, and a separator
|
|
144
|
+
* mismatch that only appeared on Windows.
|
|
145
|
+
*
|
|
146
|
+
* Vitest's `server.deps.external` takes patterns rather than a predicate, so config
|
|
147
|
+
* time and run time cannot literally call the same function. They can be generated
|
|
148
|
+
* from the same rules, which is what `packagePatterns` is for: `buildPkgMatcher`
|
|
149
|
+
* tests exactly the patterns that were handed to Vitest.
|
|
150
|
+
*
|
|
151
|
+
* Everything here is pure. apply.ts bundles a copy into the plugin entry while the
|
|
152
|
+
* worker loads this file directly, so it must stay that way — module-level state
|
|
153
|
+
* would exist twice.
|
|
154
|
+
*/
|
|
155
|
+
/**
|
|
156
|
+
* React Native's own source, including the `@react-native/*` packages. Node owns
|
|
157
|
+
* these unconditionally: the Flow strip, the boundary mocks and the precompiled
|
|
158
|
+
* registry are all Node loader hooks.
|
|
159
|
+
*/
|
|
160
|
+
const REACT_NATIVE_PATH = /[\\/]node_modules[\\/](react-native|@react-native)[\\/]/;
|
|
161
|
+
/** Escape a string for literal use inside a RegExp. */
|
|
162
|
+
function escapeRe(value) {
|
|
163
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
164
|
+
}
|
|
165
|
+
/** Forward slashes, the form Vitest presents module ids in. */
|
|
166
|
+
function toPosix(value) {
|
|
167
|
+
return value.replace(/\\/g, "/");
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Does `dir` contain `target` (or equal it)?
|
|
171
|
+
*
|
|
172
|
+
* Used to recognise the package the run lives in. A package directory containing the
|
|
173
|
+
* Vitest root is the project, not a dependency of it — externalizing it hands Vitest
|
|
174
|
+
* its own source and test files back through Node, where they are compiled to
|
|
175
|
+
* CommonJS.
|
|
176
|
+
*
|
|
177
|
+
* Compared case-insensitively on Windows: `require.resolve` reports the on-disk
|
|
178
|
+
* casing while a working directory carries whatever the shell supplied, and a
|
|
179
|
+
* drive-letter difference alone would silently defeat the check.
|
|
180
|
+
*/
|
|
181
|
+
function containsPath(dir, target) {
|
|
182
|
+
const normalize = (value) => {
|
|
183
|
+
const resolved = node_path.default.resolve(value);
|
|
184
|
+
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
185
|
+
};
|
|
186
|
+
const outer = normalize(dir);
|
|
187
|
+
const inner = normalize(target);
|
|
188
|
+
return inner === outer || inner.startsWith(outer.endsWith(node_path.default.sep) ? outer : outer + node_path.default.sep);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The on-disk directory a package resolves to, or null.
|
|
192
|
+
*
|
|
193
|
+
* `pkg/package.json` is tried first because it names the package root exactly; some
|
|
194
|
+
* packages do not export it, so fall back to resolving the entry and walking up to
|
|
195
|
+
* the manifest that names them. Symlinked and workspace packages resolve to their
|
|
196
|
+
* real location, which is the whole point — see buildPkgMatcher.
|
|
197
|
+
*/
|
|
198
|
+
function packageDirOf(name, projectRoot) {
|
|
199
|
+
const req = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
200
|
+
try {
|
|
201
|
+
return node_path.default.dirname(req.resolve(`${name}/package.json`));
|
|
202
|
+
} catch {}
|
|
203
|
+
let dir;
|
|
204
|
+
try {
|
|
205
|
+
dir = node_path.default.dirname(req.resolve(name));
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
for (;;) {
|
|
210
|
+
try {
|
|
211
|
+
if ((0, node_module.createRequire)(node_path.default.join(dir, "index.js"))("./package.json").name === name) return dir;
|
|
212
|
+
} catch {}
|
|
213
|
+
const parent = node_path.default.dirname(dir);
|
|
214
|
+
if (parent === dir) return null;
|
|
215
|
+
dir = parent;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The patterns identifying one package's files. THE rule; everything else composes it.
|
|
220
|
+
*
|
|
221
|
+
* A bare `[/\]name[/\]` test — what this used to be — also matches any DIRECTORY
|
|
222
|
+
* that happens to share a package's name. A project folder called `expo`, or a source
|
|
223
|
+
* directory named after the library it implements, made every file beneath it look
|
|
224
|
+
* like third-party source to be compiled. That mis-compiled unrelated files, this
|
|
225
|
+
* package's own runtime among them.
|
|
226
|
+
*
|
|
227
|
+
* Anchoring on `node_modules` alone would fix that and break linked packages: a
|
|
228
|
+
* workspace or `file:` dependency resolves to its real path, which has no
|
|
229
|
+
* `node_modules` segment at all. So a file matches if it is either
|
|
230
|
+
*
|
|
231
|
+
* - under `node_modules/<name>/` (covers additional copies of a package that a
|
|
232
|
+
* single resolution cannot see, and projects where resolution fails), or
|
|
233
|
+
* - inside the package's resolved directory (exact; covers workspace links, and
|
|
234
|
+
* stores like pnpm's and bun's).
|
|
235
|
+
*
|
|
236
|
+
* The resolved-directory anchor is dropped when that directory CONTAINS the project
|
|
237
|
+
* root, because then it is the project. Externalizing the project hands Vitest its
|
|
238
|
+
* own source and test files back through Node, which compiles them to CommonJS: a
|
|
239
|
+
* test file's `import { it } from 'vitest'` becomes `require('vitest')` and throws.
|
|
240
|
+
*
|
|
241
|
+
* `projectRoot` is optional; without it only the `node_modules` rule applies, since
|
|
242
|
+
* there is nothing to resolve against.
|
|
243
|
+
*/
|
|
244
|
+
function packagePatterns(name, projectRoot) {
|
|
245
|
+
const patterns = [new RegExp(`[\\\\/]node_modules[\\\\/]${escapeRe(name)}[\\\\/]`)];
|
|
246
|
+
if (!projectRoot) return patterns;
|
|
247
|
+
const dir = packageDirOf(name, projectRoot);
|
|
248
|
+
if (dir && !containsPath(dir, projectRoot)) patterns.push(new RegExp(`^${escapeRe(toPosix(dir).replace(/\/$/, ""))}[\\\\/]`));
|
|
249
|
+
return patterns;
|
|
250
|
+
}
|
|
251
|
+
/** Can this project run the native engine? (Both transform deps resolvable.) */
|
|
252
|
+
function isNativeCapable(projectRoot) {
|
|
253
|
+
try {
|
|
254
|
+
const req = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
255
|
+
req.resolve("@react-native/babel-preset");
|
|
256
|
+
req.resolve("@babel/core");
|
|
257
|
+
return true;
|
|
258
|
+
} catch {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/** Resolve the concrete engine for a run. Pure; never throws. */
|
|
263
|
+
function detectEngine(requested, projectRoot, opts) {
|
|
264
|
+
const nativeAvailable = isNativeCapable(projectRoot);
|
|
265
|
+
if (requested === "native") return {
|
|
266
|
+
engine: "native",
|
|
267
|
+
nativeAvailable,
|
|
268
|
+
notice: null
|
|
269
|
+
};
|
|
270
|
+
if (requested === "mock") return {
|
|
271
|
+
engine: "mock",
|
|
272
|
+
nativeAvailable,
|
|
273
|
+
notice: null
|
|
274
|
+
};
|
|
275
|
+
const prefersNative = opts?.autoPrefersNative ?? true;
|
|
276
|
+
if (prefersNative && nativeAvailable) return {
|
|
277
|
+
engine: "native",
|
|
278
|
+
nativeAvailable,
|
|
279
|
+
notice: null
|
|
280
|
+
};
|
|
281
|
+
if (prefersNative) return {
|
|
282
|
+
engine: "mock",
|
|
283
|
+
nativeAvailable: false,
|
|
284
|
+
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."
|
|
285
|
+
};
|
|
286
|
+
return {
|
|
287
|
+
engine: "mock",
|
|
288
|
+
nativeAvailable,
|
|
289
|
+
notice: null
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
//#endregion
|
|
293
|
+
//#region src/native/ecosystem.ts
|
|
294
|
+
/**
|
|
295
|
+
* Packages that must never be auto-inlined even though they depend on React
|
|
296
|
+
* Native. The test library and the renderers underneath it are infrastructure the
|
|
297
|
+
* engine itself wires up; pulling a second copy of them into the module graph has
|
|
298
|
+
* corrupted rendering before, in ways that surface as unrelated act() and
|
|
299
|
+
* host-component failures.
|
|
300
|
+
*
|
|
301
|
+
* This list is one clause of the ownership rule; the whole of it is written down in
|
|
302
|
+
* the header of native/apply.ts, which is the file to read before changing what ends
|
|
303
|
+
* up in either graph.
|
|
304
|
+
*/
|
|
305
|
+
const NEVER_INLINE = /* @__PURE__ */ new Set([
|
|
306
|
+
"@testing-library/react-native",
|
|
307
|
+
"react-test-renderer",
|
|
308
|
+
"test-renderer",
|
|
309
|
+
"react-native",
|
|
310
|
+
"react",
|
|
311
|
+
"react-is"
|
|
312
|
+
]);
|
|
313
|
+
/** Manifest fields that make a package part of the React Native ecosystem. */
|
|
314
|
+
function dependsOnReactNative(manifest) {
|
|
315
|
+
for (const field of ["dependencies", "peerDependencies"]) {
|
|
316
|
+
const deps = manifest[field];
|
|
317
|
+
if (deps && typeof deps === "object" && "react-native" in deps) return true;
|
|
318
|
+
}
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Every package manifest from the project up to the filesystem root.
|
|
323
|
+
*
|
|
324
|
+
* A workspace does not always declare its dependencies where the tests run: an app
|
|
325
|
+
* package may inherit React Native libraries from the repository root, and a Vitest
|
|
326
|
+
* `root` can point at a directory above or below the package that owns them. Reading
|
|
327
|
+
* only the project's own manifest silently found nothing in those layouts and sent
|
|
328
|
+
* the user back to maintaining `transform: [...]` by hand.
|
|
329
|
+
*
|
|
330
|
+
* Over-collecting is safe and under-collecting is not: a package that is listed but
|
|
331
|
+
* never imported costs nothing, because inlining only applies to files that are
|
|
332
|
+
* actually loaded.
|
|
333
|
+
*/
|
|
334
|
+
/**
|
|
335
|
+
* Directories of the workspace members a manifest declares, plus any listed in a
|
|
336
|
+
* sibling pnpm-workspace.yaml.
|
|
337
|
+
*
|
|
338
|
+
* Needed because `manifestsFrom` only walks UP. In a workspace the run root is
|
|
339
|
+
* frequently ABOVE the package under test — Nx invokes tasks from the workspace
|
|
340
|
+
* root, and Vitest's root defaults to the working directory — so the app's own
|
|
341
|
+
* dependencies are declared in a manifest that walking up never reaches. The
|
|
342
|
+
* package then misses auto-detection and stays in Vite's graph while Node loads it
|
|
343
|
+
* too, which is the dual-ownership failure reproducing from nothing but a different
|
|
344
|
+
* working directory.
|
|
345
|
+
*
|
|
346
|
+
* Only the two glob shapes workspace fields actually use are expanded (`dir/*` and a
|
|
347
|
+
* literal path). Over-collecting is safe here — an unimported package costs nothing —
|
|
348
|
+
* so a shape that is not understood is skipped rather than approximated.
|
|
349
|
+
*/
|
|
350
|
+
function workspaceMemberDirs(dir, manifest) {
|
|
351
|
+
const patterns = [];
|
|
352
|
+
const declared = manifest.workspaces;
|
|
353
|
+
if (Array.isArray(declared)) patterns.push(...declared.filter((p) => typeof p === "string"));
|
|
354
|
+
else if (declared && typeof declared === "object" && Array.isArray(declared.packages)) patterns.push(...declared.packages.filter((p) => typeof p === "string"));
|
|
355
|
+
try {
|
|
356
|
+
const yaml = node_fs.default.readFileSync(node_path.default.join(dir, "pnpm-workspace.yaml"), "utf8");
|
|
357
|
+
for (const line of yaml.split(/\r?\n/)) {
|
|
358
|
+
const entry = /^\s*-\s*["']?([^"'#]+?)["']?\s*$/.exec(line);
|
|
359
|
+
if (entry) patterns.push(entry[1]);
|
|
360
|
+
}
|
|
361
|
+
} catch {}
|
|
362
|
+
const dirs = [];
|
|
363
|
+
for (const pattern of patterns) if (pattern.endsWith("/*")) {
|
|
364
|
+
const parent = node_path.default.join(dir, pattern.slice(0, -2));
|
|
365
|
+
try {
|
|
366
|
+
for (const entry of node_fs.default.readdirSync(parent, { withFileTypes: true })) if (entry.isDirectory()) dirs.push(node_path.default.join(parent, entry.name));
|
|
367
|
+
} catch {}
|
|
368
|
+
} else if (!pattern.includes("*")) dirs.push(node_path.default.join(dir, pattern));
|
|
369
|
+
return dirs;
|
|
370
|
+
}
|
|
371
|
+
function manifestsFrom(projectRoot) {
|
|
372
|
+
const manifests = [];
|
|
373
|
+
let dir = node_path.default.resolve(projectRoot);
|
|
374
|
+
for (;;) {
|
|
375
|
+
try {
|
|
376
|
+
const raw = node_fs.default.readFileSync(node_path.default.join(dir, "package.json"), "utf8");
|
|
377
|
+
const manifest = JSON.parse(raw);
|
|
378
|
+
manifests.push({
|
|
379
|
+
dir,
|
|
380
|
+
manifest
|
|
381
|
+
});
|
|
382
|
+
for (const member of workspaceMemberDirs(dir, manifest)) try {
|
|
383
|
+
manifests.push({
|
|
384
|
+
dir: member,
|
|
385
|
+
manifest: JSON.parse(node_fs.default.readFileSync(node_path.default.join(member, "package.json"), "utf8"))
|
|
386
|
+
});
|
|
387
|
+
} catch {}
|
|
388
|
+
} catch {}
|
|
389
|
+
const parent = node_path.default.dirname(dir);
|
|
390
|
+
if (parent === dir) return manifests;
|
|
391
|
+
dir = parent;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Packages in the project that declare React Native in their own manifest.
|
|
396
|
+
*
|
|
397
|
+
* These are the ones published the way the React Native ecosystem publishes:
|
|
398
|
+
* untranspiled JSX, Flow, or TypeScript, on the assumption that Metro will compile
|
|
399
|
+
* them. Node cannot run that source, which is why they previously had to be listed
|
|
400
|
+
* by hand in `transform: [...]` — a list every project had to discover the hard way,
|
|
401
|
+
* one `SyntaxError: Unexpected token '<'` at a time.
|
|
402
|
+
*
|
|
403
|
+
* A package's own manifest is the authority here rather than a heuristic on its
|
|
404
|
+
* name: `react-native-*` misses `@gorhom/bottom-sheet` and would wrongly claim
|
|
405
|
+
* `react-native` itself, while the manifest states the relationship exactly.
|
|
406
|
+
*
|
|
407
|
+
* Declared dependencies are read from the project and every manifest above it, so a
|
|
408
|
+
* workspace that declares its React Native libraries at the repository root is covered
|
|
409
|
+
* too. Resolving each name is cheap and package-manager-agnostic, where walking the
|
|
410
|
+
* installed tree depends on the store layout. Transitive dependencies that nothing
|
|
411
|
+
* declares stay available through `transform: [...]`.
|
|
412
|
+
*
|
|
413
|
+
* Excluded: packages a preset already shadows (their real source never loads), the
|
|
414
|
+
* test infrastructure in NEVER_INLINE, and anything the consumer listed explicitly
|
|
415
|
+
* in `transform` — that option keeps its existing meaning and takes precedence.
|
|
416
|
+
*/
|
|
417
|
+
function detectEcosystemPackages(projectRoot, explicit = [], testRoots = []) {
|
|
418
|
+
const roots = (Array.isArray(projectRoot) ? projectRoot : [projectRoot]).filter(Boolean);
|
|
419
|
+
const skip = /* @__PURE__ */ new Set([
|
|
420
|
+
...NEVER_INLINE,
|
|
421
|
+
...Object.keys(require_preset_map.AUTO_DETECT_PRESETS),
|
|
422
|
+
...explicit
|
|
423
|
+
]);
|
|
424
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
425
|
+
const found = roots.flatMap((root) => manifestsFrom(root));
|
|
426
|
+
const requires = [...new Set(found.map((entry) => entry.dir))].map((dir) => (0, node_module.createRequire)(node_path.default.join(dir, "package.json")));
|
|
427
|
+
for (const { manifest } of found) for (const field of [
|
|
428
|
+
"dependencies",
|
|
429
|
+
"devDependencies",
|
|
430
|
+
"optionalDependencies"
|
|
431
|
+
]) {
|
|
432
|
+
const deps = manifest[field];
|
|
433
|
+
if (deps && typeof deps === "object") for (const name of Object.keys(deps)) candidates.add(name);
|
|
434
|
+
}
|
|
435
|
+
if (candidates.size === 0) return [];
|
|
436
|
+
/** Resolve a package's manifest from whichever root can see it. */
|
|
437
|
+
const manifestOf = (name) => {
|
|
438
|
+
for (const req of requires) try {
|
|
439
|
+
return req(`${name}/package.json`);
|
|
440
|
+
} catch {}
|
|
441
|
+
return null;
|
|
442
|
+
};
|
|
443
|
+
/**
|
|
444
|
+
* The packages the run itself lives in — the project, in other words.
|
|
445
|
+
*
|
|
446
|
+
* In a workspace, the package under test is usually declared as a dependency by a
|
|
447
|
+
* sibling or by the repository root, so it appears in the candidate set like any
|
|
448
|
+
* third-party library — and it declares React Native, because it is React Native
|
|
449
|
+
* code. Detecting it would claim the project's own source: its directory becomes a
|
|
450
|
+
* `server.deps.external` pattern, so Vitest hands every file under it to Node,
|
|
451
|
+
* where the loader compiles it to CommonJS. A test file's own
|
|
452
|
+
* `import { it } from 'vitest'` becomes `require('vitest')`, which throws
|
|
453
|
+
* "Vitest cannot be imported in a CommonJS module using require()" — a failure that
|
|
454
|
+
* appears in one workspace package and not another for no visible reason.
|
|
455
|
+
*
|
|
456
|
+
* A package cannot be its own dependency, so a manifest whose directory contains a
|
|
457
|
+
* run root belongs to the project. Read straight off the manifests already walked,
|
|
458
|
+
* which is both cheaper than resolving each candidate and independent of how a
|
|
459
|
+
* package manager happens to lay out its store — under pnpm every workspace member
|
|
460
|
+
* is additionally linked into a hidden directory on NODE_PATH, so a package can
|
|
461
|
+
* resolve its own name from its own directory, but nothing here depends on that.
|
|
462
|
+
*
|
|
463
|
+
* Workspace libraries the project merely depends on sit beside a run root rather
|
|
464
|
+
* than above it and are unaffected — detecting those is the point of the
|
|
465
|
+
* workspace-member walk above.
|
|
466
|
+
*/
|
|
467
|
+
const ownerRoots = [...roots, ...testRoots];
|
|
468
|
+
const runOwners = new Set(found.filter(({ dir }) => ownerRoots.some((root) => containsPath(dir, root))).map(({ manifest }) => manifest.name).filter((name) => typeof name === "string"));
|
|
469
|
+
const ownsTheRun = (name) => runOwners.has(name);
|
|
470
|
+
const detected = [];
|
|
471
|
+
for (const name of candidates) {
|
|
472
|
+
if (skip.has(name) || name.startsWith("@react-native/")) continue;
|
|
473
|
+
const pkg = manifestOf(name);
|
|
474
|
+
if (pkg && dependsOnReactNative(pkg) && !ownsTheRun(name)) detected.push(name);
|
|
475
|
+
}
|
|
476
|
+
const rnOwned = new Set(Object.keys(manifestOf("react-native")?.dependencies ?? {}));
|
|
477
|
+
/** Every package reachable from `roots` through runtime dependencies. */
|
|
478
|
+
const closureOf = (starts) => {
|
|
479
|
+
const seen = /* @__PURE__ */ new Set();
|
|
480
|
+
const pending = [...starts];
|
|
481
|
+
while (pending.length > 0) {
|
|
482
|
+
const name = pending.pop();
|
|
483
|
+
if (seen.has(name)) continue;
|
|
484
|
+
seen.add(name);
|
|
485
|
+
const deps = manifestOf(name)?.dependencies;
|
|
486
|
+
if (deps && typeof deps === "object") pending.push(...Object.keys(deps));
|
|
487
|
+
}
|
|
488
|
+
return seen;
|
|
489
|
+
};
|
|
490
|
+
const toolchain = closureOf(["@babel/core", "@react-native/babel-preset"]);
|
|
491
|
+
const inClosure = new Set(detected);
|
|
492
|
+
const queue = [...detected];
|
|
493
|
+
while (queue.length > 0) {
|
|
494
|
+
const deps = manifestOf(queue.pop())?.dependencies;
|
|
495
|
+
if (!deps || typeof deps !== "object") continue;
|
|
496
|
+
for (const dep of Object.keys(deps)) {
|
|
497
|
+
if (inClosure.has(dep) || skip.has(dep) || dep.startsWith("@react-native/")) continue;
|
|
498
|
+
if (rnOwned.has(dep)) continue;
|
|
499
|
+
if (toolchain.has(dep)) continue;
|
|
500
|
+
if (ownsTheRun(dep)) continue;
|
|
501
|
+
if (!manifestOf(dep)) continue;
|
|
502
|
+
inClosure.add(dep);
|
|
503
|
+
queue.push(dep);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return [...inClosure].sort();
|
|
507
|
+
}
|
|
508
|
+
//#endregion
|
|
509
|
+
Object.defineProperty(exports, "PEER_REQUIREMENTS", {
|
|
510
|
+
enumerable: true,
|
|
511
|
+
get: function() {
|
|
512
|
+
return PEER_REQUIREMENTS;
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
Object.defineProperty(exports, "REACT_NATIVE_PATH", {
|
|
516
|
+
enumerable: true,
|
|
517
|
+
get: function() {
|
|
518
|
+
return REACT_NATIVE_PATH;
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
Object.defineProperty(exports, "containsPath", {
|
|
522
|
+
enumerable: true,
|
|
523
|
+
get: function() {
|
|
524
|
+
return containsPath;
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
Object.defineProperty(exports, "detectEcosystemPackages", {
|
|
528
|
+
enumerable: true,
|
|
529
|
+
get: function() {
|
|
530
|
+
return detectEcosystemPackages;
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
Object.defineProperty(exports, "detectEngine", {
|
|
534
|
+
enumerable: true,
|
|
535
|
+
get: function() {
|
|
536
|
+
return detectEngine;
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
Object.defineProperty(exports, "packageDirOf", {
|
|
540
|
+
enumerable: true,
|
|
541
|
+
get: function() {
|
|
542
|
+
return packageDirOf;
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
Object.defineProperty(exports, "packagePatterns", {
|
|
546
|
+
enumerable: true,
|
|
547
|
+
get: function() {
|
|
548
|
+
return packagePatterns;
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
Object.defineProperty(exports, "validateOptions", {
|
|
552
|
+
enumerable: true,
|
|
553
|
+
get: function() {
|
|
554
|
+
return validateOptions;
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
Object.defineProperty(exports, "validatePeerDependency", {
|
|
558
|
+
enumerable: true,
|
|
559
|
+
get: function() {
|
|
560
|
+
return validatePeerDependency;
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
Object.defineProperty(exports, "warnUnknownOptions", {
|
|
564
|
+
enumerable: true,
|
|
565
|
+
get: function() {
|
|
566
|
+
return warnUnknownOptions;
|
|
567
|
+
}
|
|
568
|
+
});
|