vitest-native 0.11.0 → 0.12.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 +261 -0
- package/dist/cli.cjs +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/{ecosystem-BJzJU3Cf.cjs → ecosystem-CEIF_-jq.cjs} +108 -17
- package/dist/{ecosystem-C4qT_vl3.mjs → ecosystem-Duw40-mO.mjs} +108 -17
- package/dist/{index-DIMbjH8R.d.cts → index-CPGq6p95.d.cts} +36 -1
- package/dist/{index-CrMeE8rd.d.mts → index-CmETArCM.d.mts} +36 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +48 -11
- package/dist/native/explain.mjs +28 -7
- package/dist/native/hooks.mjs +30 -6
- package/dist/native/loader.mjs +69 -1
- package/dist/native/module-reset.mjs +10 -5
- package/dist/native/resolve.mjs +17 -7
- package/dist/native/setup.mjs +37 -2
- package/dist/native/transform.mjs +149 -37
- package/dist/native/worker.mjs +5 -0
- package/dist/presets.d.mts +1 -1
- package/package.json +2 -1
package/dist/native/loader.mjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import fs from "node:fs";
|
|
6
|
-
import { transformRN, isFlow, cjsExportNames } from "./transform.mjs";
|
|
6
|
+
import { transformRN, isFlow, cjsExportNames, needsTransform } from "./transform.mjs";
|
|
7
7
|
import { boundarySourceFor } from "./boundary.mjs";
|
|
8
8
|
import { resolvePlatformFile } from "./resolve.mjs";
|
|
9
9
|
import {
|
|
@@ -55,6 +55,59 @@ let presetExports = {};
|
|
|
55
55
|
// Asset file extensions (without leading dot, lower-cased) the loader should stub.
|
|
56
56
|
let assetExtSet = new Set();
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Hot-runtime ESM generation (experimental, `VITEST_NATIVE_HOT_ESM_GEN=1`).
|
|
60
|
+
*
|
|
61
|
+
* Node's ESM registry has no invalidation API, so a package a TEST FILE reaches
|
|
62
|
+
* through `import` keeps its module state for the whole run — the per-file reset
|
|
63
|
+
* can only clear the CommonJS cache. That is the hot runtime's one measured
|
|
64
|
+
* correctness hole at scale (see validation/idiomatic/scale, `extstore`).
|
|
65
|
+
*
|
|
66
|
+
* The registry is keyed by full URL, query included, so stamping a generation onto
|
|
67
|
+
* the URL makes the next file's import a different module and Node evaluates it
|
|
68
|
+
* again. The counter lives in a SharedArrayBuffer because loader hooks run on their
|
|
69
|
+
* own thread; the worker bumps it in the per-file reset.
|
|
70
|
+
*
|
|
71
|
+
* Cost is bounded by ownership: a CommonJS package re-enters through Module._cache,
|
|
72
|
+
* which the reset already drops, so only a thin namespace wrapper is retained per
|
|
73
|
+
* generation. A true-ESM package retains its whole instance — which is why this is
|
|
74
|
+
* measured before it is trusted.
|
|
75
|
+
*/
|
|
76
|
+
let genView = null;
|
|
77
|
+
const generationOf = () => (genView ? Atomics.load(genView, 0) : 0);
|
|
78
|
+
// Modules whose identity must be stable across files: dropping them makes a twin
|
|
79
|
+
// rather than a fresh copy. Mirrors KEEP_RESIDENT in module-reset.mjs.
|
|
80
|
+
const KEEP_RESIDENT =
|
|
81
|
+
/[\\/]node_modules[\\/](react|react-is|react-dom|scheduler|react-reconciler|react-test-renderer|test-renderer|@testing-library[\\/]react-native)[\\/]/;
|
|
82
|
+
// The test stack itself: Vitest's runtime, the chai it asserts through, and this
|
|
83
|
+
// engine. The worker's runtime holds instances of these from boot — the runner's
|
|
84
|
+
// SnapshotClient, chai's extended Assertion, the engine's registry and hooks — and a
|
|
85
|
+
// test file reaches the SAME packages through Node's ESM path. Stamping those
|
|
86
|
+
// resolutions hands the file a twin runtime: `toMatchSnapshot` then asks a
|
|
87
|
+
// SnapshotClient no one set up, `vi.useFakeTimers` flips a copy of the timer state
|
|
88
|
+
// the runner never reads, and matchers extend a chai the expect chain doesn't use.
|
|
89
|
+
//
|
|
90
|
+
// The workspace gates cannot catch a miss here: in-repo, this package and the
|
|
91
|
+
// validation suites resolve OUTSIDE node_modules, so the NODE_MODULES_PATH guard
|
|
92
|
+
// below exempts the engine by accident of layout. Only a packed install — the
|
|
93
|
+
// bake-off apps — puts the whole test stack under node_modules where stamping can
|
|
94
|
+
// reach it. That is how this list was earned: react-native-paper under the hot
|
|
95
|
+
// runtime, 603/678 -> 473/648, every extra failure a twin-runtime symptom.
|
|
96
|
+
const TEST_RUNTIME_RESIDENT =
|
|
97
|
+
/[\\/]node_modules[\\/](vitest|@vitest[\\/][^\\/]+|chai|vitest-native)[\\/]/;
|
|
98
|
+
|
|
99
|
+
/** Should this resolved file carry a generation stamp? */
|
|
100
|
+
function versionable(url) {
|
|
101
|
+
if (!genView || !url.startsWith("file:")) return false;
|
|
102
|
+
const norm = fileURLToPath(url).replace(/\\/g, "/");
|
|
103
|
+
if (!NODE_MODULES_PATH.test(norm)) return false;
|
|
104
|
+
// React Native itself is preloaded once per worker and shared deliberately —
|
|
105
|
+
// re-instantiating it per file would undo the reason the worker stays warm.
|
|
106
|
+
if (REACT_NATIVE_PATH.test(norm) || KEEP_RESIDENT.test(norm)) return false;
|
|
107
|
+
if (TEST_RUNTIME_RESIDENT.test(norm)) return false;
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
58
111
|
export async function initialize(data) {
|
|
59
112
|
if (data && data.projectRoot) PROJECT_ROOT = data.projectRoot;
|
|
60
113
|
if (data && data.platform === "android") PLATFORM = "android";
|
|
@@ -63,6 +116,7 @@ export async function initialize(data) {
|
|
|
63
116
|
if (data && data.presetExports) presetExports = data.presetExports;
|
|
64
117
|
if (data && data.assetExts)
|
|
65
118
|
assetExtSet = new Set(data.assetExts.map((e) => String(e).replace(/^\./, "").toLowerCase()));
|
|
119
|
+
if (data && data.hotGenerationBuffer) genView = new Int32Array(data.hotGenerationBuffer);
|
|
66
120
|
}
|
|
67
121
|
|
|
68
122
|
export async function resolve(specifier, context, nextResolve) {
|
|
@@ -133,6 +187,16 @@ export async function resolve(specifier, context, nextResolve) {
|
|
|
133
187
|
shortCircuit: true,
|
|
134
188
|
};
|
|
135
189
|
}
|
|
190
|
+
|
|
191
|
+
// Stamp the hot generation so the next test file re-evaluates this module rather
|
|
192
|
+
// than reusing the ESM registry's copy. `fileURLToPath` ignores the query, so
|
|
193
|
+
// `load` and every on-disk read below are unaffected.
|
|
194
|
+
if (versionable(resolved.url)) {
|
|
195
|
+
const gen = generationOf();
|
|
196
|
+
if (gen > 0 && !resolved.url.includes("vnhot=")) {
|
|
197
|
+
return { ...resolved, url: `${resolved.url}?vnhot=${gen}`, shortCircuit: true };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
136
200
|
return resolved;
|
|
137
201
|
}
|
|
138
202
|
|
|
@@ -248,6 +312,10 @@ export async function load(url, context, nextLoad) {
|
|
|
248
312
|
// JSX are not installed, so a require here fails with "Unexpected token '<'".
|
|
249
313
|
if (TRANSFORMABLE.test(norm)) {
|
|
250
314
|
const src = fs.readFileSync(file, "utf8");
|
|
315
|
+
// Only compile what Node cannot run as published — see needsTransform. A file V8
|
|
316
|
+
// accepts is handed straight back to Node, which is how the toolchain packages a
|
|
317
|
+
// closure walk drags in stop reaching the Babel preset at all.
|
|
318
|
+
if (!needsTransform(file, src)) return nextLoad(url, context);
|
|
251
319
|
const code = transformRN(file, src, PROJECT_ROOT, PLATFORM);
|
|
252
320
|
const names = cjsExportNames(code);
|
|
253
321
|
return {
|
|
@@ -19,11 +19,16 @@ const UNRESETTABLE = /\.node$/;
|
|
|
19
19
|
* rather than a fresh one.
|
|
20
20
|
*
|
|
21
21
|
* Test files reach these through ESM `import`, which caches them in Node's ESM
|
|
22
|
-
* registry
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
22
|
+
* registry. Dropping the CJS entry therefore does not replace the module; it adds a
|
|
23
|
+
* twin, and the two halves of the test stack stop recognising each other. The symptom
|
|
24
|
+
* is not an error about modules: it is RNTL's matchers failing to see elements that a
|
|
25
|
+
* resident renderer produced.
|
|
26
|
+
*
|
|
27
|
+
* The ESM registry is not reachable from here — it has no invalidation API — but it
|
|
28
|
+
* IS keyed by full URL, and the engine owns the resolve hook, so the loader gives
|
|
29
|
+
* every other externalized package a per-file generation stamp instead (see
|
|
30
|
+
* `versionable` in loader.mjs). The entries below are exempt from that on purpose:
|
|
31
|
+
* for them a fresh instance is the bug, not the fix.
|
|
27
32
|
*
|
|
28
33
|
* Which entries carry weight depends on the RNTL version, so the list is bisected
|
|
29
34
|
* rather than assumed. Measured one entry at a time:
|
package/dist/native/resolve.mjs
CHANGED
|
@@ -18,16 +18,26 @@ import path from "node:path";
|
|
|
18
18
|
export const METRO_SOURCE_EXTS = ["js", "jsx", "json", "ts", "tsx"];
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
21
|
+
* Extension-MAJOR, exactly as Metro resolves: for each source extension in order,
|
|
22
|
+
* try the platform variant, then `.native`, then bare — `.ios.js`, `.native.js`,
|
|
23
|
+
* `.js`, `.ios.jsx`, … (metro-resolver's `resolveSourceFile` loops `sourceExts`
|
|
24
|
+
* in the outer loop and the platform variants inside it).
|
|
25
|
+
*
|
|
26
|
+
* This order previously interleaved the other way — every `.ios.*` before any
|
|
27
|
+
* `.native.*` before any bare extension — and the difference is not stylistic:
|
|
28
|
+
* a module shipping `Foo.native.js` beside `Foo.ios.tsx` resolves to
|
|
29
|
+
* `.native.js` under Metro (the `js` round wins before `tsx` is ever tried) but
|
|
30
|
+
* resolved to `.ios.tsx` here, so the test ran a different file than the app
|
|
31
|
+
* ships. The order is asserted against real metro-resolver by
|
|
32
|
+
* tests/metro-resolver-oracle.test.ts, not just against these literals.
|
|
23
33
|
*/
|
|
24
34
|
export function extensionsFor(platform) {
|
|
25
35
|
const suffix = platform === "android" ? "android" : "ios";
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
const exts = [];
|
|
37
|
+
for (const e of METRO_SOURCE_EXTS) {
|
|
38
|
+
exts.push(`.${suffix}.${e}`, `.native.${e}`, `.${e}`);
|
|
39
|
+
}
|
|
40
|
+
return exts;
|
|
31
41
|
}
|
|
32
42
|
|
|
33
43
|
// Per-worker resolution cache: `${platform}\0${absBase}` → resolved path | null.
|
package/dist/native/setup.mjs
CHANGED
|
@@ -98,6 +98,21 @@ for (const name of presetNames) {
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
// Preset-shadowed packages join the Node-side transform set. Their bare and
|
|
102
|
+
// subpath imports are redirected to the preset mock before any file loads, so the
|
|
103
|
+
// only real files reachable from them are the deliberately exempted pass-throughs
|
|
104
|
+
// (package.json subpaths, assets, and Node-safe utility entries such as `mock`,
|
|
105
|
+
// `plugin`, `jest-utils`). Some of those ship Metro-only source —
|
|
106
|
+
// react-native-worklets' lib/module/mock.js mixes ESM `import` with
|
|
107
|
+
// `module.exports`, which served as published throws "module is not defined in
|
|
108
|
+
// ES module scope" or, on Node 24 (whose global `module` is the Module
|
|
109
|
+
// constructor), loads SILENTLY with empty exports —
|
|
110
|
+
// and the preset shadow is exactly what keeps such packages OUT of the detected
|
|
111
|
+
// ecosystem list, so nothing else will ever compile them. Membership here only
|
|
112
|
+
// makes their files eligible: needsTransform still gates every compile, so a
|
|
113
|
+
// pass-through Node can already run is served untouched.
|
|
114
|
+
const nodeTransformPkgs = [...new Set([...transformPkgs, ...Object.keys(presetExports)])];
|
|
115
|
+
|
|
101
116
|
// Enable the V8 compile cache before RN is compiled (it loads when the test file
|
|
102
117
|
// imports react-native, after this setup runs) so its bytecode is cached to disk
|
|
103
118
|
// and reused on the next file/worker/run. Covers the stock (non-hot) path.
|
|
@@ -119,8 +134,28 @@ if (typeof globalThis.expect === "undefined") {
|
|
|
119
134
|
// installRequireHooks are internally guarded the same way.)
|
|
120
135
|
if (!globalThis.__vitest_native_loader_registered) {
|
|
121
136
|
globalThis.__vitest_native_loader_registered = true;
|
|
137
|
+
// Hot ESM generation (see loader.mjs): closes the hot runtime's one measured
|
|
138
|
+
// correctness hole, where a package a test file `import`s keeps its module state
|
|
139
|
+
// for the whole run because Node's ESM registry cannot be invalidated. Only
|
|
140
|
+
// meaningful under the hot runtime — `__vitest_native_hot_reset` is installed by
|
|
141
|
+
// the hot worker before this setup file runs, and nothing bumps the counter
|
|
142
|
+
// otherwise. Shared rather than passed by value because loader hooks run on their
|
|
143
|
+
// own thread. Set VITEST_NATIVE_HOT_ESM_GEN=0 to opt out (and to mutation-test the
|
|
144
|
+
// isolation gate that covers this).
|
|
145
|
+
if (globalThis.__vitest_native_hot_reset && process.env.VITEST_NATIVE_HOT_ESM_GEN !== "0") {
|
|
146
|
+
globalThis.__vitest_native_hot_generation = new Int32Array(new SharedArrayBuffer(4));
|
|
147
|
+
globalThis.__vitest_native_hot_generation[0] = 1;
|
|
148
|
+
}
|
|
122
149
|
register("./loader.mjs", import.meta.url, {
|
|
123
|
-
data: {
|
|
150
|
+
data: {
|
|
151
|
+
projectRoot,
|
|
152
|
+
platform,
|
|
153
|
+
reactNativeVersion,
|
|
154
|
+
transformPkgs: nodeTransformPkgs,
|
|
155
|
+
presetExports,
|
|
156
|
+
assetExts,
|
|
157
|
+
hotGenerationBuffer: globalThis.__vitest_native_hot_generation?.buffer,
|
|
158
|
+
},
|
|
124
159
|
});
|
|
125
160
|
}
|
|
126
161
|
// Serve React Native from the precompiled registry when the plugin produced one
|
|
@@ -136,7 +171,7 @@ if (process.env.VITEST_NATIVE_RN_REGISTRY) {
|
|
|
136
171
|
);
|
|
137
172
|
}
|
|
138
173
|
}
|
|
139
|
-
installRequireHooks(projectRoot,
|
|
174
|
+
installRequireHooks(projectRoot, nodeTransformPkgs, platform, reactNativeVersion, assetExts);
|
|
140
175
|
|
|
141
176
|
// Build the mock objects now that the require hooks are installed (preset
|
|
142
177
|
// factories may lazily resolve react-native at render time).
|
|
@@ -18,16 +18,43 @@ import path from "node:path";
|
|
|
18
18
|
import fs from "node:fs";
|
|
19
19
|
import os from "node:os";
|
|
20
20
|
import crypto from "node:crypto";
|
|
21
|
+
import vm from "node:vm";
|
|
21
22
|
import { decorateTransformError } from "./explain.mjs";
|
|
22
23
|
import { VitestNativeError } from "../errors.mjs";
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
// Toolchain state is scoped PER PROJECT ROOT, not per process. It used to be a
|
|
26
|
+
// set of module globals initialized by the first caller — but registries for
|
|
27
|
+
// every project in a Vitest workspace are built in the one Vite main process,
|
|
28
|
+
// so the first project's resolved preset, @babel/core, and cache directory
|
|
29
|
+
// silently served every other project. Proven by
|
|
30
|
+
// tests/transform-project-scope.test.ts: a second root's file came back
|
|
31
|
+
// stamped with the first root's Babel toolchain. Same first-caller-wins guard
|
|
32
|
+
// family as the hot worker's require-hook install (see hooks.mjs).
|
|
33
|
+
const contexts = new Map(); // canonical project root -> toolchain context
|
|
34
|
+
|
|
35
|
+
// Memoized: transformRN runs per required file, and the realpath of a project
|
|
36
|
+
// root cannot change within a process. Realpath (not just resolve) so a root
|
|
37
|
+
// reached through a symlink — pnpm layouts, linked workspaces — shares one
|
|
38
|
+
// context with its real directory instead of building a twin toolchain.
|
|
39
|
+
const canonicalRoots = new Map();
|
|
40
|
+
function canonicalRoot(projectRoot) {
|
|
41
|
+
const hit = canonicalRoots.get(projectRoot);
|
|
42
|
+
if (hit) return hit;
|
|
43
|
+
let root;
|
|
44
|
+
try {
|
|
45
|
+
root = fs.realpathSync(projectRoot);
|
|
46
|
+
} catch {
|
|
47
|
+
root = path.resolve(projectRoot);
|
|
48
|
+
}
|
|
49
|
+
canonicalRoots.set(projectRoot, root);
|
|
50
|
+
return root;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Any already-built context — for parse-only helpers with no root of their own. */
|
|
54
|
+
function anyContext() {
|
|
55
|
+
for (const ctx of contexts.values()) return ctx;
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
31
58
|
// 4: Flow enums are now transformed ahead of the preset's strip-types pass, so cached
|
|
32
59
|
// output from 3 is missing every `export enum` declaration. Neither the preset nor
|
|
33
60
|
// Babel version changed, and those are the only other inputs to the directory name, so
|
|
@@ -57,16 +84,20 @@ export function cacheRootFor(projectRoot) {
|
|
|
57
84
|
return dir;
|
|
58
85
|
}
|
|
59
86
|
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
87
|
+
function ctxFor(projectRoot) {
|
|
88
|
+
const root = canonicalRoot(projectRoot);
|
|
89
|
+
const existing = contexts.get(root);
|
|
90
|
+
if (existing) return existing;
|
|
91
|
+
const req = createRequire(path.join(root, "package.json"));
|
|
92
|
+
let preset;
|
|
93
|
+
let flowEnums;
|
|
63
94
|
let presetVersion;
|
|
64
95
|
let babelVersion;
|
|
65
96
|
try {
|
|
66
97
|
// Resolve-only here (cheap); the actual require of @babel/core happens
|
|
67
98
|
// lazily on the first cache miss.
|
|
68
|
-
|
|
69
|
-
|
|
99
|
+
preset = req.resolve("@react-native/babel-preset");
|
|
100
|
+
req.resolve("@babel/core");
|
|
70
101
|
// Flow enums must be transformed BEFORE @babel/plugin-transform-flow-strip-types
|
|
71
102
|
// sees them, or the declaration is removed as if it were a type annotation. The
|
|
72
103
|
// React Native preset carries both, but in separate `overrides` entries that Babel
|
|
@@ -79,16 +110,16 @@ function init(projectRoot) {
|
|
|
79
110
|
// and the enum survives. Resolved from the preset, which depends on it, so no new
|
|
80
111
|
// dependency is required.
|
|
81
112
|
try {
|
|
82
|
-
|
|
113
|
+
flowEnums = createRequire(req.resolve("@react-native/babel-preset/package.json")).resolve(
|
|
83
114
|
"babel-plugin-transform-flow-enums",
|
|
84
115
|
);
|
|
85
116
|
} catch {
|
|
86
117
|
// A preset layout without it: fall back to the preset's own handling rather than
|
|
87
118
|
// failing the run. Flow enums are rare and only two React Native files use them.
|
|
88
|
-
|
|
119
|
+
flowEnums = null;
|
|
89
120
|
}
|
|
90
|
-
presetVersion =
|
|
91
|
-
babelVersion =
|
|
121
|
+
presetVersion = req("@react-native/babel-preset/package.json").version;
|
|
122
|
+
babelVersion = req("@babel/core/package.json").version;
|
|
92
123
|
} catch {
|
|
93
124
|
throw new VitestNativeError(
|
|
94
125
|
"ENGINE_REQUIRES_BABEL",
|
|
@@ -103,16 +134,19 @@ function init(projectRoot) {
|
|
|
103
134
|
// output (e.g. _jsxFileName injection) under NODE_ENV=development than under
|
|
104
135
|
// test/production.
|
|
105
136
|
const babelEnv = process.env.BABEL_ENV || process.env.NODE_ENV || "none";
|
|
106
|
-
|
|
107
|
-
cacheRootFor(
|
|
137
|
+
const cacheDir = path.join(
|
|
138
|
+
cacheRootFor(root),
|
|
108
139
|
`transform-${presetVersion}-b${babelVersion}-${babelEnv}-v${TRANSFORM_CACHE_VERSION}`,
|
|
109
140
|
);
|
|
110
|
-
fs.mkdirSync(
|
|
141
|
+
fs.mkdirSync(cacheDir, { recursive: true });
|
|
142
|
+
const ctx = { req, preset, flowEnums, cacheDir, babel: null, mem: new Map(), writeSeq: 0 };
|
|
143
|
+
contexts.set(root, ctx);
|
|
144
|
+
return ctx;
|
|
111
145
|
}
|
|
112
146
|
|
|
113
|
-
/**
|
|
114
|
-
export function transformCacheDir() {
|
|
115
|
-
return
|
|
147
|
+
/** A project's transform disk-cache directory, once resolved. Test hook. */
|
|
148
|
+
export function transformCacheDir(projectRoot) {
|
|
149
|
+
return contexts.get(canonicalRoot(projectRoot))?.cacheDir ?? null;
|
|
116
150
|
}
|
|
117
151
|
|
|
118
152
|
/** Returns true if the source contains RN Flow syntax that must be transformed. */
|
|
@@ -138,13 +172,15 @@ const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
|
138
172
|
* someValue`), which leaves Node's own detection in charge — no worse than before.
|
|
139
173
|
*/
|
|
140
174
|
export function cjsExportNames(code) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
175
|
+
// Parse-only, so any project's @babel/core serves: the input is this module's
|
|
176
|
+
// own transformed output, and parsing is toolchain-neutral across the versions
|
|
177
|
+
// in play. Callers have no project root of their own to hand over.
|
|
178
|
+
const ctx = anyContext();
|
|
179
|
+
if (!ctx) return [];
|
|
180
|
+
if (!ctx.babel) ctx.babel = ctx.req("@babel/core");
|
|
145
181
|
let ast;
|
|
146
182
|
try {
|
|
147
|
-
ast =
|
|
183
|
+
ast = ctx.babel.parseSync(code, { babelrc: false, configFile: false, sourceType: "script" });
|
|
148
184
|
} catch {
|
|
149
185
|
return [];
|
|
150
186
|
}
|
|
@@ -197,9 +233,85 @@ export function isFlow(src) {
|
|
|
197
233
|
return /@flow|import typeof|\bcomponent\s+\w/.test(src);
|
|
198
234
|
}
|
|
199
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Does this file need compiling, or can Node run it as published?
|
|
238
|
+
*
|
|
239
|
+
* The engine used to answer this by NAME: a package was compiled because detection or
|
|
240
|
+
* a closure walk selected it. That guess was wrong far more often than it was right,
|
|
241
|
+
* and every way of being wrong looked the same — a parse error deep inside a package
|
|
242
|
+
* the project never mentioned. `@babel/runtime`, Metro's `lru-cache` chain and a
|
|
243
|
+
* pure-ESM validator all arrived that way, and each was answered with another name to
|
|
244
|
+
* exclude.
|
|
245
|
+
*
|
|
246
|
+
* The file itself answers precisely. If V8 can parse it, Node can run it, so compiling
|
|
247
|
+
* is optional; if V8 cannot, Node cannot, so compiling is required. That is not a
|
|
248
|
+
* heuristic — it is the same question Node is about to ask.
|
|
249
|
+
*
|
|
250
|
+
* Measured on a 41 KB file: the check costs 0.02 ms against 91 ms for the transform it
|
|
251
|
+
* avoids. That is NOT a speed win in practice and should not be sold as one — on this
|
|
252
|
+
* repository's own native suite, cold cache, it measured 7.68 s against 7.35 s without
|
|
253
|
+
* the check, because React Native's own sources need compiling either way (440 of 450
|
|
254
|
+
* fail to parse) and the check adds a parse to each. The win is that a package Node can
|
|
255
|
+
* already run is never handed to Babel, whatever selected it.
|
|
256
|
+
*
|
|
257
|
+
* What is lost by skipping is downleveling for Hermes — `const` to `var`, destructuring
|
|
258
|
+
* lowered. Verified against React Native's own sources and the installed ecosystem:
|
|
259
|
+
* that is all the preset does to a file V8 accepts. It is behaviour-preserving on
|
|
260
|
+
* Node, and if anything closer to what the package published. `__DEV__` is left
|
|
261
|
+
* standing and top-level requires are not inlined, so neither observable transform
|
|
262
|
+
* applies here.
|
|
263
|
+
*
|
|
264
|
+
* SCRIPT GOAL ONLY. A `type: module` package is still compiled as before: the loader
|
|
265
|
+
* hands those to Node as CommonJS today, and changing that is an interop question
|
|
266
|
+
* about named exports and live bindings that this check does not answer.
|
|
267
|
+
*/
|
|
268
|
+
export function needsTransform(file, src) {
|
|
269
|
+
if (file.endsWith(".mjs") || moduleGoal(file)) return true; // Not this check's business.
|
|
270
|
+
try {
|
|
271
|
+
// Parsed for the verdict only; the compiled script is never run.
|
|
272
|
+
const parsed = new vm.Script(src, { filename: file });
|
|
273
|
+
return parsed === undefined;
|
|
274
|
+
} catch {
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Is this file's nearest package.json `type: module`? Cached per directory. */
|
|
280
|
+
const goalCache = new Map();
|
|
281
|
+
function moduleGoal(file) {
|
|
282
|
+
if (file.endsWith(".cjs")) return false;
|
|
283
|
+
let dir = path.dirname(file);
|
|
284
|
+
const seen = [];
|
|
285
|
+
for (;;) {
|
|
286
|
+
const cached = goalCache.get(dir);
|
|
287
|
+
if (cached !== undefined) {
|
|
288
|
+
for (const d of seen) goalCache.set(d, cached);
|
|
289
|
+
return cached;
|
|
290
|
+
}
|
|
291
|
+
seen.push(dir);
|
|
292
|
+
let verdict;
|
|
293
|
+
try {
|
|
294
|
+
verdict =
|
|
295
|
+
JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8")).type === "module";
|
|
296
|
+
} catch {
|
|
297
|
+
verdict = undefined;
|
|
298
|
+
}
|
|
299
|
+
if (verdict !== undefined) {
|
|
300
|
+
for (const d of seen) goalCache.set(d, verdict);
|
|
301
|
+
return verdict;
|
|
302
|
+
}
|
|
303
|
+
const parent = path.dirname(dir);
|
|
304
|
+
if (parent === dir) {
|
|
305
|
+
for (const d of seen) goalCache.set(d, false);
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
dir = parent;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
200
312
|
/** Transform an RN source file to runnable CJS. Cached in-memory + on disk. */
|
|
201
313
|
export function transformRN(file, src, projectRoot, platform = "ios") {
|
|
202
|
-
|
|
314
|
+
const ctx = ctxFor(projectRoot);
|
|
203
315
|
// The in-memory key uses mtime+size (one statSync) so the hot path skips
|
|
204
316
|
// hashing; the DISK key hashes the actual content, so entries stay valid
|
|
205
317
|
// across fresh installs, Docker mtime normalization, and CI cache restores —
|
|
@@ -209,7 +321,7 @@ export function transformRN(file, src, projectRoot, platform = "ios") {
|
|
|
209
321
|
// identical sources at different paths must not share an entry.
|
|
210
322
|
const st = fs.statSync(file);
|
|
211
323
|
const memKey = `${platform}\0${file}\0${st.mtimeMs}\0${st.size}`;
|
|
212
|
-
const memHit = mem.get(memKey);
|
|
324
|
+
const memHit = ctx.mem.get(memKey);
|
|
213
325
|
if (memHit !== undefined) return memHit;
|
|
214
326
|
|
|
215
327
|
const key = crypto
|
|
@@ -220,20 +332,20 @@ export function transformRN(file, src, projectRoot, platform = "ios") {
|
|
|
220
332
|
.update("\0")
|
|
221
333
|
.update(src)
|
|
222
334
|
.digest("hex");
|
|
223
|
-
const cachePath = path.join(
|
|
335
|
+
const cachePath = path.join(ctx.cacheDir, key + ".js");
|
|
224
336
|
try {
|
|
225
337
|
const cached = fs.readFileSync(cachePath, "utf8");
|
|
226
|
-
mem.set(memKey, cached);
|
|
338
|
+
ctx.mem.set(memKey, cached);
|
|
227
339
|
return cached;
|
|
228
340
|
} catch {}
|
|
229
341
|
|
|
230
|
-
if (!
|
|
342
|
+
if (!ctx.babel) ctx.babel = ctx.req("@babel/core");
|
|
231
343
|
let out;
|
|
232
344
|
try {
|
|
233
|
-
out =
|
|
345
|
+
out = ctx.babel.transformSync(src, {
|
|
234
346
|
filename: file,
|
|
235
|
-
plugins:
|
|
236
|
-
presets: [[
|
|
347
|
+
plugins: ctx.flowEnums ? [ctx.flowEnums] : [],
|
|
348
|
+
presets: [[ctx.preset, { disableStaticViewConfigsCodegen: true }]],
|
|
237
349
|
babelrc: false,
|
|
238
350
|
configFile: false,
|
|
239
351
|
caller: { name: "metro", bundler: "metro", platform, supportsStaticESM: false },
|
|
@@ -247,7 +359,7 @@ export function transformRN(file, src, projectRoot, platform = "ios") {
|
|
|
247
359
|
// Atomic write: multiple worker threads may transform the same RN file
|
|
248
360
|
// concurrently on a cold cache. Write to a unique temp file then rename
|
|
249
361
|
// (atomic on POSIX same-dir) so a concurrent reader never sees a partial file.
|
|
250
|
-
const tmp = `${cachePath}.${process.pid}.${
|
|
362
|
+
const tmp = `${cachePath}.${process.pid}.${ctx.writeSeq++}.tmp`;
|
|
251
363
|
try {
|
|
252
364
|
fs.writeFileSync(tmp, out);
|
|
253
365
|
fs.renameSync(tmp, cachePath);
|
|
@@ -256,6 +368,6 @@ export function transformRN(file, src, projectRoot, platform = "ios") {
|
|
|
256
368
|
fs.rmSync(tmp, { force: true });
|
|
257
369
|
} catch {}
|
|
258
370
|
}
|
|
259
|
-
mem.set(memKey, out);
|
|
371
|
+
ctx.mem.set(memKey, out);
|
|
260
372
|
return out;
|
|
261
373
|
}
|
package/dist/native/worker.mjs
CHANGED
|
@@ -107,6 +107,11 @@ const resetModules = captureModuleBaseline();
|
|
|
107
107
|
const { hotReset, bless } = installHotReset({ projectRoot, diagnostics, preserveGlobals });
|
|
108
108
|
globalThis.__vitest_native_hot_reset = () => {
|
|
109
109
|
globalThis.__vitest_native_registry_reset?.();
|
|
110
|
+
// Advance the ESM generation so externalized packages a test file `import`s are
|
|
111
|
+
// re-evaluated instead of served from Node's uninvalidatable ESM registry.
|
|
112
|
+
if (globalThis.__vitest_native_hot_generation) {
|
|
113
|
+
Atomics.add(globalThis.__vitest_native_hot_generation, 0, 1);
|
|
114
|
+
}
|
|
110
115
|
const dropped = resetModules();
|
|
111
116
|
hotReset();
|
|
112
117
|
if (diagnostics) console.log(`[vitest-native] hot reset: dropped ${dropped} modules`);
|
package/dist/presets.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { _ as gestureHandler, a as flashList, c as svg, d as deviceInfo, f as expo, g as safeAreaContext, h as navigation, i as bottomSheet, l as netInfo, m as asyncStorage, n as worklets, o as vectorIcons, p as screens, r as keyboardController, s as webview, u as mmkv, v as reanimated } from "./index-
|
|
1
|
+
import { _ as gestureHandler, a as flashList, c as svg, d as deviceInfo, f as expo, g as safeAreaContext, h as navigation, i as bottomSheet, l as netInfo, m as asyncStorage, n as worklets, o as vectorIcons, p as screens, r as keyboardController, s as webview, u as mmkv, v as reanimated } from "./index-CmETArCM.mjs";
|
|
2
2
|
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, netInfo, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest-native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Run real React Native tests under Vitest. One install, zero config.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -151,6 +151,7 @@
|
|
|
151
151
|
"ext-platform-lib": "file:./tests-native/fixtures/ext-platform-lib",
|
|
152
152
|
"ext-rn-named-lib": "file:./tests-native/fixtures/ext-rn-named-lib",
|
|
153
153
|
"generic-mainfield-lib": "file:./tests-native/fixtures/generic-mainfield-lib",
|
|
154
|
+
"metro-resolver": "0.87.0",
|
|
154
155
|
"react": "^19.2.8",
|
|
155
156
|
"react-native": "^0.86.0",
|
|
156
157
|
"react-native-gesture-handler": "3.1.0",
|