vitest-native 0.6.1 → 0.8.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 +84 -0
- package/README.md +60 -13
- package/dist/{index-DGNLmGjq.d.mts → index-YjjYELG7.d.cts} +40 -3
- package/dist/{index-Dr3WvKWI.d.cts → index-vlK4zxuK.d.mts} +38 -5
- package/dist/index.cjs +12 -4
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +12 -4
- package/dist/jest-compat/setup.mjs +15 -0
- package/dist/native/compile-cache.mjs +40 -0
- package/dist/native/hooks.mjs +9 -2
- package/dist/native/loader.mjs +50 -5
- package/dist/native/resolve.mjs +18 -0
- package/dist/native/setup.mjs +39 -2
- package/dist/native/transform.mjs +5 -3
- package/dist/native/worker.mjs +4 -0
- package/dist/{preset-map-CNBOeqAu.cjs → preset-map-BY9zk9Pj.cjs} +4 -1
- package/dist/{preset-map-C1UwEald.mjs → preset-map-DCWaM08s.mjs} +4 -1
- package/dist/{presets-DVLDrPC-.mjs → presets-23FJ76O4.mjs} +641 -6
- package/dist/{presets-BPVVlfI2.cjs → presets-7eMfp7o2.cjs} +712 -5
- package/dist/presets.cjs +4 -1
- package/dist/presets.d.cts +2 -2
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/setup.cjs +24 -219
- package/dist/setup.mjs +15 -210
- package/docs/migrating-from-jest.md +3 -1
- package/package.json +11 -5
package/dist/native/resolve.mjs
CHANGED
|
@@ -22,12 +22,30 @@ function extensionsFor(platform) {
|
|
|
22
22
|
];
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
// Per-worker resolution cache: `${platform}\0${absBase}` → resolved path | null.
|
|
26
|
+
// Platform resolution is deterministic for a given on-disk layout, and Node's own
|
|
27
|
+
// module cache already dedupes most re-resolution; this dedupes the rest (distinct
|
|
28
|
+
// import edges resolving to the same base), so each base is scanned at most once per
|
|
29
|
+
// worker instead of running up to ~24 `existsSync` calls every time. Negative
|
|
30
|
+
// results are cached too. Lifetime is the worker process — like Vite's own
|
|
31
|
+
// resolution cache, a newly-added platform variant is picked up on the next restart.
|
|
32
|
+
const resolveCache = new Map();
|
|
33
|
+
|
|
25
34
|
/**
|
|
26
35
|
* Given an absolute base path with no extension (e.g. ".../Foo"), return the
|
|
27
36
|
* first existing platform variant (".../Foo.ios.tsx", etc.) or directory index,
|
|
28
37
|
* or null if none exist.
|
|
29
38
|
*/
|
|
30
39
|
export function resolvePlatformFile(absBase, platform = "ios") {
|
|
40
|
+
const key = platform + "\0" + absBase;
|
|
41
|
+
const cached = resolveCache.get(key);
|
|
42
|
+
if (cached !== undefined) return cached;
|
|
43
|
+
const resolved = scanPlatformFile(absBase, platform);
|
|
44
|
+
resolveCache.set(key, resolved);
|
|
45
|
+
return resolved;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function scanPlatformFile(absBase, platform) {
|
|
31
49
|
const extensions = extensionsFor(platform);
|
|
32
50
|
for (const ext of extensions) {
|
|
33
51
|
if (fs.existsSync(absBase + ext)) return absBase + ext;
|
package/dist/native/setup.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import path from "node:path";
|
|
|
6
6
|
import { expect, vi } from "vitest";
|
|
7
7
|
import { installGlobals } from "./globals.mjs";
|
|
8
8
|
import { installRequireHooks } from "./hooks.mjs";
|
|
9
|
+
import { enableV8CompileCache } from "./compile-cache.mjs";
|
|
9
10
|
import * as presetFactories from "../presets.mjs";
|
|
10
11
|
import { animatedMatchers } from "../matchers.mjs";
|
|
11
12
|
import { serializer as rnSerializer } from "../serializer.mjs";
|
|
@@ -72,6 +73,13 @@ try {
|
|
|
72
73
|
if (process.env.VITEST_NATIVE_PRESET_NAMES)
|
|
73
74
|
presetNames = JSON.parse(process.env.VITEST_NATIVE_PRESET_NAMES);
|
|
74
75
|
} catch {}
|
|
76
|
+
// Per-preset config (preset name → JSON config), e.g. navigation route params.
|
|
77
|
+
// Presets are rebuilt here from their name, so any factory options come via env.
|
|
78
|
+
let presetConfig = {};
|
|
79
|
+
try {
|
|
80
|
+
if (process.env.VITEST_NATIVE_PRESET_CONFIG)
|
|
81
|
+
presetConfig = JSON.parse(process.env.VITEST_NATIVE_PRESET_CONFIG);
|
|
82
|
+
} catch {}
|
|
75
83
|
|
|
76
84
|
// Discover preset module (package) names + their static export lists WITHOUT
|
|
77
85
|
// building the mocks yet — building can lazily touch react-native, so the require
|
|
@@ -81,13 +89,17 @@ const presetExports = {}; // pkg -> string[] (named exports, for the ESM loader)
|
|
|
81
89
|
for (const name of presetNames) {
|
|
82
90
|
const factory = presetFactories[name];
|
|
83
91
|
if (typeof factory !== "function") continue;
|
|
84
|
-
const preset = factory();
|
|
92
|
+
const preset = factory(presetConfig[name]);
|
|
85
93
|
for (const [pkg, mod] of Object.entries(preset.modules)) {
|
|
86
94
|
presetDefs.push({ pkg, mod, presetName: preset.name });
|
|
87
95
|
presetExports[pkg] = mod.exports || [];
|
|
88
96
|
}
|
|
89
97
|
}
|
|
90
98
|
|
|
99
|
+
// Enable the V8 compile cache before RN is compiled (it loads when the test file
|
|
100
|
+
// imports react-native, after this setup runs) so its bytecode is cached to disk
|
|
101
|
+
// and reused on the next file/worker/run. Covers the stock (non-hot) path.
|
|
102
|
+
enableV8CompileCache();
|
|
91
103
|
installGlobals();
|
|
92
104
|
// RNTL 13+ auto-registers matchers through the global expect when imported.
|
|
93
105
|
// Expose Vitest's expect only when the consumer has not enabled globals.
|
|
@@ -106,7 +118,7 @@ if (typeof globalThis.expect === "undefined") {
|
|
|
106
118
|
if (!globalThis.__vitest_native_loader_registered) {
|
|
107
119
|
globalThis.__vitest_native_loader_registered = true;
|
|
108
120
|
register("./loader.mjs", import.meta.url, {
|
|
109
|
-
data: { projectRoot, platform, reactNativeVersion, transformPkgs, presetExports },
|
|
121
|
+
data: { projectRoot, platform, reactNativeVersion, transformPkgs, presetExports, assetExts },
|
|
110
122
|
});
|
|
111
123
|
}
|
|
112
124
|
installRequireHooks(projectRoot, transformPkgs, platform, reactNativeVersion, assetExts);
|
|
@@ -214,6 +226,31 @@ try {
|
|
|
214
226
|
}
|
|
215
227
|
}
|
|
216
228
|
|
|
229
|
+
// --- RNTL 14: register RCTVirtualText as a text host ---
|
|
230
|
+
// RNTL 14 reconciles with `test-renderer`, which enforces that string children
|
|
231
|
+
// live under a "text" host component (the names in RNTL's HOST_TEXT_NAMES) and
|
|
232
|
+
// otherwise throws "Text strings must be rendered within a <Text> component".
|
|
233
|
+
// Real RN renders a NESTED <Text> as the host "RCTVirtualText", which RNTL's list
|
|
234
|
+
// (['Text', 'RCTText']) omits — so any composite/nested <Text> crashes under the
|
|
235
|
+
// native engine. (Jest's preset never hits this: it mocks Text to a single flat
|
|
236
|
+
// "Text" host, hiding the real RCTText/RCTVirtualText split.) Add RCTVirtualText so
|
|
237
|
+
// nested text reconciles and getByText matches across nested <Text>, matching how
|
|
238
|
+
// RN actually renders. The helper is a leaf module (no RNTL main-module init), so
|
|
239
|
+
// requiring it here is free of the evaluation-order hazards noted in the hot-reset
|
|
240
|
+
// block. Gated on the list existing: a no-op for RNTL <=13 and the mock engine.
|
|
241
|
+
try {
|
|
242
|
+
const hcn = req("@testing-library/react-native/dist/helpers/host-component-names");
|
|
243
|
+
if (Array.isArray(hcn?.HOST_TEXT_NAMES) && !hcn.HOST_TEXT_NAMES.includes("RCTVirtualText")) {
|
|
244
|
+
hcn.HOST_TEXT_NAMES.push("RCTVirtualText");
|
|
245
|
+
}
|
|
246
|
+
} catch (e) {
|
|
247
|
+
if (diagnostics) {
|
|
248
|
+
console.log(
|
|
249
|
+
`[vitest-native] (native) could not register RCTVirtualText text host: ${e?.message}`,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
217
254
|
expect.extend(animatedMatchers);
|
|
218
255
|
expect.addSnapshotSerializer(rnSerializer);
|
|
219
256
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// Flow-strips a React Native source file via the project's @react-native/babel-preset
|
|
2
|
-
// (the only transformer that lowers RN's `component` syntax).
|
|
3
|
-
//
|
|
4
|
-
//
|
|
2
|
+
// (the only transformer that lowers RN's `component` syntax). Used by BOTH the loader
|
|
3
|
+
// hook (import) and the require hook, which run in separate threads: each gets its own
|
|
4
|
+
// instance of this module, so the in-memory `mem` cache below is per-thread (no shared
|
|
5
|
+
// mutable state to race on). The disk cache — keyed by path + mtime + size + preset
|
|
6
|
+
// version — is the layer shared across them.
|
|
5
7
|
import { createRequire } from "node:module";
|
|
6
8
|
import path from "node:path";
|
|
7
9
|
import fs from "node:fs";
|
package/dist/native/worker.mjs
CHANGED
|
@@ -16,6 +16,7 @@ import { init, runBaseTests, setupEnvironment } from "vitest/worker";
|
|
|
16
16
|
import { installGlobals } from "./globals.mjs";
|
|
17
17
|
import { installRequireHooks } from "./hooks.mjs";
|
|
18
18
|
import { installHotReset } from "./reset.mjs";
|
|
19
|
+
import { enableV8CompileCache } from "./compile-cache.mjs";
|
|
19
20
|
|
|
20
21
|
if (isMainThread || !parentPort) {
|
|
21
22
|
throw new Error("[vitest-native] hot worker entry must run in node:worker_threads");
|
|
@@ -52,6 +53,9 @@ if (diagnostics) {
|
|
|
52
53
|
// guarded; doing them at boot lets RN preload BEFORE the globals baseline and
|
|
53
54
|
// listener tracking in reset.mjs, so RN's own boot state is preserved across
|
|
54
55
|
// per-file resets rather than wrongly torn down with test pollution.
|
|
56
|
+
// Enable the V8 compile cache before any RN module compiles, so the resident
|
|
57
|
+
// graph's bytecode is cached to disk for the next worker/run.
|
|
58
|
+
enableV8CompileCache();
|
|
55
59
|
installGlobals();
|
|
56
60
|
installRequireHooks(projectRoot, transformPkgs, platform, reactNativeVersion, assetExts);
|
|
57
61
|
try {
|
|
@@ -20,7 +20,10 @@ const AUTO_DETECT_PRESETS = {
|
|
|
20
20
|
"react-native-svg": "svg",
|
|
21
21
|
"react-native-webview": "webview",
|
|
22
22
|
"@react-native-vector-icons/common": "vectorIcons",
|
|
23
|
-
"react-native-vector-icons": "vectorIcons"
|
|
23
|
+
"react-native-vector-icons": "vectorIcons",
|
|
24
|
+
"@shopify/flash-list": "flashList",
|
|
25
|
+
"@gorhom/bottom-sheet": "bottomSheet",
|
|
26
|
+
"react-native-keyboard-controller": "keyboardController"
|
|
24
27
|
};
|
|
25
28
|
//#endregion
|
|
26
29
|
Object.defineProperty(exports, "AUTO_DETECT_PRESETS", {
|
|
@@ -20,7 +20,10 @@ const AUTO_DETECT_PRESETS = {
|
|
|
20
20
|
"react-native-svg": "svg",
|
|
21
21
|
"react-native-webview": "webview",
|
|
22
22
|
"@react-native-vector-icons/common": "vectorIcons",
|
|
23
|
-
"react-native-vector-icons": "vectorIcons"
|
|
23
|
+
"react-native-vector-icons": "vectorIcons",
|
|
24
|
+
"@shopify/flash-list": "flashList",
|
|
25
|
+
"@gorhom/bottom-sheet": "bottomSheet",
|
|
26
|
+
"react-native-keyboard-controller": "keyboardController"
|
|
24
27
|
};
|
|
25
28
|
//#endregion
|
|
26
29
|
export { AUTO_DETECT_PRESETS as t };
|