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
package/dist/native/match.mjs
CHANGED
|
@@ -1,11 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which files belong to which package — the one place that decides it.
|
|
3
|
+
*
|
|
4
|
+
* The native engine splits the module graph between Vite and Node (the rule itself is
|
|
5
|
+
* written down in the header of apply.ts). Four places used to answer "does this file
|
|
6
|
+
* belong to a Node-owned package?" independently: the externalization patterns handed
|
|
7
|
+
* to Vitest, auto-detection, and the transform matchers in the require hook and the
|
|
8
|
+
* ESM loader. They agreed by construction rather than by structure, and three of the
|
|
9
|
+
* defects in this area were one of them disagreeing with the others — a directory
|
|
10
|
+
* anchor the transform side applied and the config side did not, a `node_modules`
|
|
11
|
+
* rule that also matched any folder sharing a package's name, and a separator
|
|
12
|
+
* mismatch that only appeared on Windows.
|
|
13
|
+
*
|
|
14
|
+
* Vitest's `server.deps.external` takes patterns rather than a predicate, so config
|
|
15
|
+
* time and run time cannot literally call the same function. They can be generated
|
|
16
|
+
* from the same rules, which is what `packagePatterns` is for: `buildPkgMatcher`
|
|
17
|
+
* tests exactly the patterns that were handed to Vitest.
|
|
18
|
+
*
|
|
19
|
+
* Everything here is pure. apply.ts bundles a copy into the plugin entry while the
|
|
20
|
+
* worker loads this file directly, so it must stay that way — module-level state
|
|
21
|
+
* would exist twice.
|
|
22
|
+
*/
|
|
1
23
|
import { createRequire } from "node:module";
|
|
2
24
|
import path from "node:path";
|
|
3
25
|
|
|
26
|
+
/** Any file under a node_modules directory. */
|
|
27
|
+
export const NODE_MODULES_PATH = /[\\/]node_modules[\\/]/;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* React Native's own source, including the `@react-native/*` packages. Node owns
|
|
31
|
+
* these unconditionally: the Flow strip, the boundary mocks and the precompiled
|
|
32
|
+
* registry are all Node loader hooks.
|
|
33
|
+
*/
|
|
34
|
+
export const REACT_NATIVE_PATH = /[\\/]node_modules[\\/](react-native|@react-native)[\\/]/;
|
|
35
|
+
|
|
4
36
|
/** Escape a string for literal use inside a RegExp. */
|
|
5
37
|
function escapeRe(value) {
|
|
6
38
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
7
39
|
}
|
|
8
40
|
|
|
41
|
+
/** Forward slashes, the form Vitest presents module ids in. */
|
|
42
|
+
function toPosix(value) {
|
|
43
|
+
return value.replace(/\\/g, "/");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Does `dir` contain `target` (or equal it)?
|
|
48
|
+
*
|
|
49
|
+
* Used to recognise the package the run lives in. A package directory containing the
|
|
50
|
+
* Vitest root is the project, not a dependency of it — externalizing it hands Vitest
|
|
51
|
+
* its own source and test files back through Node, where they are compiled to
|
|
52
|
+
* CommonJS.
|
|
53
|
+
*
|
|
54
|
+
* Compared case-insensitively on Windows: `require.resolve` reports the on-disk
|
|
55
|
+
* casing while a working directory carries whatever the shell supplied, and a
|
|
56
|
+
* drive-letter difference alone would silently defeat the check.
|
|
57
|
+
*/
|
|
58
|
+
export function containsPath(dir, target) {
|
|
59
|
+
const normalize = (value) => {
|
|
60
|
+
const resolved = path.resolve(value);
|
|
61
|
+
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
62
|
+
};
|
|
63
|
+
const outer = normalize(dir);
|
|
64
|
+
const inner = normalize(target);
|
|
65
|
+
return inner === outer || inner.startsWith(outer.endsWith(path.sep) ? outer : outer + path.sep);
|
|
66
|
+
}
|
|
67
|
+
|
|
9
68
|
/**
|
|
10
69
|
* The on-disk directory a package resolves to, or null.
|
|
11
70
|
*
|
|
@@ -14,7 +73,7 @@ function escapeRe(value) {
|
|
|
14
73
|
* the manifest that names them. Symlinked and workspace packages resolve to their
|
|
15
74
|
* real location, which is the whole point — see buildPkgMatcher.
|
|
16
75
|
*/
|
|
17
|
-
function packageDirOf(name, projectRoot) {
|
|
76
|
+
export function packageDirOf(name, projectRoot) {
|
|
18
77
|
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
19
78
|
try {
|
|
20
79
|
return path.dirname(req.resolve(`${name}/package.json`));
|
|
@@ -36,9 +95,9 @@ function packageDirOf(name, projectRoot) {
|
|
|
36
95
|
}
|
|
37
96
|
|
|
38
97
|
/**
|
|
39
|
-
*
|
|
98
|
+
* The patterns identifying one package's files. THE rule; everything else composes it.
|
|
40
99
|
*
|
|
41
|
-
* A bare `[/\]name[/\]` test — what this used to
|
|
100
|
+
* A bare `[/\]name[/\]` test — what this used to be — also matches any DIRECTORY
|
|
42
101
|
* that happens to share a package's name. A project folder called `expo`, or a source
|
|
43
102
|
* directory named after the library it implements, made every file beneath it look
|
|
44
103
|
* like third-party source to be compiled. That mis-compiled unrelated files, this
|
|
@@ -48,29 +107,42 @@ function packageDirOf(name, projectRoot) {
|
|
|
48
107
|
* workspace or `file:` dependency resolves to its real path, which has no
|
|
49
108
|
* `node_modules` segment at all. So a file matches if it is either
|
|
50
109
|
*
|
|
110
|
+
* - under `node_modules/<name>/` (covers additional copies of a package that a
|
|
111
|
+
* single resolution cannot see, and projects where resolution fails), or
|
|
51
112
|
* - inside the package's resolved directory (exact; covers workspace links, and
|
|
52
|
-
* stores like pnpm's and bun's)
|
|
53
|
-
*
|
|
54
|
-
*
|
|
113
|
+
* stores like pnpm's and bun's).
|
|
114
|
+
*
|
|
115
|
+
* The resolved-directory anchor is dropped when that directory CONTAINS the project
|
|
116
|
+
* root, because then it is the project. Externalizing the project hands Vitest its
|
|
117
|
+
* own source and test files back through Node, which compiles them to CommonJS: a
|
|
118
|
+
* test file's `import { it } from 'vitest'` becomes `require('vitest')` and throws.
|
|
55
119
|
*
|
|
56
|
-
* `projectRoot` is optional
|
|
57
|
-
*
|
|
120
|
+
* `projectRoot` is optional; without it only the `node_modules` rule applies, since
|
|
121
|
+
* there is nothing to resolve against.
|
|
58
122
|
*/
|
|
59
|
-
export function
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
if (projectRoot) {
|
|
64
|
-
|
|
65
|
-
const dir = packageDirOf(name, projectRoot);
|
|
66
|
-
if (dir) dirs.push(dir.replace(/\\/g, "/").replace(/\/$/, "") + "/");
|
|
67
|
-
}
|
|
123
|
+
export function packagePatterns(name, projectRoot) {
|
|
124
|
+
const patterns = [new RegExp(`[\\\\/]node_modules[\\\\/]${escapeRe(name)}[\\\\/]`)];
|
|
125
|
+
if (!projectRoot) return patterns;
|
|
126
|
+
const dir = packageDirOf(name, projectRoot);
|
|
127
|
+
if (dir && !containsPath(dir, projectRoot)) {
|
|
128
|
+
patterns.push(new RegExp(`^${escapeRe(toPosix(dir).replace(/\/$/, ""))}[\\\\/]`));
|
|
68
129
|
}
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
130
|
+
return patterns;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A predicate over the same patterns Vitest is given, so what the loader transforms
|
|
135
|
+
* and what Vitest externalizes cannot drift apart.
|
|
136
|
+
*
|
|
137
|
+
* Paths are normalised before testing: the directory anchor is written with forward
|
|
138
|
+
* slashes (the form Vitest presents module ids in) while callers here pass whatever
|
|
139
|
+
* Node handed them, which on Windows is backslashes.
|
|
140
|
+
*/
|
|
141
|
+
export function buildPkgMatcher(pkgs, projectRoot) {
|
|
142
|
+
const patterns = (pkgs || []).flatMap((name) => packagePatterns(name, projectRoot));
|
|
143
|
+
return (file) => {
|
|
144
|
+
const norm = toPosix(file);
|
|
145
|
+
return patterns.some((re) => re.test(norm));
|
|
74
146
|
};
|
|
75
147
|
}
|
|
76
148
|
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Per-file reset of the worker's Node module graph (hot runtime).
|
|
2
|
+
//
|
|
3
|
+
// The hot runtime keeps a worker alive across test files — that is where its speed
|
|
4
|
+
// comes from, since a fresh worker costs ~200ms of boot and that dominates a run at
|
|
5
|
+
// scale. What it must not keep is state. Vitest's own per-file reset covers the
|
|
6
|
+
// module-runner graph; everything Vitest externalizes lives in Node's require cache,
|
|
7
|
+
// outside its reach.
|
|
8
|
+
//
|
|
9
|
+
// Anything the worker loaded to bootstrap ITSELF stays; anything a TEST FILE caused
|
|
10
|
+
// to load is dropped and runs again on the next file.
|
|
11
|
+
import Module from "node:module";
|
|
12
|
+
|
|
13
|
+
// Native addons cannot be unloaded — dropping one and requiring it again
|
|
14
|
+
// re-initialises native state in the same process, which crashes some addons.
|
|
15
|
+
const UNRESETTABLE = /\.node$/;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Modules that must NOT be dropped, because dropping them creates a second copy
|
|
19
|
+
* rather than a fresh one.
|
|
20
|
+
*
|
|
21
|
+
* Test files reach these through ESM `import`, which caches them in Node's ESM
|
|
22
|
+
* registry — and that registry has no invalidation API, so the copy a test holds
|
|
23
|
+
* survives any reset. Dropping the CJS entry therefore does not replace the module;
|
|
24
|
+
* it adds a twin, and the two halves of the test stack stop recognising each other.
|
|
25
|
+
* The symptom is not an error about modules: it is RNTL's matchers failing to see
|
|
26
|
+
* elements that a resident renderer produced.
|
|
27
|
+
*
|
|
28
|
+
* Which entries carry weight depends on the RNTL version, so the list is bisected
|
|
29
|
+
* rather than assumed. Measured one entry at a time:
|
|
30
|
+
*
|
|
31
|
+
* @testing-library/react-native RNTL 14: parity 135/135 -> 81/135, 10.4x -> 7.8x.
|
|
32
|
+
* The dominant case on every version.
|
|
33
|
+
* react-test-renderer, RNTL 13: `test:native:hot` 175 -> 173 passing.
|
|
34
|
+
* test-renderer RNTL 14 does not use them and is unaffected —
|
|
35
|
+
* which is why a single-version check calls them
|
|
36
|
+
* dead and is wrong.
|
|
37
|
+
* react, react-is, react-dom, No measured effect on either suite under RNTL 13
|
|
38
|
+
* scheduler, react-reconciler or 14: React is already loaded by the worker's
|
|
39
|
+
* boot-time RN preload, so the baseline snapshot
|
|
40
|
+
* below protects it before this pattern is
|
|
41
|
+
* consulted. Kept because that protection is a
|
|
42
|
+
* side effect of preload contents, not a contract.
|
|
43
|
+
*
|
|
44
|
+
* Note for anyone re-testing this: the two suites disagree, so run both.
|
|
45
|
+
* `validate:hot-parity` is the only one that sees the RNTL regression (it is
|
|
46
|
+
* app-shaped rendering); `test:native:hot` is the only one that sees the
|
|
47
|
+
* react-test-renderer regression (it is engine mechanics).
|
|
48
|
+
*/
|
|
49
|
+
const KEEP_RESIDENT =
|
|
50
|
+
/[\\/]node_modules[\\/](react|react-is|react-dom|scheduler|react-reconciler|react-test-renderer|test-renderer|@testing-library[\\/]react-native)[\\/]/;
|
|
51
|
+
|
|
52
|
+
export function captureModuleBaseline() {
|
|
53
|
+
const baseline = new Set(Object.keys(Module._cache));
|
|
54
|
+
return function resetModules() {
|
|
55
|
+
let dropped = 0;
|
|
56
|
+
for (const id of Object.keys(Module._cache)) {
|
|
57
|
+
if (baseline.has(id) || UNRESETTABLE.test(id) || KEEP_RESIDENT.test(id)) continue;
|
|
58
|
+
delete Module._cache[id];
|
|
59
|
+
dropped++;
|
|
60
|
+
}
|
|
61
|
+
return dropped;
|
|
62
|
+
};
|
|
63
|
+
}
|
package/dist/native/registry.mjs
CHANGED
|
@@ -26,18 +26,67 @@
|
|
|
26
26
|
// through to the per-file hooks in hooks.mjs / loader.mjs, and a failed build leaves
|
|
27
27
|
// the engine running exactly as it did before.
|
|
28
28
|
import Module from "node:module";
|
|
29
|
+
import { REACT_NATIVE_PATH } from "./match.mjs";
|
|
29
30
|
import { createRequire } from "node:module";
|
|
30
31
|
import path from "node:path";
|
|
31
32
|
import fs from "node:fs";
|
|
32
33
|
import crypto from "node:crypto";
|
|
33
|
-
import { transformRN, isFlow, cacheRootFor } from "./transform.mjs";
|
|
34
|
+
import { transformRN, isFlow, cacheRootFor, TRANSFORM_CACHE_VERSION } from "./transform.mjs";
|
|
35
|
+
|
|
34
36
|
import { boundarySourceFor, BOUNDARY_SOURCES } from "./boundary.mjs";
|
|
35
37
|
import { resolvePlatformFile } from "./resolve.mjs";
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Losing the registry is a silent performance cliff, not a correctness problem:
|
|
41
|
+
* React Native still loads, file by file, and nothing fails. Measured on this
|
|
42
|
+
* package's own native suite (~42 files), that costs roughly 1.4x — 1.8s becomes
|
|
43
|
+
* 2.5-2.8s — and the per-file cost compounds on a real app suite.
|
|
44
|
+
*
|
|
45
|
+
* Both failure paths used to return null in silence (the build path spoke only
|
|
46
|
+
* under `diagnostics`, the cache-directory path never spoke at all), so the
|
|
47
|
+
* fallback was indistinguishable from working normally. A precompile failure is
|
|
48
|
+
* exactly the kind of thing that happens on one machine and not another — it
|
|
49
|
+
* happened on a Windows CI runner while this was being reviewed, and reported
|
|
50
|
+
* nothing but a null.
|
|
51
|
+
*
|
|
52
|
+
* Reported once per distinct cause, so a project failing on both platforms says
|
|
53
|
+
* it once, while two different causes are both visible.
|
|
54
|
+
*/
|
|
55
|
+
const reportedRegistryFailures = new Set();
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Exported as a test seam. The shape of the underlying error depends on how the
|
|
59
|
+
* host resolves modules — under the package's own mock-engine test config,
|
|
60
|
+
* `react-native` is aliased to a virtual module and the failure is a one-line
|
|
61
|
+
* argument error, so a test driving `buildRegistry` there can never produce the
|
|
62
|
+
* multi-line "Require stack:" this collapses. Calling it directly is the only way
|
|
63
|
+
* to assert the collapse rather than assume the environment supplies one.
|
|
64
|
+
*/
|
|
65
|
+
export function _warnRegistryUnavailable(rawReason) {
|
|
66
|
+
warnRegistryUnavailable(rawReason);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function warnRegistryUnavailable(rawReason) {
|
|
70
|
+
// Node appends a "Require stack:" block to resolution failures; one line is
|
|
71
|
+
// enough to identify the cause without turning a warning into a wall of text.
|
|
72
|
+
const reason = String(rawReason).split("\n")[0].trim();
|
|
73
|
+
if (reportedRegistryFailures.has(reason)) return;
|
|
74
|
+
reportedRegistryFailures.add(reason);
|
|
75
|
+
console.warn(
|
|
76
|
+
`[vitest-native] (native) could not precompile the React Native registry (${reason}); ` +
|
|
77
|
+
"falling back to per-file module loading, which is slower. Tests still run and " +
|
|
78
|
+
"results are unaffected. Set VITEST_NATIVE_NO_REGISTRY=1 to opt out silently.",
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Test seam: the warning is once-per-cause for the life of the process. */
|
|
83
|
+
export function _resetRegistryFailureReports() {
|
|
84
|
+
reportedRegistryFailures.clear();
|
|
85
|
+
}
|
|
86
|
+
|
|
38
87
|
// Bump when the emitted registry's shape or the walk's semantics change, so a
|
|
39
88
|
// stale on-disk registry from an older vitest-native can never be reused.
|
|
40
|
-
const REGISTRY_FORMAT_VERSION =
|
|
89
|
+
const REGISTRY_FORMAT_VERSION = 2;
|
|
41
90
|
|
|
42
91
|
/**
|
|
43
92
|
* Literal `require('…')` / `require("…")` calls. The leading class excludes
|
|
@@ -111,8 +160,10 @@ function ownVersion() {
|
|
|
111
160
|
* This package's version is in the key for the same reason, covering everything
|
|
112
161
|
* else it contributes — the transform's Babel options, platform resolution, the
|
|
113
162
|
* emitted registry's own shape.
|
|
163
|
+
*
|
|
164
|
+
* Exported for tests. Not part of the public surface — see docs/versioning.md.
|
|
114
165
|
*/
|
|
115
|
-
function registryKey({ projectRoot, platform, reactNativeVersion }) {
|
|
166
|
+
export function registryKey({ projectRoot, platform, reactNativeVersion }) {
|
|
116
167
|
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
117
168
|
const version = (name) => {
|
|
118
169
|
try {
|
|
@@ -135,10 +186,28 @@ function registryKey({ projectRoot, platform, reactNativeVersion }) {
|
|
|
135
186
|
.update(
|
|
136
187
|
[
|
|
137
188
|
`f${REGISTRY_FORMAT_VERSION}`,
|
|
189
|
+
// The registry stores TRANSFORMED module source, so a change in what the
|
|
190
|
+
// transform emits must invalidate it. Nothing else here would: the preset and
|
|
191
|
+
// Babel versions are unchanged when the transform's own configuration changes,
|
|
192
|
+
// and ownVersion() is the published package version, which does not move during
|
|
193
|
+
// development. Adding the Flow-enum plugin produced exactly that situation — the
|
|
194
|
+
// transform was fixed and a warm registry kept serving modules built without it.
|
|
195
|
+
`t${TRANSFORM_CACHE_VERSION}`,
|
|
138
196
|
ownVersion(),
|
|
139
197
|
platform,
|
|
140
198
|
reactNativeVersion,
|
|
141
199
|
version("react-native"),
|
|
200
|
+
// React Native's modules resolve `react` at runtime, so the registry is only
|
|
201
|
+
// valid for the React it was built against. This was missing, and the manifest
|
|
202
|
+
// check below could not cover it: it stats React Native's own files, which do
|
|
203
|
+
// not change when React alone is upgraded. Upgrading React while staying on the
|
|
204
|
+
// same React Native therefore served a registry built against the previous one,
|
|
205
|
+
// and the suite failed with a null React dispatcher ("Cannot read properties of
|
|
206
|
+
// null (reading 'useContext')") plus React Native singletons that no longer
|
|
207
|
+
// compared equal — the duplicate-instance failure this cache exists to avoid.
|
|
208
|
+
// Reproduced by switching between two trees differing only in React's version;
|
|
209
|
+
// clearing node_modules/.cache/vitest-native was the only way out.
|
|
210
|
+
version("react"),
|
|
142
211
|
version("@react-native/babel-preset"),
|
|
143
212
|
version("@babel/core"),
|
|
144
213
|
process.env.BABEL_ENV || process.env.NODE_ENV || "none",
|
|
@@ -268,7 +337,7 @@ function emit(files, modules) {
|
|
|
268
337
|
`const __entry = __r(0);`,
|
|
269
338
|
`module.exports = __entry;`,
|
|
270
339
|
`Object.defineProperty(module.exports, "__vitestNativeRegistry", {`,
|
|
271
|
-
` value: { ids: __ids, load: __r, entry: __ids[0] },`,
|
|
340
|
+
` value: { ids: __ids, load: __r, entry: __ids[0], reset: () => { __m.length = 0; } },`,
|
|
272
341
|
` enumerable: false, configurable: true,`,
|
|
273
342
|
`});`,
|
|
274
343
|
];
|
|
@@ -305,6 +374,9 @@ export function buildRegistry({
|
|
|
305
374
|
assetExts = [],
|
|
306
375
|
diagnostics = false,
|
|
307
376
|
}) {
|
|
377
|
+
// See warnRegistryUnavailable: this is the one path that stays quiet, because
|
|
378
|
+
// the user asked for it.
|
|
379
|
+
//
|
|
308
380
|
// Escape hatch. The registry is transparent by design, but it is also the piece
|
|
309
381
|
// most likely to be implicated if a project sees something unexpected from React
|
|
310
382
|
// Native — so there is a way to take it out of the picture and get the per-file
|
|
@@ -323,7 +395,8 @@ export function buildRegistry({
|
|
|
323
395
|
key = registryKey({ projectRoot, platform, reactNativeVersion });
|
|
324
396
|
dir = path.join(cacheRootFor(projectRoot), "registry");
|
|
325
397
|
fs.mkdirSync(dir, { recursive: true });
|
|
326
|
-
} catch {
|
|
398
|
+
} catch (error) {
|
|
399
|
+
warnRegistryUnavailable(error?.message ?? "cache directory unavailable");
|
|
327
400
|
return null;
|
|
328
401
|
}
|
|
329
402
|
|
|
@@ -348,6 +421,11 @@ export function buildRegistry({
|
|
|
348
421
|
const options = { projectRoot, platform, reactNativeVersion, assetExtSet };
|
|
349
422
|
const modules = new Map();
|
|
350
423
|
const manifest = [];
|
|
424
|
+
// Targets the registry does NOT inline but DOES bake in as pre-resolved absolute
|
|
425
|
+
// paths. They belong in the manifest for the same reason React Native's own files
|
|
426
|
+
// do: if one moves or changes, the paths compiled into the registry are wrong.
|
|
427
|
+
// See the note above the manifest write below.
|
|
428
|
+
const externals = new Set();
|
|
351
429
|
try {
|
|
352
430
|
const entry = createRequire(path.join(projectRoot, "package.json")).resolve("react-native");
|
|
353
431
|
const queue = [entry];
|
|
@@ -367,10 +445,11 @@ export function buildRegistry({
|
|
|
367
445
|
// identity and are shared with the rest of the worker.
|
|
368
446
|
const internal =
|
|
369
447
|
target !== null &&
|
|
370
|
-
|
|
448
|
+
REACT_NATIVE_PATH.test(target.replace(/\\/g, "/")) &&
|
|
371
449
|
!PASSTHROUGH_EXT.has(path.extname(target).toLowerCase());
|
|
372
450
|
deps[request] = target;
|
|
373
451
|
if (internal) queue.push(target);
|
|
452
|
+
else if (target !== null) externals.add(target);
|
|
374
453
|
}
|
|
375
454
|
}
|
|
376
455
|
modules.set(file, { code, deps });
|
|
@@ -378,6 +457,28 @@ export function buildRegistry({
|
|
|
378
457
|
manifest.push([file, st.mtimeMs, st.size]);
|
|
379
458
|
}
|
|
380
459
|
|
|
460
|
+
// Externals are require()d through pre-resolved absolute paths baked into the
|
|
461
|
+
// emitted registry, so the registry is only valid while those paths still point
|
|
462
|
+
// at what they pointed at when it was built. Nothing noticed when they did not:
|
|
463
|
+
// upgrading React served a registry whose baked path was a react directory that
|
|
464
|
+
// no longer existed, and the fallback produced a second React — a null dispatcher
|
|
465
|
+
// and React Native singletons that stopped comparing equal.
|
|
466
|
+
//
|
|
467
|
+
// Naming the packages in the cache key instead would work only for the ones
|
|
468
|
+
// somebody remembered to name. Statting the resolved targets covers every
|
|
469
|
+
// external there is, and covers both node_modules layouts: under bun and pnpm a
|
|
470
|
+
// version change moves the path, so the stat fails; under a flat npm or yarn tree
|
|
471
|
+
// the path survives but the file's size and mtime change.
|
|
472
|
+
for (const target of externals) {
|
|
473
|
+
try {
|
|
474
|
+
const st = fs.statSync(target);
|
|
475
|
+
manifest.push([target, st.mtimeMs, st.size]);
|
|
476
|
+
} catch {
|
|
477
|
+
// Not a real file on disk (a native addon resolved elsewhere, a virtual
|
|
478
|
+
// target). Nothing to pin; the runtime require still decides.
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
381
482
|
const files = [...modules.keys()];
|
|
382
483
|
const { code, map } = emit(files, modules);
|
|
383
484
|
// Write via a unique temp file then rename: several workers may race here on a
|
|
@@ -403,12 +504,7 @@ export function buildRegistry({
|
|
|
403
504
|
}
|
|
404
505
|
return registryFile;
|
|
405
506
|
} catch (error) {
|
|
406
|
-
|
|
407
|
-
console.warn(
|
|
408
|
-
`[vitest-native] (native) could not precompile the RN registry (${error?.message}); ` +
|
|
409
|
-
`falling back to per-file module loading.`,
|
|
410
|
-
);
|
|
411
|
-
}
|
|
507
|
+
warnRegistryUnavailable(error?.message ?? "unknown error");
|
|
412
508
|
return null;
|
|
413
509
|
}
|
|
414
510
|
}
|
|
@@ -440,6 +536,7 @@ export function installRegistry(registryFile, projectRoot) {
|
|
|
440
536
|
return false;
|
|
441
537
|
}
|
|
442
538
|
globalThis.__vitest_native_registry_installed = true;
|
|
539
|
+
globalThis.__vitest_native_registry_reset = () => registry.reset();
|
|
443
540
|
|
|
444
541
|
const idOf = new Map(registry.ids.map((f, i) => [f, i]));
|
|
445
542
|
const entryId = idOf.get(registry.entry);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for resolve.mjs. Hand-written because the implementation is plain .mjs: it
|
|
3
|
+
* is loaded verbatim by the Node-side require hook and ESM loader at run time.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Metro's default `sourceExts`, in Metro's own precedence order. */
|
|
7
|
+
export declare const METRO_SOURCE_EXTS: string[];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Extensions to try for `platform`, in Metro's order: every platform-suffixed
|
|
11
|
+
* variant, then every `.native` one, then the bare extensions.
|
|
12
|
+
*/
|
|
13
|
+
export declare function extensionsFor(platform: "ios" | "android"): string[];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* First existing platform variant of an extensionless absolute base path, or its
|
|
17
|
+
* directory index, or null when nothing matches.
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolvePlatformFile(
|
|
20
|
+
absBase: string,
|
|
21
|
+
platform?: "ios" | "android",
|
|
22
|
+
): string | null;
|
package/dist/native/resolve.mjs
CHANGED
|
@@ -4,21 +4,29 @@
|
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Metro's default `sourceExts`, in its order. Kept as data rather than spelled out
|
|
9
|
+
* per platform so the Vite graph and the Node graph cannot drift apart — both build
|
|
10
|
+
* their list from this one array (see `getPlatformExtensions` in ../resolve.ts).
|
|
11
|
+
*
|
|
12
|
+
* The order is load-bearing where a module has more than one variant on disk:
|
|
13
|
+
* Metro picks `Foo.js` over `Foo.tsx`, so this list must too, or a project with a
|
|
14
|
+
* compiled file beside its source tests a different file than it ships. `json` is
|
|
15
|
+
* a source extension to Metro, which is why `import config from './config'`
|
|
16
|
+
* resolves `config.json` in an app.
|
|
17
|
+
*/
|
|
18
|
+
export const METRO_SOURCE_EXTS = ["js", "jsx", "json", "ts", "tsx"];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Metro tries every platform-suffixed variant first, then every `.native` one,
|
|
22
|
+
* then the bare extensions — not extension-major.
|
|
23
|
+
*/
|
|
24
|
+
export function extensionsFor(platform) {
|
|
8
25
|
const suffix = platform === "android" ? "android" : "ios";
|
|
9
26
|
return [
|
|
10
|
-
`.${suffix}
|
|
11
|
-
|
|
12
|
-
`.${
|
|
13
|
-
`.${suffix}.js`,
|
|
14
|
-
".native.tsx",
|
|
15
|
-
".native.ts",
|
|
16
|
-
".native.jsx",
|
|
17
|
-
".native.js",
|
|
18
|
-
".tsx",
|
|
19
|
-
".ts",
|
|
20
|
-
".jsx",
|
|
21
|
-
".js",
|
|
27
|
+
...METRO_SOURCE_EXTS.map((e) => `.${suffix}.${e}`),
|
|
28
|
+
...METRO_SOURCE_EXTS.map((e) => `.native.${e}`),
|
|
29
|
+
...METRO_SOURCE_EXTS.map((e) => `.${e}`),
|
|
22
30
|
];
|
|
23
31
|
}
|
|
24
32
|
|
package/dist/native/runner.mjs
CHANGED
|
@@ -28,6 +28,27 @@ if (!TestRunner) {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export default class NativeHotRunner extends TestRunner {
|
|
31
|
+
// Fires once per test file, before its setup files and its own modules are
|
|
32
|
+
// imported — the point where Vitest resets the module graph when isolation is on.
|
|
33
|
+
// The hot pool runs with isolation off so the worker survives, so the reset happens
|
|
34
|
+
// here instead, through public API rather than by mutating Vitest's worker state.
|
|
35
|
+
//
|
|
36
|
+
// onBeforeCollect, not onCollectStart. Vitest AWAITS this one; it calls
|
|
37
|
+
// onCollectStart without awaiting, and the worker wraps that hook in an async
|
|
38
|
+
// function that first awaits an RPC to the main thread. The reset therefore landed
|
|
39
|
+
// some indeterminate time after collection had already started — measured at ~0.2ms
|
|
40
|
+
// against a 1-45ms head start on the setup-file import, so it always won in
|
|
41
|
+
// practice, but nothing ordered it. Losing that race would have reset the graph
|
|
42
|
+
// mid-import and dropped any mock a setup file had just registered.
|
|
43
|
+
//
|
|
44
|
+
// Vitest passes an array because the hook is shaped for a batch, but its per-file
|
|
45
|
+
// loop invokes the run through startTests([file]) one file at a time, which is the
|
|
46
|
+
// cadence the reset needs.
|
|
47
|
+
async onBeforeCollect(paths) {
|
|
48
|
+
globalThis.__vitest_native_reset_module_runner?.();
|
|
49
|
+
return super.onBeforeCollect?.(paths);
|
|
50
|
+
}
|
|
51
|
+
|
|
31
52
|
async onBeforeRunFiles(files) {
|
|
32
53
|
globalThis.__vitest_native_hot_bless?.();
|
|
33
54
|
return super.onBeforeRunFiles?.(files);
|
package/dist/native/setup.mjs
CHANGED
|
@@ -11,6 +11,7 @@ import { enableV8CompileCache } from "./compile-cache.mjs";
|
|
|
11
11
|
import * as presetFactories from "../presets.mjs";
|
|
12
12
|
import { animatedMatchers } from "../matchers.mjs";
|
|
13
13
|
import { serializer as rnSerializer } from "../serializer.mjs";
|
|
14
|
+
import { VitestNativeError } from "../errors.mjs";
|
|
14
15
|
|
|
15
16
|
// Hot runtime: surgical reset of state left by the PREVIOUS file. Setup files
|
|
16
17
|
// are force-inlined by Vitest, so this body re-runs per test file even when the
|
|
@@ -170,8 +171,9 @@ function emitColorScheme(colorScheme) {
|
|
|
170
171
|
g.__vitest_native_control = {
|
|
171
172
|
engine: "native",
|
|
172
173
|
setPlatform() {
|
|
173
|
-
throw new
|
|
174
|
-
|
|
174
|
+
throw new VitestNativeError(
|
|
175
|
+
"WRONG_ENGINE_FOR_HELPER",
|
|
176
|
+
`setPlatform() is only available with engine:'mock'. ` +
|
|
175
177
|
`The native engine selects platform files when the module graph loads. ` +
|
|
176
178
|
`Use reactNative({ platform: 'ios' | 'android' }) or separate Vitest projects.`,
|
|
177
179
|
);
|