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.
@@ -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;
@@ -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). Disk-cached by
3
- // path + mtime + size + preset version. Used by BOTH the loader hook (import) and
4
- // the require hook — they run in separate threads, so this module is stateless.
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";
@@ -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 };