vitest-native 0.13.0 → 0.14.0-rc.2
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 +339 -0
- package/README.md +138 -48
- package/dist/cli.cjs +70 -5
- package/dist/cli.d.cts +2 -3
- package/dist/cli.d.mts +2 -3
- package/dist/cli.mjs +70 -5
- package/dist/{ecosystem-Duw40-mO.mjs → ecosystem-DFbU_MPe.mjs} +389 -18
- package/dist/{ecosystem-CEIF_-jq.cjs → ecosystem-DI1AhK_k.cjs} +408 -25
- package/dist/errors.d.mts +12 -0
- package/dist/helpers.cjs +6 -3
- package/dist/helpers.d.cts +8 -9
- package/dist/helpers.d.mts +8 -9
- package/dist/helpers.mjs +6 -3
- package/dist/{index-CPGq6p95.d.cts → index-BxzW2D7K.d.cts} +33 -9
- package/dist/{index-CmETArCM.d.mts → index-CEbwa4QU.d.mts} +33 -9
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +537 -118
- package/dist/jest-compat/aliases.d.mts +5 -0
- package/dist/jest-compat/aliases.mjs +65 -0
- package/dist/jest-compat/caller.mjs +53 -0
- package/dist/jest-compat/setup.mjs +72 -36
- package/dist/jest-compat.cjs +1 -3
- package/dist/jest-compat.d.cts +4 -5
- package/dist/jest-compat.d.mts +4 -5
- package/dist/jest-compat.mjs +1 -1
- package/dist/matchers.d.cts +9 -9
- package/dist/matchers.d.mts +9 -9
- package/dist/memory-6EPHrJA1.cjs +144 -0
- package/dist/memory-FYbP0_gg.mjs +126 -0
- package/dist/native/boundary.mjs +13 -2
- package/dist/native/bounded-process.mjs +165 -0
- package/dist/native/export-condition-recovery-preload.mjs +5 -0
- package/dist/native/export-condition-recovery.mjs +195 -0
- package/dist/native/globals.mjs +133 -5
- package/dist/native/hooks.mjs +131 -71
- package/dist/native/loader.mjs +41 -28
- package/dist/native/match.d.mts +3 -0
- package/dist/native/match.mjs +38 -2
- package/dist/native/metro-profile-compiler.mjs +234 -0
- package/dist/native/metro-profile.mjs +115 -0
- package/dist/native/module-reset.mjs +111 -6
- package/dist/native/ownership.d.mts +105 -0
- package/dist/native/ownership.mjs +474 -0
- package/dist/native/registry-compiler.mjs +43 -0
- package/dist/native/registry-process.mjs +124 -0
- package/dist/native/registry.mjs +139 -32
- package/dist/native/reset.mjs +305 -135
- package/dist/native/resolve.d.mts +20 -3
- package/dist/native/resolve.mjs +32 -12
- package/dist/native/rntl-hooks.mjs +76 -0
- package/dist/native/setup.mjs +81 -32
- package/dist/native/state-manifest.d.mts +22 -0
- package/dist/native/state-manifest.mjs +136 -0
- package/dist/native/transform.mjs +29 -8
- package/dist/native/worker.mjs +52 -4
- package/dist/pool-B4yesGhI.cjs +121 -0
- package/dist/pool-DjpOjOf3.mjs +119 -0
- package/dist/{presets-CoNf4ByH.cjs → presets-BYHmi7-W.cjs} +117 -18
- package/dist/{presets-DGProTaA.mjs → presets-DC-eM5IA.mjs} +117 -18
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/rntl-matchers.d.ts +58 -12
- package/dist/serializer.cjs +4 -2
- package/dist/serializer.d.cts +3 -4
- package/dist/serializer.d.mts +3 -4
- package/dist/serializer.mjs +4 -2
- package/dist/setup.mjs +61 -50
- package/dist/worker-vitest-BGPo0ek6.mjs +49 -0
- package/dist/worker-vitest-BfiQB8ND.cjs +62 -0
- package/docs/hot-default-design.md +175 -88
- package/docs/migrating-from-jest.md +12 -5
- package/docs/migrating-from-vitest-react-native.md +4 -4
- package/docs/release-readiness.md +39 -13
- package/docs/versioning.md +19 -18
- package/package.json +42 -31
- package/dist/pool-BU-1ecIF.cjs +0 -122
- package/dist/pool-Bcxn9a3I.mjs +0 -118
|
@@ -9,6 +9,112 @@
|
|
|
9
9
|
// Anything the worker loaded to bootstrap ITSELF stays; anything a TEST FILE caused
|
|
10
10
|
// to load is dropped and runs again on the next file.
|
|
11
11
|
import Module from "node:module";
|
|
12
|
+
import { isRuntimeResidentFile } from "./ownership.mjs";
|
|
13
|
+
import { VitestNativeError } from "../errors.mjs";
|
|
14
|
+
|
|
15
|
+
// Capture before the worker installs registry/preset _load interceptors.
|
|
16
|
+
const nodeLoad = Module._load;
|
|
17
|
+
const CLEANUP_ID = "\0vitest-native:cjs-cache-drain";
|
|
18
|
+
|
|
19
|
+
function cleanupError(detail, cause) {
|
|
20
|
+
return new VitestNativeError(
|
|
21
|
+
"HOT_CJS_CACHE_RESET",
|
|
22
|
+
`Cannot safely reset Node's CommonJS cache between test files: ${detail} ` +
|
|
23
|
+
"A custom loader may be incompatible with hot cleanup. " +
|
|
24
|
+
"Set hotRuntime:false to use worker isolation.",
|
|
25
|
+
cause === undefined ? undefined : { cause },
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Compatibility bridge for Node's stale relativeResolveCache fast path.
|
|
31
|
+
*
|
|
32
|
+
* Deleting require.cache is insufficient: subsequent ESM re-export discovery can
|
|
33
|
+
* insert unloaded CJS placeholders which an old relative lookup mistakes for a
|
|
34
|
+
* circular require. Observed on Node 20/22/24; see node-cjs-cache-investigation.md.
|
|
35
|
+
*
|
|
36
|
+
* After all per-file entries are deleted, dry-run only their recorded lookup
|
|
37
|
+
* keys. Node drops each stale key, then our temporary resolver returns an already
|
|
38
|
+
* loaded sentinel, before any source executes. Fake parents keep sentinel children
|
|
39
|
+
* out of real module graphs. This remains an internal-API compatibility bridge,
|
|
40
|
+
* not a public Node cache API; replace it when the upstream fix is supported.
|
|
41
|
+
*
|
|
42
|
+
* Install after require hooks/preload, before tests. drain() is synchronous and
|
|
43
|
+
* must run immediately after cache deletion, before the next file starts loading.
|
|
44
|
+
*/
|
|
45
|
+
export function trackCjsResolutions() {
|
|
46
|
+
const originalResolve = Module._resolveFilename;
|
|
47
|
+
const edges = new Map();
|
|
48
|
+
const sentinel = {};
|
|
49
|
+
let count = 0;
|
|
50
|
+
const resolver = function (request, parent, ...rest) {
|
|
51
|
+
const filename = originalResolve.call(this, request, parent, ...rest);
|
|
52
|
+
if (parent && typeof parent.path === "string" && !Module.isBuiltin(filename)) {
|
|
53
|
+
edges.set(`${parent.path}\0${request}`, { request, parentPath: parent.path, filename });
|
|
54
|
+
}
|
|
55
|
+
return filename;
|
|
56
|
+
};
|
|
57
|
+
Module._resolveFilename = resolver;
|
|
58
|
+
return {
|
|
59
|
+
drain() {
|
|
60
|
+
// A later wrapper can short-circuit our tracker, leaving *zero* recorded
|
|
61
|
+
// edges despite stale Node lookups. Check before the empty-map fast path.
|
|
62
|
+
if (Module._resolveFilename !== resolver) {
|
|
63
|
+
throw cleanupError("the tracking resolver was replaced after installation.");
|
|
64
|
+
}
|
|
65
|
+
const stale = [];
|
|
66
|
+
for (const edge of edges.values()) {
|
|
67
|
+
if (!Object.hasOwn(Module._cache, edge.filename)) stale.push(edge);
|
|
68
|
+
}
|
|
69
|
+
// Retained targets stay retained by baseline/residency policy. Release
|
|
70
|
+
// their per-file aliases too, rather than accumulate worker-long metadata.
|
|
71
|
+
edges.clear();
|
|
72
|
+
if (!stale.length) return 0;
|
|
73
|
+
if (Object.hasOwn(Module._cache, CLEANUP_ID)) {
|
|
74
|
+
throw cleanupError("the reserved cleanup cache entry is already in use.");
|
|
75
|
+
}
|
|
76
|
+
const savedResolve = Module._resolveFilename;
|
|
77
|
+
const savedLoad = Module.prototype.load;
|
|
78
|
+
Module._cache[CLEANUP_ID] = { loaded: true, exports: sentinel };
|
|
79
|
+
Module._resolveFilename = () => CLEANUP_ID;
|
|
80
|
+
Module.prototype.load = () => {
|
|
81
|
+
throw cleanupError("cache-drain attempted module execution.");
|
|
82
|
+
};
|
|
83
|
+
try {
|
|
84
|
+
for (const { request, parentPath } of stale) {
|
|
85
|
+
const parent = { path: parentPath, children: [] };
|
|
86
|
+
if (nodeLoad(request, parent, false) !== sentinel) {
|
|
87
|
+
throw cleanupError("a loader bypassed the cleanup resolver.");
|
|
88
|
+
}
|
|
89
|
+
count++;
|
|
90
|
+
}
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (error?.code === "HOT_CJS_CACHE_RESET") throw error;
|
|
93
|
+
throw cleanupError("the cleanup lookup failed.", error);
|
|
94
|
+
} finally {
|
|
95
|
+
Module._resolveFilename = savedResolve;
|
|
96
|
+
Module.prototype.load = savedLoad;
|
|
97
|
+
delete Module._cache[CLEANUP_ID];
|
|
98
|
+
}
|
|
99
|
+
return stale.length;
|
|
100
|
+
},
|
|
101
|
+
get count() {
|
|
102
|
+
return count;
|
|
103
|
+
},
|
|
104
|
+
get pending() {
|
|
105
|
+
return edges.size;
|
|
106
|
+
},
|
|
107
|
+
// Used by standalone contract tests; the production tracker lives as long
|
|
108
|
+
// as its worker. Never remove a subsequently installed third-party wrapper.
|
|
109
|
+
dispose() {
|
|
110
|
+
if (Module._resolveFilename !== resolver) {
|
|
111
|
+
throw cleanupError("another resolver wrapped the tracking hook.");
|
|
112
|
+
}
|
|
113
|
+
Module._resolveFilename = originalResolve;
|
|
114
|
+
edges.clear();
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
12
118
|
|
|
13
119
|
// Native addons cannot be unloaded — dropping one and requiring it again
|
|
14
120
|
// re-initialises native state in the same process, which crashes some addons.
|
|
@@ -30,8 +136,8 @@ const UNRESETTABLE = /\.node$/;
|
|
|
30
136
|
* `versionable` in loader.mjs). The entries below are exempt from that on purpose:
|
|
31
137
|
* for them a fresh instance is the bug, not the fix.
|
|
32
138
|
*
|
|
33
|
-
* Which entries carry weight depends on the RNTL version, so the
|
|
34
|
-
* rather than assumed. Measured one entry at a time:
|
|
139
|
+
* Which entries carry weight depends on the RNTL version, so the resident policy in
|
|
140
|
+
* ownership.mjs is bisected rather than assumed. Measured one entry at a time:
|
|
35
141
|
*
|
|
36
142
|
* @testing-library/react-native RNTL 14: parity 135/135 -> 81/135, 10.4x -> 7.8x.
|
|
37
143
|
* The dominant case on every version.
|
|
@@ -51,18 +157,17 @@ const UNRESETTABLE = /\.node$/;
|
|
|
51
157
|
* app-shaped rendering); `test:native:hot` is the only one that sees the
|
|
52
158
|
* react-test-renderer regression (it is engine mechanics).
|
|
53
159
|
*/
|
|
54
|
-
const KEEP_RESIDENT =
|
|
55
|
-
/[\\/]node_modules[\\/](react|react-is|react-dom|scheduler|react-reconciler|react-test-renderer|test-renderer|@testing-library[\\/]react-native)[\\/]/;
|
|
56
|
-
|
|
57
160
|
export function captureModuleBaseline() {
|
|
58
161
|
const baseline = new Set(Object.keys(Module._cache));
|
|
162
|
+
const resolutions = trackCjsResolutions();
|
|
59
163
|
return function resetModules() {
|
|
60
164
|
let dropped = 0;
|
|
61
165
|
for (const id of Object.keys(Module._cache)) {
|
|
62
|
-
if (baseline.has(id) || UNRESETTABLE.test(id) ||
|
|
166
|
+
if (baseline.has(id) || UNRESETTABLE.test(id) || isRuntimeResidentFile(id)) continue;
|
|
63
167
|
delete Module._cache[id];
|
|
64
168
|
dropped++;
|
|
65
169
|
}
|
|
170
|
+
resolutions.drain();
|
|
66
171
|
return dropped;
|
|
67
172
|
};
|
|
68
173
|
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export type NativeModuleOwner = "vite" | "node" | "vitest-default";
|
|
2
|
+
export type NativeOwnershipReason =
|
|
3
|
+
| "test-entry"
|
|
4
|
+
| "project-source"
|
|
5
|
+
| "react-native-core"
|
|
6
|
+
| "explicit-transform"
|
|
7
|
+
| "ecosystem-detected"
|
|
8
|
+
| "runtime-transform"
|
|
9
|
+
| "identity-sensitive-runtime"
|
|
10
|
+
| "test-runtime"
|
|
11
|
+
| "delegated";
|
|
12
|
+
|
|
13
|
+
export interface NativeOwnershipDecision {
|
|
14
|
+
readonly owner: NativeModuleOwner;
|
|
15
|
+
readonly format: "source" | "cjs" | "package" | "unknown";
|
|
16
|
+
readonly transform: "vite" | "registry" | "native-babel" | "none" | "default";
|
|
17
|
+
readonly reset: "per-file" | "worker-resident" | "worker-resident-if-external" | "vitest-default";
|
|
18
|
+
readonly reason: NativeOwnershipReason;
|
|
19
|
+
readonly reasons: readonly NativeOwnershipReason[];
|
|
20
|
+
readonly evidence: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface NativeOwnershipRule {
|
|
24
|
+
readonly effect: "owner-transform-reset" | "owner-transform" | "reset-if-external";
|
|
25
|
+
readonly owner?: "vite" | "node";
|
|
26
|
+
readonly transform?: "vite" | "registry" | "native-babel";
|
|
27
|
+
readonly reset?: "per-file" | "worker-resident";
|
|
28
|
+
readonly reason: NativeOwnershipReason;
|
|
29
|
+
readonly packages?: readonly string[];
|
|
30
|
+
readonly roots?: readonly string[];
|
|
31
|
+
readonly patterns?: readonly string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface NativeOwnershipManifest {
|
|
35
|
+
readonly version: 1;
|
|
36
|
+
readonly enforcement: "enforced" | "overridden-by-inline-all";
|
|
37
|
+
readonly projectRoot: string;
|
|
38
|
+
readonly projectDirs: readonly string[];
|
|
39
|
+
readonly reactNativeRoots: readonly string[];
|
|
40
|
+
readonly nodeTransformPackages: readonly string[];
|
|
41
|
+
readonly runtimeTransformAugmentations: readonly string[];
|
|
42
|
+
readonly identityResidentPackages: readonly string[];
|
|
43
|
+
readonly testRuntimeResidentPackages: readonly string[];
|
|
44
|
+
readonly rules: readonly NativeOwnershipRule[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface NativeOwnershipPolicy {
|
|
48
|
+
readonly enforcement: "enforced" | "overridden-by-inline-all";
|
|
49
|
+
readonly projectRoot: string;
|
|
50
|
+
readonly nodeTransformPackages: readonly string[];
|
|
51
|
+
readonly externalPatterns: readonly RegExp[];
|
|
52
|
+
readonly testEntryPatterns: readonly RegExp[];
|
|
53
|
+
readonly matchesNodeTransformedFile: (file: string) => boolean;
|
|
54
|
+
readonly isReactNativeFile: (file: string) => boolean;
|
|
55
|
+
/** Normalize a linked RN file to `/react-native/<relative path>` for boundary matching. */
|
|
56
|
+
readonly reactNativePathFor: (file: string) => string | null;
|
|
57
|
+
readonly decideFile: (file: string) => NativeOwnershipDecision;
|
|
58
|
+
readonly manifest: () => NativeOwnershipManifest;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface NativeOwnershipConflict {
|
|
62
|
+
readonly code: "NATIVE_MODULE_OWNER_CONFLICT";
|
|
63
|
+
readonly file: string;
|
|
64
|
+
readonly observedOwner: "vite" | "node";
|
|
65
|
+
readonly expectedOwner: "vite" | "node";
|
|
66
|
+
readonly decision: NativeOwnershipDecision;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface NativeOwnershipOptions {
|
|
70
|
+
projectRoot?: string;
|
|
71
|
+
explicitTransforms?: string[];
|
|
72
|
+
ecosystemPackages?: string[];
|
|
73
|
+
runtimeTransforms?: string[];
|
|
74
|
+
runtimeTransformAugmentations?: string[];
|
|
75
|
+
projectDirs?: string[];
|
|
76
|
+
reactNativeRoots?: string[];
|
|
77
|
+
serverDepsInlineAll?: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export declare const TEST_ENTRY_PATTERNS: readonly RegExp[];
|
|
81
|
+
export declare const RUNTIME_RESIDENT_PACKAGES: readonly string[];
|
|
82
|
+
export declare const RUNTIME_RESIDENT_PATH: RegExp;
|
|
83
|
+
export declare const TEST_RUNTIME_RESIDENT_PACKAGES: readonly string[];
|
|
84
|
+
export declare const TEST_RUNTIME_RESIDENT_PATH: RegExp;
|
|
85
|
+
|
|
86
|
+
export declare function isRuntimeResidentFile(file: string): boolean;
|
|
87
|
+
export declare function isTestRuntimeResidentFile(file: string): boolean;
|
|
88
|
+
export declare function createNativeOwnershipPolicy(
|
|
89
|
+
options?: NativeOwnershipOptions,
|
|
90
|
+
): NativeOwnershipPolicy;
|
|
91
|
+
export declare function findNativeOwnershipConflict(
|
|
92
|
+
policy: NativeOwnershipPolicy,
|
|
93
|
+
file: string,
|
|
94
|
+
observedOwner: "vite" | "node",
|
|
95
|
+
): NativeOwnershipConflict | null;
|
|
96
|
+
export declare function findNativeInlineConflicts(
|
|
97
|
+
policy: NativeOwnershipPolicy,
|
|
98
|
+
inline: unknown,
|
|
99
|
+
): string[];
|
|
100
|
+
export declare function parseNativeOwnershipManifest(
|
|
101
|
+
raw: string | unknown,
|
|
102
|
+
): NativeOwnershipManifest | null;
|
|
103
|
+
export declare function formatNativeOwnershipManifest(
|
|
104
|
+
manifest: NativeOwnershipManifest | string | unknown,
|
|
105
|
+
): string[];
|