vitest-native 0.7.0 → 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 CHANGED
@@ -1,5 +1,46 @@
1
1
  # vitest-native
2
2
 
3
+ ## 0.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 067e2aa: Add built-in presets for `@shopify/flash-list`, `@gorhom/bottom-sheet`, and `react-native-keyboard-controller`.
8
+
9
+ These libraries rely on native runtimes (a native recycler, reanimated worklets, and keyboard native modules) that cannot run under Node, so before this they had to be mocked by hand. Each is now auto-detected when installed and shadowed by a self-contained preset under both engines: `FlashList` renders its data through `renderItem` so rows stay queryable, the bottom-sheet containers render their children through real React Native with no-op imperative refs, and the keyboard-controller containers render their children while `KeyboardController` and the reanimated-backed hooks return inert handles.
10
+
11
+ - b31e3d9: Support `@testing-library/react-native` 14 alongside 12 and 13.
12
+
13
+ RNTL 14 made `render`, `fireEvent`, and `act` asynchronous and reconciles with the new
14
+ `test-renderer` (replacing `react-test-renderer`). Two changes make the native engine work
15
+ across the full supported peer range (`>=12 <15`) from a single setup:
16
+
17
+ - Register `RCTVirtualText` as a text host under the native engine. Real React Native renders a
18
+ nested `<Text>` as the host `RCTVirtualText`, which RNTL 14's `test-renderer` did not recognize
19
+ as text — so any composite or nested `<Text>` threw "Text strings must be rendered within a
20
+ `<Text>` component". Nested and composite text now render and match correctly.
21
+ - The engine itself is RNTL-version agnostic; the only caller-visible difference is that RNTL 14's
22
+ `render`/`fireEvent`/`act` must be awaited. Awaiting them is back-compatible with RNTL 12/13,
23
+ where the calls are synchronous.
24
+
25
+ CI now exercises RNTL 12, 13, and 14. Note that RNTL 14 requires Node >= 22.13; on Node 20, use
26
+ RNTL 13.
27
+
28
+ ### Patch Changes
29
+
30
+ - 489a536: Scope React Native path detection to `node_modules`. The native engine identified
31
+ React Native package files with the regex `/[\\/]react-native[\\/]/`, which also
32
+ matched any project checked out under a directory named `react-native` (for example
33
+ `/home/runner/work/react-native/react-native/` in CI for a repo named `react-native`).
34
+ Every project file then matched, so `.tsx` test files were externalized and sent raw
35
+ to Node (`Unknown file extension ".tsx"`) and `vi.mock()` calls stopped hoisting.
36
+ The matchers in `apply.ts`, `loader.mjs`, and `hooks.mjs` now require a
37
+ `node_modules/` segment, which still matches real RN and `@react-native/*` packages
38
+ (including pnpm-nested layouts) without false-matching project paths. Reported and fixed
39
+ by [@Doko-Demo-Doa](https://github.com/Doko-Demo-Doa) (#50).
40
+ - 5eaf8cb: Cache the React Native graph's compiled bytecode under the native engine.
41
+
42
+ With per-file isolation (`engine: 'native'`, the default), React Native's module graph is re-instantiated for every test file, recompiling its source to V8 bytecode each time. The native engine now enables Node's on-disk compile cache (Node 22.8+) before React Native is loaded, so subsequent compilations across files, workers, and runs reuse cached bytecode. Measured on a 100-file suite (single worker), this reduced cold time by ~7% and warm time by ~7-18% with tighter run-to-run variance and no change in memory use. It is a no-op on Node versions without the compile-cache API.
43
+
3
44
  ## 0.7.0
4
45
 
5
46
  ### Minor Changes
package/README.md CHANGED
@@ -451,19 +451,23 @@ export default defineConfig({
451
451
 
452
452
  ### Available Presets
453
453
 
454
- | Import | Function |
455
- | --------------------------- | ----------------------------------------- |
456
- | `presets.reanimated()` | react-native-reanimated |
457
- | `presets.gestureHandler()` | react-native-gesture-handler |
458
- | `presets.safeAreaContext()` | react-native-safe-area-context |
459
- | `presets.navigation()` | @react-navigation/native |
460
- | `presets.asyncStorage()` | @react-native-async-storage/async-storage |
461
- | `presets.screens()` | react-native-screens |
462
- | `presets.expo()` | Expo modules |
463
- | `presets.deviceInfo()` | react-native-device-info |
464
- | `presets.mmkv()` | react-native-mmkv |
465
- | `presets.svg()` | react-native-svg |
466
- | `presets.webview()` | react-native-webview |
454
+ | Import | Function |
455
+ | ------------------------------ | ----------------------------------------- |
456
+ | `presets.reanimated()` | react-native-reanimated |
457
+ | `presets.gestureHandler()` | react-native-gesture-handler |
458
+ | `presets.safeAreaContext()` | react-native-safe-area-context |
459
+ | `presets.navigation()` | @react-navigation/native |
460
+ | `presets.asyncStorage()` | @react-native-async-storage/async-storage |
461
+ | `presets.screens()` | react-native-screens |
462
+ | `presets.expo()` | Expo modules |
463
+ | `presets.deviceInfo()` | react-native-device-info |
464
+ | `presets.mmkv()` | react-native-mmkv |
465
+ | `presets.svg()` | react-native-svg |
466
+ | `presets.webview()` | react-native-webview |
467
+ | `presets.vectorIcons()` | react-native-vector-icons |
468
+ | `presets.flashList()` | @shopify/flash-list |
469
+ | `presets.bottomSheet()` | @gorhom/bottom-sheet |
470
+ | `presets.keyboardController()` | react-native-keyboard-controller |
467
471
 
468
472
  Presets apply under **both** engines: the mock engine and `engine: 'native'` (where they shadow each
469
473
  library's native runtime — worklets, native modules — exactly as Jest does, while the surrounding tree
@@ -1,5 +1,7 @@
1
1
  import { Mock } from "vitest";
2
2
 
3
+ //#region \0rolldown/runtime.js
4
+ //#endregion
3
5
  //#region src/types.d.ts
4
6
  /** Static methods available on the Image component mock. */
5
7
  interface ImageStatic {
@@ -350,8 +352,17 @@ declare function webview(): Preset;
350
352
  //#endregion
351
353
  //#region src/presets/vector-icons.d.ts
352
354
  declare function vectorIcons(): Preset;
355
+ //#endregion
356
+ //#region src/presets/flash-list.d.ts
357
+ declare function flashList(): Preset;
358
+ //#endregion
359
+ //#region src/presets/bottom-sheet.d.ts
360
+ declare function bottomSheet(): Preset;
361
+ //#endregion
362
+ //#region src/presets/keyboard-controller.d.ts
363
+ declare function keyboardController(): Preset;
353
364
  declare namespace index_d_exports {
354
- export { asyncStorage, deviceInfo, expo, gestureHandler, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview };
365
+ export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview };
355
366
  }
356
367
  //#endregion
357
- export { mmkv as a, screens as c, safeAreaContext as d, gestureHandler as f, VitestNativeOptions as g, ReactNativeMock as h, svg as i, asyncStorage as l, Preset as m, vectorIcons as n, deviceInfo as o, reanimated as p, webview as r, expo as s, index_d_exports as t, navigation as u };
368
+ export { Preset as _, vectorIcons as a, mmkv as c, screens as d, asyncStorage as f, reanimated as g, gestureHandler as h, flashList as i, deviceInfo as l, safeAreaContext as m, keyboardController as n, webview as o, navigation as p, bottomSheet as r, svg as s, index_d_exports as t, expo as u, ReactNativeMock as v, VitestNativeOptions as y };
@@ -1,7 +1,5 @@
1
1
  import { Mock } from "vitest";
2
2
 
3
- //#region \0rolldown/runtime.js
4
- //#endregion
5
3
  //#region src/types.d.ts
6
4
  /** Static methods available on the Image component mock. */
7
5
  interface ImageStatic {
@@ -352,8 +350,17 @@ declare function webview(): Preset;
352
350
  //#endregion
353
351
  //#region src/presets/vector-icons.d.ts
354
352
  declare function vectorIcons(): Preset;
353
+ //#endregion
354
+ //#region src/presets/flash-list.d.ts
355
+ declare function flashList(): Preset;
356
+ //#endregion
357
+ //#region src/presets/bottom-sheet.d.ts
358
+ declare function bottomSheet(): Preset;
359
+ //#endregion
360
+ //#region src/presets/keyboard-controller.d.ts
361
+ declare function keyboardController(): Preset;
355
362
  declare namespace index_d_exports {
356
- export { asyncStorage, deviceInfo, expo, gestureHandler, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview };
363
+ export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview };
357
364
  }
358
365
  //#endregion
359
- export { mmkv as a, screens as c, safeAreaContext as d, gestureHandler as f, VitestNativeOptions as g, ReactNativeMock as h, svg as i, asyncStorage as l, Preset as m, vectorIcons as n, deviceInfo as o, reanimated as p, webview as r, expo as s, index_d_exports as t, navigation as u };
366
+ export { Preset as _, vectorIcons as a, mmkv as c, screens as d, asyncStorage as f, reanimated as g, gestureHandler as h, flashList as i, deviceInfo as l, safeAreaContext as m, keyboardController as n, webview as o, navigation as p, bottomSheet as r, svg as s, index_d_exports as t, expo as u, ReactNativeMock as v, VitestNativeOptions as y };
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_chunk = require("./chunk-CZWwpsFl.cjs");
3
- const require_preset_map = require("./preset-map-CNBOeqAu.cjs");
4
- const require_presets = require("./presets-BTG8fiNF.cjs");
3
+ const require_preset_map = require("./preset-map-BY9zk9Pj.cjs");
4
+ const require_presets = require("./presets-7eMfp7o2.cjs");
5
5
  let node_url = require("node:url");
6
6
  let node_path = require("node:path");
7
7
  node_path = require_chunk.__toESM(node_path, 1);
@@ -167,8 +167,8 @@ function nativeEngineConfig(setupFilePath, env, extensions, transformPkgs = [],
167
167
  runner: hot.runnerPath
168
168
  } : { pool: "threads" },
169
169
  server: { deps: { external: [
170
- /[\\/]react-native[\\/]/,
171
- /[\\/]@react-native[\\/]/,
170
+ /[\\/]node_modules[\\/]react-native[\\/]/,
171
+ /[\\/]node_modules[\\/]@react-native[\\/]/,
172
172
  ...extraExternal
173
173
  ] } }
174
174
  }
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { g as VitestNativeOptions, h as ReactNativeMock, m as Preset, t as index_d_exports } from "./index-BgzErxHj.cjs";
1
+ import { _ as Preset, t as index_d_exports, v as ReactNativeMock, y as VitestNativeOptions } from "./index-YjjYELG7.cjs";
2
2
  import { Plugin } from "vite";
3
3
 
4
4
  //#region src/plugin.d.ts
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { g as VitestNativeOptions, h as ReactNativeMock, m as Preset, t as index_d_exports } from "./index-DbfKy_bm.mjs";
1
+ import { _ as Preset, t as index_d_exports, v as ReactNativeMock, y as VitestNativeOptions } from "./index-vlK4zxuK.mjs";
2
2
  import { Plugin } from "vite";
3
3
 
4
4
  //#region src/plugin.d.ts
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { t as AUTO_DETECT_PRESETS } from "./preset-map-C1UwEald.mjs";
2
- import { t as presets_exports } from "./presets-BFuAMwYQ.mjs";
1
+ import { t as AUTO_DETECT_PRESETS } from "./preset-map-DCWaM08s.mjs";
2
+ import { t as presets_exports } from "./presets-23FJ76O4.mjs";
3
3
  import { createRequire } from "node:module";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import path from "node:path";
@@ -162,8 +162,8 @@ function nativeEngineConfig(setupFilePath, env, extensions, transformPkgs = [],
162
162
  runner: hot.runnerPath
163
163
  } : { pool: "threads" },
164
164
  server: { deps: { external: [
165
- /[\\/]react-native[\\/]/,
166
- /[\\/]@react-native[\\/]/,
165
+ /[\\/]node_modules[\\/]react-native[\\/]/,
166
+ /[\\/]node_modules[\\/]@react-native[\\/]/,
167
167
  ...extraExternal
168
168
  ] } }
169
169
  }
@@ -0,0 +1,40 @@
1
+ // V8 compile-cache for the externalized React Native graph.
2
+ //
3
+ // RN is loaded through Node (externalized), and our require/loader hooks compile
4
+ // each RN module's Flow-stripped source with `mod._compile` / a synthetic module
5
+ // source. The transform disk cache (transform.mjs) means Babel runs once per RN
6
+ // version — but V8 still re-parses and re-compiles that source to bytecode on
7
+ // every fresh module graph. Under `engine:'native'` (isolate:true) RN's ~240-file
8
+ // graph is re-instantiated per test FILE, so that V8 compile is paid over and
9
+ // over. Node's on-disk compile cache (Node 22.8+) keys bytecode by source content
10
+ // + Node version and is shared process-wide, so the second compile of any RN
11
+ // module — the next file, the next worker, the next run — loads bytecode instead
12
+ // of recompiling. Verified to apply to `mod._compile`, which is how the hooks load
13
+ // RN. No-op on Node < 22.8 (the API is absent) and harmless when caching is off.
14
+ import nodeModule from "node:module";
15
+ import os from "node:os";
16
+ import path from "node:path";
17
+
18
+ let _enabled = false;
19
+
20
+ /**
21
+ * Enable Node's persistent V8 compile cache for this worker/thread. Idempotent and
22
+ * guarded per realm. Colocated under the `vitest-native-cache` tmp prefix so it
23
+ * shares the transform cache's lifecycle (cleared with it on a cold run, reused on
24
+ * warm). Must be called before RN's modules are first compiled.
25
+ */
26
+ export function enableV8CompileCache() {
27
+ if (_enabled) return;
28
+ _enabled = true;
29
+ const enable = nodeModule.enableCompileCache;
30
+ if (typeof enable !== "function") return; // Node < 22.8: feature absent
31
+ try {
32
+ enable.call(nodeModule, path.join(os.tmpdir(), "vitest-native-cache-v8"));
33
+ // No atomic-write handling needed (unlike the transform cache, which writes
34
+ // executable source): Node CRC32-validates each compile-cache entry on read,
35
+ // so a torn or raced write from concurrent workers is a cache miss and a
36
+ // recompile, never a corruption-induced failure.
37
+ } catch {
38
+ // Read-only tmp, unsupported platform, etc. — caching is a pure optimization.
39
+ }
40
+ }
@@ -8,7 +8,7 @@ import { boundarySourceFor } from "./boundary.mjs";
8
8
  import { resolvePlatformFile } from "./resolve.mjs";
9
9
  import { buildPkgMatcher } from "./match.mjs";
10
10
 
11
- const RN_PATH = /[\\/](react-native|@react-native)[\\/]/;
11
+ const RN_PATH = /[\\/]node_modules[\\/](react-native|@react-native)[\\/]/;
12
12
  // Any file under a node_modules directory. Platform-extension resolution
13
13
  // (`.native.js` etc.) applies to every node_modules package, not just RN — matching
14
14
  // Metro, which resolves platform variants project-wide. See loader.mjs for the
@@ -8,7 +8,7 @@ import { boundarySourceFor } from "./boundary.mjs";
8
8
  import { resolvePlatformFile } from "./resolve.mjs";
9
9
  import { buildPkgMatcher } from "./match.mjs";
10
10
 
11
- const RN_PATH = /[\\/](react-native|@react-native)[\\/]/;
11
+ const RN_PATH = /[\\/]node_modules[\\/](react-native|@react-native)[\\/]/;
12
12
  // Any file living under a node_modules directory. Platform-extension resolution
13
13
  // (`.native.js` etc.) applies to every node_modules package, not just RN, matching
14
14
  // Metro — which resolves platform variants project-wide. (Without this, e.g.
@@ -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";
@@ -95,6 +96,10 @@ for (const name of presetNames) {
95
96
  }
96
97
  }
97
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();
98
103
  installGlobals();
99
104
  // RNTL 13+ auto-registers matchers through the global expect when imported.
100
105
  // Expose Vitest's expect only when the consumer has not enabled globals.
@@ -221,6 +226,31 @@ try {
221
226
  }
222
227
  }
223
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
+
224
254
  expect.extend(animatedMatchers);
225
255
  expect.addSnapshotSerializer(rnSerializer);
226
256
 
@@ -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 };