vitest-native 0.11.1 → 0.13.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 +239 -0
- package/dist/cli.cjs +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/{ecosystem-DGlajfDG.cjs → ecosystem-CEIF_-jq.cjs} +51 -14
- package/dist/{ecosystem-DJMGR2YY.mjs → ecosystem-Duw40-mO.mjs} +51 -14
- package/dist/helpers.cjs +45 -0
- package/dist/helpers.d.cts +8 -1
- package/dist/helpers.d.mts +8 -1
- package/dist/helpers.mjs +45 -1
- 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 +50 -12
- package/dist/native/explain.mjs +28 -7
- package/dist/native/globals.mjs +22 -3
- package/dist/native/hooks.mjs +43 -8
- package/dist/native/loader.mjs +81 -2
- package/dist/native/module-reset.mjs +10 -5
- package/dist/native/registry.mjs +14 -2
- package/dist/native/resolve.mjs +60 -7
- package/dist/native/setup.mjs +38 -3
- package/dist/native/transform.mjs +149 -37
- package/dist/native/worker.mjs +5 -0
- package/dist/{presets-CwO_c4aL.cjs → presets-CoNf4ByH.cjs} +8 -2
- package/dist/{presets-IStcJEOa.mjs → presets-DGProTaA.mjs} +8 -2
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/setup.mjs +26 -2
- package/docs/migrating-from-vitest-react-native.md +1 -1
- package/docs/versioning.md +1 -1
- package/package.json +4 -3
package/dist/native/registry.mjs
CHANGED
|
@@ -34,7 +34,7 @@ import crypto from "node:crypto";
|
|
|
34
34
|
import { transformRN, isFlow, cacheRootFor, TRANSFORM_CACHE_VERSION } from "./transform.mjs";
|
|
35
35
|
|
|
36
36
|
import { boundarySourceFor, BOUNDARY_SOURCES } from "./boundary.mjs";
|
|
37
|
-
import { resolvePlatformFile } from "./resolve.mjs";
|
|
37
|
+
import { resolvePlatformFile, resolveDeepPackageFile } from "./resolve.mjs";
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* Losing the registry is a silent performance cliff, not a correctness problem:
|
|
@@ -557,10 +557,22 @@ export function installRegistry(registryFile, projectRoot) {
|
|
|
557
557
|
try {
|
|
558
558
|
resolved = resolver.resolve(request);
|
|
559
559
|
} catch {
|
|
560
|
-
|
|
560
|
+
// RN 0.87's exports map refuses the deep self-references its preset
|
|
561
|
+
// emits; resolve by path (Metro's behavior) so the file still lands on
|
|
562
|
+
// the registry's instance instead of loading a twin outside it.
|
|
563
|
+
resolved = resolveDeepPackageFile(
|
|
564
|
+
request,
|
|
565
|
+
parent?.filename ? path.dirname(parent.filename) : projectRoot,
|
|
566
|
+
process.env.VITEST_NATIVE_PLATFORM === "android" ? "android" : "ios",
|
|
567
|
+
);
|
|
561
568
|
}
|
|
562
569
|
const id = resolved === null ? undefined : idOf.get(resolved);
|
|
563
570
|
if (id !== undefined) return registry.load(id);
|
|
571
|
+
// Resolved by path but not in the registry: forward the PATH — reloading
|
|
572
|
+
// the bare specifier would just hit the exports map again.
|
|
573
|
+
if (resolved !== null && path.isAbsolute(resolved)) {
|
|
574
|
+
return origLoad.call(this, resolved, parent, ...rest);
|
|
575
|
+
}
|
|
564
576
|
}
|
|
565
577
|
return origLoad.call(this, request, parent, ...rest);
|
|
566
578
|
};
|
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.
|
|
@@ -64,3 +74,46 @@ function scanPlatformFile(absBase, platform) {
|
|
|
64
74
|
}
|
|
65
75
|
return null;
|
|
66
76
|
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Resolve a deep package specifier by PATH when Node's exports-map enforcement
|
|
80
|
+
* rejects it.
|
|
81
|
+
*
|
|
82
|
+
* React Native 0.87 ships an `exports` map whose deep-import surface is gated
|
|
83
|
+
* behind Metro's `react-native-legacy-deep-imports` condition — and the 0.87
|
|
84
|
+
* Babel preset compiles RN's own relative imports into bare deep specifiers
|
|
85
|
+
* (`react-native/src/private/…`), so RN's compiled graph self-references through
|
|
86
|
+
* paths plain Node now refuses (`ERR_PACKAGE_PATH_NOT_EXPORTED`). Metro resolves
|
|
87
|
+
* them; this engine mirrors Metro: locate the package directory by walking
|
|
88
|
+
* node_modules from the requiring file and resolve the file directly, platform
|
|
89
|
+
* extensions included — path resolution is not subject to exports maps.
|
|
90
|
+
*
|
|
91
|
+
* Scoped to react-native and @react-native/* — the packages whose graphs the
|
|
92
|
+
* preset rewrites. Ordinary packages keep Node's exports enforcement.
|
|
93
|
+
*/
|
|
94
|
+
export function resolveDeepPackageFile(request, fromDir, platform) {
|
|
95
|
+
const m = /^(react-native|@react-native\/[^/]+)\/(.+)$/.exec(request);
|
|
96
|
+
if (!m) return null;
|
|
97
|
+
const [, pkg, rest] = m;
|
|
98
|
+
const pkgDir = findPackageDir(fromDir, pkg);
|
|
99
|
+
if (!pkgDir) return null;
|
|
100
|
+
const base = path.join(pkgDir, rest);
|
|
101
|
+
try {
|
|
102
|
+
if (fs.statSync(base).isFile()) return base;
|
|
103
|
+
} catch {}
|
|
104
|
+
// `Foo.js` may exist only as a platform variant, and extensionless specifiers
|
|
105
|
+
// need the full Metro candidate list either way.
|
|
106
|
+
const stem = /\.[a-z0-9]+$/i.test(base) ? base.replace(/\.js$/, "") : base;
|
|
107
|
+
return resolvePlatformFile(stem, platform);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function findPackageDir(startDir, pkg) {
|
|
111
|
+
let dir = path.resolve(startDir);
|
|
112
|
+
for (;;) {
|
|
113
|
+
const candidate = path.join(dir, "node_modules", ...pkg.split("/"));
|
|
114
|
+
if (fs.existsSync(path.join(candidate, "package.json"))) return candidate;
|
|
115
|
+
const parent = path.dirname(dir);
|
|
116
|
+
if (parent === dir) return null;
|
|
117
|
+
dir = parent;
|
|
118
|
+
}
|
|
119
|
+
}
|
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,12 +171,12 @@ 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).
|
|
143
178
|
const g = globalThis;
|
|
144
|
-
g.__vitest_native_preset_mocks = g.__vitest_native_preset_mocks ||
|
|
179
|
+
g.__vitest_native_preset_mocks = g.__vitest_native_preset_mocks || Object.create(null);
|
|
145
180
|
for (const { pkg, mod, presetName } of presetDefs) {
|
|
146
181
|
g.__vitest_native_preset_mocks[pkg] = mod.factory();
|
|
147
182
|
if (diagnostics) {
|
|
@@ -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`);
|
|
@@ -1474,7 +1474,10 @@ function expo() {
|
|
|
1474
1474
|
exports: [
|
|
1475
1475
|
"createURL",
|
|
1476
1476
|
"parse",
|
|
1477
|
-
"useURL"
|
|
1477
|
+
"useURL",
|
|
1478
|
+
"resolveScheme",
|
|
1479
|
+
"addEventListener",
|
|
1480
|
+
"getInitialURL"
|
|
1478
1481
|
],
|
|
1479
1482
|
factory: () => {
|
|
1480
1483
|
const exports = {
|
|
@@ -1485,7 +1488,10 @@ function expo() {
|
|
|
1485
1488
|
path: url,
|
|
1486
1489
|
queryParams: {}
|
|
1487
1490
|
})),
|
|
1488
|
-
useURL: vitest.vi.fn(() => null)
|
|
1491
|
+
useURL: vitest.vi.fn(() => null),
|
|
1492
|
+
resolveScheme: vitest.vi.fn(() => "exp"),
|
|
1493
|
+
addEventListener: vitest.vi.fn(() => ({ remove: () => {} })),
|
|
1494
|
+
getInitialURL: vitest.vi.fn(async () => null)
|
|
1489
1495
|
};
|
|
1490
1496
|
return {
|
|
1491
1497
|
...exports,
|
|
@@ -1472,7 +1472,10 @@ function expo() {
|
|
|
1472
1472
|
exports: [
|
|
1473
1473
|
"createURL",
|
|
1474
1474
|
"parse",
|
|
1475
|
-
"useURL"
|
|
1475
|
+
"useURL",
|
|
1476
|
+
"resolveScheme",
|
|
1477
|
+
"addEventListener",
|
|
1478
|
+
"getInitialURL"
|
|
1476
1479
|
],
|
|
1477
1480
|
factory: () => {
|
|
1478
1481
|
const exports = {
|
|
@@ -1483,7 +1486,10 @@ function expo() {
|
|
|
1483
1486
|
path: url,
|
|
1484
1487
|
queryParams: {}
|
|
1485
1488
|
})),
|
|
1486
|
-
useURL: vi.fn(() => null)
|
|
1489
|
+
useURL: vi.fn(() => null),
|
|
1490
|
+
resolveScheme: vi.fn(() => "exp"),
|
|
1491
|
+
addEventListener: vi.fn(() => ({ remove: () => {} })),
|
|
1492
|
+
getInitialURL: vi.fn(async () => null)
|
|
1487
1493
|
};
|
|
1488
1494
|
return {
|
|
1489
1495
|
...exports,
|
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/dist/presets.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { C as asyncStorage, E as gestureHandler, S as screens, T as safeAreaContext, _ as svg, a as bottomSheet, b as deviceInfo, g as webview, h as vectorIcons, k as reanimated, m as flashList, n as worklets, r as keyboardController, v as netInfo, w as navigation, x as expo, y as mmkv } from "./presets-
|
|
1
|
+
import { C as asyncStorage, E as gestureHandler, S as screens, T as safeAreaContext, _ as svg, a as bottomSheet, b as deviceInfo, g as webview, h as vectorIcons, k as reanimated, m as flashList, n as worklets, r as keyboardController, v as netInfo, w as navigation, x as expo, y as mmkv } from "./presets-DGProTaA.mjs";
|
|
2
2
|
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, netInfo, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
package/dist/setup.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { n as __require } from "./rolldown-runtime-BhDjJH2R.mjs";
|
|
2
2
|
import { t as AUTO_DETECT_PRESETS } from "./preset-map-DuX0ocdn.mjs";
|
|
3
|
-
import { D as createPressableMock, O as buildPressableHostProps, c as createTouchableOpacityMock, d as createFlatListMock, f as createScrollViewMock, i as createViewMock, l as createVirtualizedListMock, o as createTouchableWithoutFeedbackMock, p as createTextInputMock, s as createTouchableHighlightMock, t as presets_exports, u as createSectionListMock } from "./presets-
|
|
3
|
+
import { D as createPressableMock, O as buildPressableHostProps, c as createTouchableOpacityMock, d as createFlatListMock, f as createScrollViewMock, i as createViewMock, l as createVirtualizedListMock, o as createTouchableWithoutFeedbackMock, p as createTextInputMock, s as createTouchableHighlightMock, t as presets_exports, u as createSectionListMock } from "./presets-DGProTaA.mjs";
|
|
4
4
|
import { serializer } from "./serializer.mjs";
|
|
5
5
|
import { animatedMatchers } from "./matchers.mjs";
|
|
6
6
|
import { createRequire } from "node:module";
|
|
@@ -1759,6 +1759,29 @@ function createInteractionManagerMock() {
|
|
|
1759
1759
|
};
|
|
1760
1760
|
}
|
|
1761
1761
|
//#endregion
|
|
1762
|
+
//#region src/mocks/apis/AssetRegistry.ts
|
|
1763
|
+
/**
|
|
1764
|
+
* AssetRegistry — a top-level export since React Native 0.87 (previously only
|
|
1765
|
+
* reachable via `@react-native/assets-registry/registry`). The real module is a
|
|
1766
|
+
* process-wide array: `registerAsset` pushes and returns the new length, so the
|
|
1767
|
+
* first asset gets id 1 (truthy), and `getAssetByID` reads `assets[id - 1]`.
|
|
1768
|
+
* Mirrored exactly, including the 1-based ids, so code that treats an id of 0
|
|
1769
|
+
* as "unregistered" behaves the same against the mock.
|
|
1770
|
+
*/
|
|
1771
|
+
function createAssetRegistryMock() {
|
|
1772
|
+
const assets = [];
|
|
1773
|
+
const mock = {
|
|
1774
|
+
registerAsset: vi.fn((asset) => assets.push(asset)),
|
|
1775
|
+
getAssetByID: vi.fn((assetId) => assets[assetId - 1]),
|
|
1776
|
+
_reset: () => {
|
|
1777
|
+
assets.length = 0;
|
|
1778
|
+
mock.registerAsset.mockClear();
|
|
1779
|
+
mock.getAssetByID.mockClear();
|
|
1780
|
+
}
|
|
1781
|
+
};
|
|
1782
|
+
return mock;
|
|
1783
|
+
}
|
|
1784
|
+
//#endregion
|
|
1762
1785
|
//#region src/mocks/apis/PanResponder.ts
|
|
1763
1786
|
function createPanResponderMock() {
|
|
1764
1787
|
return { create: vi.fn((config = {}) => ({ panHandlers: {
|
|
@@ -2430,6 +2453,7 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
2430
2453
|
Share: createShareMock(),
|
|
2431
2454
|
AccessibilityInfo: createAccessibilityInfoMock(),
|
|
2432
2455
|
InteractionManager: createInteractionManagerMock(),
|
|
2456
|
+
AssetRegistry: createAssetRegistryMock(),
|
|
2433
2457
|
PanResponder: createPanResponderMock(),
|
|
2434
2458
|
ToastAndroid: createToastAndroidMock(),
|
|
2435
2459
|
ActionSheetIOS: createActionSheetIOSMock(),
|
|
@@ -2800,7 +2824,7 @@ function loadExplicitPresets(names) {
|
|
|
2800
2824
|
return loaded;
|
|
2801
2825
|
}
|
|
2802
2826
|
const presets = explicitPresetNames ? loadExplicitPresets(explicitPresetNames) : autoDetectPresets().filter((p) => !disabledPresetNames.includes(p.name));
|
|
2803
|
-
g.__vitest_native_preset_mocks = g.__vitest_native_preset_mocks ||
|
|
2827
|
+
g.__vitest_native_preset_mocks = g.__vitest_native_preset_mocks || Object.create(null);
|
|
2804
2828
|
for (const preset of presets) for (const [modName, presetModule] of Object.entries(preset.modules)) {
|
|
2805
2829
|
const presetMock = presetModule.factory();
|
|
2806
2830
|
installPresetCjsBridge(modName, presetMock);
|
|
@@ -28,7 +28,7 @@ npm i -D vitest-native @react-native/babel-preset @babel/core
|
|
|
28
28
|
|
|
29
29
|
Keep your existing `vitest`, `vite`, `react`, `react-native`, and
|
|
30
30
|
`@testing-library/react-native`. Requirements: **Vitest 4.x or 5.x, Vite ^6.4.2, ^7.3.2, or ^8.0.5, the React
|
|
31
|
-
version required by your RN release, React Native 0.81–0.
|
|
31
|
+
version required by your RN release, React Native 0.81–0.87 (validated), and
|
|
32
32
|
RNTL 12–14.** RNTL 14 uses async rendering APIs and requires Node 22.13 or 24+.
|
|
33
33
|
|
|
34
34
|
## 2. Update `vitest.config`
|
package/docs/versioning.md
CHANGED
|
@@ -23,7 +23,7 @@ and deep paths are not supported even when they happen to resolve.
|
|
|
23
23
|
| Subpath | Covered exports |
|
|
24
24
|
| --- | --- |
|
|
25
25
|
| `vitest-native` | `reactNative`, `presets`, `disabledPresetNames` |
|
|
26
|
-
| `vitest-native/helpers` | `setPlatform`, `setDimensions`, `setColorScheme`, `setInsets`, `mockNativeModule`, `resetAllMocks` |
|
|
26
|
+
| `vitest-native/helpers` | `setPlatform`, `setDimensions`, `setColorScheme`, `setInsets`, `mockNativeModule`, `extendPresetMock`, `resetAllMocks` |
|
|
27
27
|
| `vitest-native/presets` | the 17 preset factories, by name |
|
|
28
28
|
| `vitest-native/matchers` | `toHaveAnimatedStyle`, `toHaveAnimatedProps`, `animatedMatchers` |
|
|
29
29
|
| `vitest-native/serializer` | `serializer` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest-native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Run real React Native tests under Vitest. One install, zero config.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
"@react-native-community/netinfo": "12.0.1",
|
|
139
139
|
"@react-native-vector-icons/common": "13.0.1",
|
|
140
140
|
"@react-native-vector-icons/material-icons": "13.1.2",
|
|
141
|
-
"@react-native/babel-preset": "
|
|
141
|
+
"@react-native/babel-preset": "0.87.0",
|
|
142
142
|
"@react-navigation/native": "7.3.14",
|
|
143
143
|
"@react-navigation/native-stack": "7.18.6",
|
|
144
144
|
"@shopify/flash-list": "2.3.2",
|
|
@@ -151,8 +151,9 @@
|
|
|
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
|
-
"react-native": "
|
|
156
|
+
"react-native": "0.87.0",
|
|
156
157
|
"react-native-gesture-handler": "3.1.0",
|
|
157
158
|
"react-native-keyboard-controller": "1.22.2",
|
|
158
159
|
"react-native-reanimated": "4.5.3",
|