vitest-native 0.13.0 → 0.14.0-rc.10

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.
Files changed (117) hide show
  1. package/CHANGELOG.md +746 -0
  2. package/README.md +205 -88
  3. package/dist/cli.cjs +1478 -145
  4. package/dist/cli.d.cts +2 -3
  5. package/dist/cli.d.mts +2 -3
  6. package/dist/cli.mjs +1475 -142
  7. package/dist/context.d.mts +40 -0
  8. package/dist/context.mjs +238 -0
  9. package/dist/errors.d.mts +19 -0
  10. package/dist/helpers.cjs +6 -9
  11. package/dist/helpers.d.cts +8 -9
  12. package/dist/helpers.d.mts +8 -9
  13. package/dist/helpers.mjs +6 -9
  14. package/dist/host-D3wzmsw8.cjs +1296 -0
  15. package/dist/host-DCPzgC3D.mjs +1180 -0
  16. package/dist/{index-CPGq6p95.d.cts → index-CPEAYjaQ.d.mts} +88 -19
  17. package/dist/{index-CmETArCM.d.mts → index-D5cOy_iY.d.cts} +89 -18
  18. package/dist/index.d.mts +4 -4
  19. package/dist/index.mjs +1366 -210
  20. package/dist/jest-compat/aliases.d.mts +5 -0
  21. package/dist/jest-compat/aliases.mjs +65 -0
  22. package/dist/jest-compat/caller.mjs +53 -0
  23. package/dist/jest-compat/error-equality.mjs +127 -0
  24. package/dist/jest-compat/interop.mjs +78 -4
  25. package/dist/jest-compat/jest-globals.mjs +5 -2
  26. package/dist/jest-compat/jest-object.mjs +348 -0
  27. package/dist/jest-compat/node-registry.mjs +489 -0
  28. package/dist/jest-compat/setup.mjs +167 -66
  29. package/dist/jest-compat.cjs +35 -5
  30. package/dist/jest-compat.d.cts +9 -5
  31. package/dist/jest-compat.d.mts +9 -5
  32. package/dist/jest-compat.mjs +35 -3
  33. package/dist/matchers.d.cts +9 -8
  34. package/dist/matchers.d.mts +9 -8
  35. package/dist/memory-BFxB285Z.mjs +137 -0
  36. package/dist/memory-bBnzadf6.cjs +155 -0
  37. package/dist/native/assets.d.mts +39 -0
  38. package/dist/native/assets.mjs +431 -0
  39. package/dist/native/babel-preset.d.mts +14 -0
  40. package/dist/native/babel-preset.mjs +56 -0
  41. package/dist/native/boundary.mjs +71 -8
  42. package/dist/native/bounded-process.mjs +165 -0
  43. package/dist/native/compile-cache.mjs +21 -9
  44. package/dist/native/expo-module-mocks.mjs +432 -0
  45. package/dist/native/expo-module-table-57.mjs +1925 -0
  46. package/dist/native/expo-module-table-58.mjs +2045 -0
  47. package/dist/native/expo-module-tables.mjs +6 -0
  48. package/dist/native/expo-native-modules.mjs +66 -0
  49. package/dist/native/export-condition-recovery-preload.mjs +5 -0
  50. package/dist/native/export-condition-recovery.mjs +195 -0
  51. package/dist/native/globals.mjs +289 -9
  52. package/dist/native/hooks.mjs +392 -114
  53. package/dist/native/loader.mjs +346 -75
  54. package/dist/native/match.d.mts +13 -0
  55. package/dist/native/match.mjs +127 -2
  56. package/dist/native/metro-profile-compiler.mjs +234 -0
  57. package/dist/native/metro-profile.mjs +115 -0
  58. package/dist/native/module-reset.mjs +172 -6
  59. package/dist/native/native-modules.d.mts +15 -0
  60. package/dist/native/native-modules.mjs +228 -0
  61. package/dist/native/nitro.mjs +60 -0
  62. package/dist/native/ownership.d.mts +105 -0
  63. package/dist/native/ownership.mjs +474 -0
  64. package/dist/native/registry-compiler.mjs +43 -0
  65. package/dist/native/registry-process.mjs +124 -0
  66. package/dist/native/registry.mjs +197 -39
  67. package/dist/native/reset.mjs +334 -135
  68. package/dist/native/resolve.d.mts +20 -3
  69. package/dist/native/resolve.mjs +103 -17
  70. package/dist/native/rntl-hooks.mjs +227 -0
  71. package/dist/native/rntl-text-host.mjs +64 -0
  72. package/dist/native/runner.mjs +33 -1
  73. package/dist/native/setup.mjs +284 -96
  74. package/dist/native/state-manifest.d.mts +22 -0
  75. package/dist/native/state-manifest.mjs +138 -0
  76. package/dist/native/transform.d.mts +2 -0
  77. package/dist/native/transform.mjs +270 -47
  78. package/dist/native/tsconfig-paths.d.mts +5 -0
  79. package/dist/native/tsconfig-paths.mjs +163 -0
  80. package/dist/native/worker.mjs +151 -40
  81. package/dist/pool-DUOe-n1k.mjs +125 -0
  82. package/dist/pool-DaTJRKJb.cjs +127 -0
  83. package/dist/preset-map-BlBriTA0.mjs +197 -0
  84. package/dist/preset-map-jSFsNy1Q.cjs +241 -0
  85. package/dist/preset-modules-Bt6k_Wdw.mjs +31 -0
  86. package/dist/preset-modules-C7gu3bPn.cjs +48 -0
  87. package/dist/preset-modules.d.mts +16 -0
  88. package/dist/preset-modules.mjs +92 -0
  89. package/dist/preset-ready.mjs +9 -0
  90. package/dist/{presets-CoNf4ByH.cjs → presets-DH7fC0YV.cjs} +1125 -102
  91. package/dist/{presets-DGProTaA.mjs → presets-DuXiUNAx.mjs} +1107 -103
  92. package/dist/presets.d.mts +2 -2
  93. package/dist/presets.mjs +1 -2
  94. package/dist/report-error.d.mts +20 -0
  95. package/dist/report-error.mjs +145 -0
  96. package/dist/rntl-matchers.d.ts +58 -12
  97. package/dist/rntl-support.d.mts +12 -0
  98. package/dist/rntl-support.mjs +189 -0
  99. package/dist/serializer.cjs +4 -2
  100. package/dist/serializer.d.cts +3 -4
  101. package/dist/serializer.d.mts +3 -4
  102. package/dist/serializer.mjs +4 -2
  103. package/dist/setup.mjs +463 -97
  104. package/dist/worker-vitest-BGPo0ek6.mjs +49 -0
  105. package/dist/worker-vitest-BfiQB8ND.cjs +62 -0
  106. package/docs/hot-default-design.md +181 -88
  107. package/docs/migrating-from-jest.md +74 -16
  108. package/docs/migrating-from-vitest-react-native.md +5 -5
  109. package/docs/release-readiness.md +55 -15
  110. package/docs/versioning.md +31 -22
  111. package/package.json +122 -98
  112. package/dist/ecosystem-CEIF_-jq.cjs +0 -659
  113. package/dist/ecosystem-Duw40-mO.mjs +0 -597
  114. package/dist/pool-BU-1ecIF.cjs +0 -122
  115. package/dist/pool-Bcxn9a3I.mjs +0 -118
  116. package/dist/preset-map-BoG19dLa.cjs +0 -35
  117. package/dist/preset-map-DuX0ocdn.mjs +0 -30
package/README.md CHANGED
@@ -4,9 +4,11 @@
4
4
 
5
5
  Run your React Native tests under Vitest, against **real React Native** — the same JavaScript that ships in your app, with only the native-module boundary mocked. That is the default and the point of the project. A lightweight pure-JS mock engine is available as an opt-in for fast, RN-free unit tests.
6
6
 
7
- > **Beta.** The release-supported native engine is validated across React Native 0.81–0.86,
8
- > Vite 6–8, Vitest 4, RNTL 12–14, bare apps, Expo 56, and hoisted monorepos. The optional
9
- > hot runtime remains experimental because it uses Vitest's experimental custom-pool APIs.
7
+ > **Beta.** The release-supported native engine is validated across React Native 0.81–0.88,
8
+ > Vite 6–8, Vitest 4–5, RNTL 12–14, bare apps, Expo SDK 58 and 57 (including expo-router's own testing
9
+ > library), and hoisted monorepos. The hot
10
+ > runtime (opt-in with `hotRuntime: 'auto'`) is marked experimental because it uses Vitest's
11
+ > experimental custom-pool APIs.
10
12
  >
11
13
  > Maintained successor to [`vitest-community/vitest-react-native`](https://github.com/vitest-community/vitest-react-native) — same core idea (externalize RN, run its real JS under Node), rebuilt for modern Vitest (4+). Coming from it? See [Migrating from `vitest-react-native`](#migrating-from-vitest-react-native).
12
14
 
@@ -29,7 +31,7 @@ Run your React Native tests under Vitest, against **real React Native** — the
29
31
  - [Animated Matchers (Reanimated)](#animated-matchers-reanimated)
30
32
  - [Snapshot Serializer](#snapshot-serializer)
31
33
  - [Platform Extensions](#platform-extensions)
32
- - [Asset Stubs](#asset-stubs)
34
+ - [Assets](#assets)
33
35
  - [Diagnostics](#diagnostics)
34
36
  - [Support Policy](#support-policy)
35
37
  - [Requirements](#requirements)
@@ -70,7 +72,9 @@ pnpm add -D vitest-native
70
72
  ```
71
73
 
72
74
  A real React Native project already provides what the native engine needs —
73
- `react-native`, `@react-native/babel-preset`, and `@babel/core`. Add
75
+ `react-native`, `@react-native/babel-preset`, and `@babel/core`. From Expo SDK 56,
76
+ `babel-preset-expo` no longer depends on `@react-native/babel-preset`, and the engine compiles
77
+ with Expo's own preset instead, as Metro does. Add
74
78
  `@testing-library/react-native` if you don't have it yet (the examples below use it):
75
79
 
76
80
  ```bash
@@ -117,8 +121,14 @@ test("renders a greeting", async () => {
117
121
 
118
122
  > Prefer Jest-style globals (`test`/`expect` without imports)? Set
119
123
  > `test: { globals: true }` in your Vitest config. vitest-native does not enable the full
120
- > globals mode; it only exposes `expect` when RNTL needs it to register matchers.
121
- > Awaiting `render` works with RNTL 12–13 and is required by RNTL 14.
124
+ > globals mode; it only exposes `expect` when RNTL needs it to register matchers, and
125
+ > registers RNTL's automatic cleanup after each test, which RNTL itself does only when
126
+ > `afterEach` is a global. That cleanup runs after the file's own `afterEach` hooks, as
127
+ > RNTL's does under Vitest with `globals: true` (Jest runs it first). Without the
128
+ > [jest-compat layer](https://danfry1.github.io/vitest-native/guide/jest-compat), RNTL cannot
129
+ > see `vi.useFakeTimers()`, so `waitFor` and `findBy*` do not advance the fake clock: use
130
+ > jest-compat, or advance it yourself (`await vi.advanceTimersByTimeAsync(ms)`). Awaiting
131
+ > `render` works with RNTL 12–13 and is required by RNTL 14.
122
132
 
123
133
  Run it:
124
134
 
@@ -146,7 +156,7 @@ export default defineConfig({
146
156
  diagnostics: false, // Enable verbose logging
147
157
  assetExts: [".lottie"], // Additional asset extensions to stub
148
158
  transform: [], // Extra native-engine packages to Flow/TS transform
149
- hotRuntime: false, // Experimental persistent RN workers
159
+ hotRuntime: false, // false (default) | 'auto' | true | HotRuntimeOptions
150
160
  }),
151
161
  ],
152
162
  });
@@ -161,7 +171,35 @@ export default defineConfig({
161
171
  | `diagnostics` | `boolean` | `false` | Log plugin activity to the console for debugging. |
162
172
  | `assetExts` | `string[]` | `[]` | Additional file extensions to stub as asset imports (beyond the built-in set). |
163
173
  | `transform` | `string[]` | `[]` | **`engine: 'native'` only.** Extra `node_modules` packages whose source the native engine should transform (Flow/TS/JSX stripped) as it loads them — for third-party RN libraries that ship untranspiled source (e.g. `react-native-reanimated`). Analogous to Jest's `transformIgnorePatterns` allowlist. |
164
- | `hotRuntime` | `boolean \| HotRuntimeOptions` | `false` | **Experimental, `engine: 'native'` only.** Reuse workers while keeping app/test modules isolated per file. See [Hot runtime](#hot-runtime). |
174
+ | `hotRuntime` | `boolean \| 'auto' \| HotRuntimeOptions` | `false` | `engine: 'native'` only. Reuse workers while keeping app/test modules isolated per file. `false` (the default) gives each file a fresh worker; `'auto'` opts in where the run can be bounded and recycled, and keeps per-file isolation otherwise. See [Hot runtime](#hot-runtime). |
175
+
176
+ ### Experimental Metro configuration
177
+
178
+ Pass `metroConfig: true` to use your project's Metro source-extension order and
179
+ asset list in both Vite and Node resolution. This is opt-in while the framework
180
+ and package-manager compatibility matrix is being validated:
181
+
182
+ ```ts
183
+ reactNative({ metroConfig: true });
184
+ // Config outside the project root:
185
+ reactNative({ metroConfig: { configFile: "../../metro.config.cjs" } });
186
+ ```
187
+
188
+ Expo's defaults prefer TypeScript over JavaScript when both variants exist;
189
+ bare React Native's defaults prefer JavaScript. The profile is evaluated in a
190
+ short-lived child using the project's own Metro tooling. Explicit `assetExts`
191
+ additions still apply. Metro transforms are not installed: source formats such as
192
+ SVG need a corresponding Vitest/Vite transform.
193
+
194
+ A custom `resolver.resolveRequest` is code and does not run under Vitest: the
195
+ source and asset extensions still apply, and a warning names the gap. Where one of
196
+ its redirects matters to tests, reproduce it with `resolve.alias`. Package
197
+ main fields and conditions remain governed by the engine's ownership policy;
198
+ `diagnostics: true` reports the observed Metro values. Config is evaluated on
199
+ startup; changing it requires restarting the run.
200
+
201
+ Default: `false`. Findings and remaining gates are recorded in
202
+ [the investigation log](docs/metro-profile-investigation.md).
165
203
 
166
204
  ### `engine`
167
205
 
@@ -172,13 +210,13 @@ Choose how React Native is provided to your tests:
172
210
  requires a device. Jest's `react-native` preset replaces many of these modules with
173
211
  stubs, so the native engine exercises behavior Jest's mocks skip (see [Fidelity](#fidelity)
174
212
  below). Best for components, integration, RNTL, virtualized lists, and anywhere a false
175
- pass is costly. Requires `@react-native/babel-preset` and `@babel/core` (present by
176
- default in React Native apps).
213
+ pass is costly. Requires `@react-native/babel-preset` (or, from Expo SDK 56, Expo's
214
+ preset) and `@babel/core`, present by default in React Native and Expo apps.
177
215
  - `'mock'` — a lightweight, pure-JS reimplementation of React Native with **zero extra
178
216
  dependencies**. Best for pure-logic/unit tests, maximum determinism, or when you can't
179
217
  add the babel deps. Like Jest's preset, it is a simplification — see [Fidelity](#fidelity).
180
- - `'auto'` _(default)_ — picks automatically, and **resolves to `'native'`** whenever
181
- `@react-native/babel-preset` and `@babel/core` are present (i.e. in any real RN app). It
218
+ - `'auto'` _(default)_ — picks automatically, and **resolves to `'native'`** whenever a React
219
+ Native Babel preset and `@babel/core` are present (i.e. in any real RN or Expo app). It
182
220
  falls back to `'mock'` only when those deps are absent, printing one line to explain why.
183
221
  So `reactNative()` with no options runs real React Native. Set `engine: 'mock'` to opt out.
184
222
 
@@ -186,6 +224,12 @@ Choose how React Native is provided to your tests:
186
224
  reactNative({ engine: "native" });
187
225
  ```
188
226
 
227
+ On the first cold run, the native engine compiles React Native's synchronous module graph into a
228
+ factory registry in a short-lived, heap-bounded child process. The child exits before test workers
229
+ start, so Babel's compilation heap does not become a permanent Vite-process RSS watermark. Later
230
+ runs validate and reuse the cache in-process without launching the compiler. A failed optimization
231
+ remains visible and falls back to the slower per-file loader without changing test results.
232
+
189
233
  #### Fidelity
190
234
 
191
235
  Jest's `react-native` preset replaces many real modules with stubs, so behavior they
@@ -204,32 +248,64 @@ the cross-check/fidelity harnesses in the repository's `bench/` directory.
204
248
 
205
249
  #### Hot runtime
206
250
 
207
- `engine: 'native'` normally uses Vitest's safest isolation model: each test file gets a fresh
208
- worker, so React Native reloads for every file. Large suites can opt into persistent workers:
251
+ Vitest's default isolation gives each test file a fresh worker, so React Native reloads for every
252
+ file; that is the default (`hotRuntime: false`), as in 0.13.0. With `hotRuntime: 'auto'` the native
253
+ engine instead reuses workers and resets modules per file whenever that can be done safely. It has
254
+ known differences from per-file isolation, listed under "A test passes on its own but fails when
255
+ run with other files" in the troubleshooting guide.
256
+
257
+ `'auto'` selects persistent workers when the current scheduler provides recyclable task boundaries,
258
+ the host/container memory plan admits at least two workers, no other pool was explicitly
259
+ selected, and `test.isolate` is not set explicitly — an explicit Vitest isolation choice, `true` or
260
+ `false`, is always kept. The same holds for CLI flags (`--pool`, `--isolate`, `--no-isolate`,
261
+ `--maxWorkers=1`, `--no-file-parallelism`). With `hotRuntime: true` instead, any of these fails at
262
+ startup with `HOT_RUNTIME_OVERRIDDEN` rather than being overridden. `'auto'` also keeps Vitest's
263
+ isolation when another project in the same `sequence.groupOrder` runs a different worker count,
264
+ which Vitest does not allow; give the native project its own `test.sequence.groupOrder` to keep hot.
265
+ Suites migrated from Jest (`jestMockTransform()`, the jest-compat setup) are included: the
266
+ jest-compat surface — `jest.mock` in its forms, runtime mocks, spies, timers, React Native Testing
267
+ Library cleanup — has its own cross-file isolation gate under the hot runtime. The decision runs
268
+ after other Vite config hooks, so a pool contributed by a later plugin is included rather than
269
+ producing a mixed runtime. When it falls
270
+ back to per-file isolation it prints the reason. Per-file isolation, the default, can also be set
271
+ explicitly:
209
272
 
210
273
  ```ts
211
274
  reactNative({
212
275
  engine: "native",
213
- hotRuntime: true,
276
+ hotRuntime: false,
214
277
  });
215
278
  ```
216
279
 
217
- The hot runtime keeps React Native's externalized module graph resident while Vitest still resets
218
- the app/test module graph for every file. It also restores direct `process.env` mutations,
219
- file-created globals, RN event subscriptions, dimensions, timers, and plugin mock state between
220
- files. A dedicated one-worker CI test exercises this cross-file isolation contract.
280
+ To require hot mode and fail closed when it cannot be bounded, use the explicit form:
281
+
282
+ ```ts
283
+ reactNative({
284
+ engine: "native",
285
+ hotRuntime: true,
286
+ });
287
+ ```
221
288
 
222
- **Known limitation.** Because React Native stays resident, state held inside React Native's own
223
- internal modules is _not_ reset between files — the per-file reset deliberately does not reach into
224
- third-party or RN-internal module internals, because doing so generically is unsafe. Suites that
225
- lean on deep resident-RN-internal state can therefore see cross-file interference under the hot
226
- runtime that they would not see under the default per-file isolation. The clearest example is heavy
227
- cross-file `Animated` usage, where animations driven in one file can alter how a later file renders.
228
- The tell-tale sign is a test that passes in isolation but fails when run after other files; if you
229
- see that, move the affected suite (or the project) back to the default per-file isolation. This is
230
- why the hot runtime is opt-in and experimental rather than the default; closing the gap for all
231
- suites depends on a per-file module reset inside a persistent worker that Vitest does not yet
232
- provide.
289
+ The hot runtime keeps React Native's precompiled factory registry and worker realm warm while
290
+ resetting the registry's module instances and the app/test module graph for every file. An ordered
291
+ state manifest also restores and verifies timers/Vitest stubs, direct `process.env` changes,
292
+ process and RN listeners, complete global and console property descriptors, known RN state,
293
+ ErrorUtils, Expo runtime state, and plugin boundary mocks. A mutation gate removes each of its
294
+ eleven restore actions in turn and requires the cross-file isolation suite to fail by name.
295
+
296
+ When hot is enabled, the plugin derives a worker-total memory plan from the lower of host and
297
+ container/cgroup memory. It reserves main-process and replacement headroom, caps automatic
298
+ concurrency at four, recycles workers on local heap or process RSS, and stops before starting more
299
+ work at the hard RSS boundary. Run with `diagnostics: true` or `vitest-native doctor` to see the
300
+ chosen envelope.
301
+
302
+ **Known limitation.** No shared-realm sanitizer can generically discover arbitrary mutable state in
303
+ every resident third-party singleton. The registry resets React Native's own in-memory module
304
+ instances, and the manifest handles the supported runtime surfaces above; unknown process-wide
305
+ state is still the library's or test fixture's responsibility. A test that passes alone but fails
306
+ after another file is a correctness signal: set `hotRuntime: false` for that suite and report the
307
+ small reproduction. The hot runtime depends on Vitest's custom worker APIs, which Vitest labels
308
+ experimental.
233
309
 
234
310
  For additional leak containment:
235
311
 
@@ -244,15 +320,20 @@ reactNative({
244
320
  });
245
321
  ```
246
322
 
247
- Recycling is applied between Vitest scheduler tasks. Vitest can place multiple files in one task,
248
- especially with `maxWorkers: 1`, so a worker cannot be retired in the middle of that batch. Use
249
- more than one worker when strict per-file retirement matters. `preserveGlobals` is an exact
250
- allowlist for registries created by resident external dependencies; Storybook's preview registry
251
- is preserved automatically.
323
+ Recycling is applied between Vitest scheduler tasks. Current Vitest batches every file into one
324
+ uninterruptible task at `maxWorkers: 1`, so `'auto'` keeps per-file isolation there and
325
+ `hotRuntime: true` fails that configuration rather than claim an inert memory bound. Use at least
326
+ two workers for hot mode. If an external scheduler
327
+ already provides a hard boundary, you can deliberately accept the risk with
328
+ `hotRuntime: { allowUnboundedMemory: true }`; this disables the automatic worker cap and
329
+ process-RSS enforcement and prints a warning when the batch is observed. `preserveGlobals` is an
330
+ exact allowlist for registries created by resident external dependencies; Storybook's preview
331
+ registry is preserved automatically.
252
332
 
253
333
  This mode uses Vitest's custom pool and worker APIs, so it remains experimental. CI runs the
254
- complete native suite under both the lockfile Vitest and the newest supported Vitest 4 release,
255
- plus a generated 100-file isolation soak and an end-to-end memory-triggered recycling test.
334
+ complete native suite under both the lockfile Vitest and the newest supported Vitest release,
335
+ plus a generated 100-file isolation soak, an end-to-end memory-triggered recycling test, and a
336
+ packed auto-selection matrix covering enablement and every guarded fallback class.
256
337
 
257
338
  ---
258
339
 
@@ -465,24 +546,27 @@ export default defineConfig({
465
546
 
466
547
  ### Available Presets
467
548
 
468
- | Import | Function |
469
- | ------------------------------ | ----------------------------------------- |
470
- | `presets.reanimated()` | react-native-reanimated |
471
- | `presets.gestureHandler()` | react-native-gesture-handler |
472
- | `presets.safeAreaContext()` | react-native-safe-area-context |
473
- | `presets.navigation()` | @react-navigation/native |
474
- | `presets.asyncStorage()` | @react-native-async-storage/async-storage |
475
- | `presets.screens()` | react-native-screens |
476
- | `presets.expo()` | Expo modules |
477
- | `presets.deviceInfo()` | react-native-device-info |
478
- | `presets.mmkv()` | react-native-mmkv |
479
- | `presets.netInfo()` | @react-native-community/netinfo |
480
- | `presets.svg()` | react-native-svg |
481
- | `presets.webview()` | react-native-webview |
482
- | `presets.vectorIcons()` | react-native-vector-icons |
483
- | `presets.flashList()` | @shopify/flash-list |
484
- | `presets.bottomSheet()` | @gorhom/bottom-sheet |
485
- | `presets.keyboardController()` | react-native-keyboard-controller |
549
+ | Preset | Library | What's mocked |
550
+ |--------|---------|---------------|
551
+ | `presets.reanimated()` | `react-native-reanimated` | `useSharedValue`, `useAnimatedStyle`, `withTiming`, `withSpring`, `withDelay`, `withSequence`, `withRepeat`, layout animations (`FadeIn`, `FadeOut`, `SlideInRight`), `Easing`, `interpolate`, `createAnimatedComponent` |
552
+ | `presets.worklets()` | `react-native-worklets` | `runOnJS`, `runOnUI`, `scheduleOnRN`, `scheduleOnUI`, `createWorkletRuntime`, … run synchronously on the JS thread |
553
+ | `presets.gestureHandler()` | `react-native-gesture-handler` | `GestureHandlerRootView`, gesture handlers (Pan, Tap, LongPress, Pinch, Rotation, Fling), `Gesture` API (v2), `GestureDetector`, `Swipeable`, touchable wrappers, state constants |
554
+ | `presets.safeAreaContext()` | `react-native-safe-area-context` | `SafeAreaProvider`, `SafeAreaView`, `useSafeAreaInsets`, `useSafeAreaFrame`, `initialWindowMetrics`, `withSafeAreaInsets` |
555
+ | `presets.navigation()` | `@react-navigation/native`, `@react-navigation/native-stack`, `@react-navigation/bottom-tabs`, `@react-navigation/drawer`, `@react-navigation/elements` | `NavigationContainer`, `useNavigation`, `useRoute`, `useFocusEffect`, `useIsFocused`, `CommonActions`, `StackActions`, `TabActions`, `DrawerActions`, navigators |
556
+ | `presets.screens()` | `react-native-screens` | `enableScreens`, `Screen`, `ScreenContainer`, `ScreenStack` |
557
+ | `presets.asyncStorage()` | `@react-native-async-storage/async-storage` | in-memory store (`getItem`/`setItem`/`multiGet`/`mergeItem`/…) |
558
+ | `presets.expo()` | `expo-constants`, `expo-font`, `expo-asset`, `expo-splash-screen`, `expo-linking`, `expo-status-bar` | constants, fonts, assets, splash screen, linking, status bar |
559
+ | `presets.deviceInfo()` | `react-native-device-info` | string/bool/number getters with sync + async variants |
560
+ | `presets.mmkv()` | `react-native-mmkv` | in-memory `MMKV` + `useMMKV*` hooks, for mmkv 2 only: the library tests itself from v3 (its own in-memory backend under Vitest), so the preset steps aside |
561
+ | `presets.netInfo()` | `@react-native-community/netinfo` | connected-wifi state, `fetch`/`refresh`/`addEventListener`/`useNetInfo`, state-type enums |
562
+ | `presets.svg()` | `react-native-svg` | `Svg`, `Path`, `Circle`, `Rect`, `G`, … as host components |
563
+ | `presets.webview()` | `react-native-webview` | `WebView` (default + named) host component |
564
+ | `presets.vectorIcons()` | `@react-native-vector-icons/common` | `createIconSet` and the dynamic font loader shared by the v10+ scoped icon sets (not the legacy unscoped `react-native-vector-icons`) |
565
+ | `presets.flashList()` | `@shopify/flash-list` | `FlashList` rendering its data through `renderItem`, the ref surface, v2 recycler hooks |
566
+ | `presets.bottomSheet()` | `@gorhom/bottom-sheet` | `BottomSheet`, `BottomSheetModal` + provider, sheet views, scroll/list variants, `BottomSheetTextInput`, backdrop, footer |
567
+ | `presets.keyboardController()` | `react-native-keyboard-controller` | `KeyboardProvider`, `KeyboardAvoidingView`, `KeyboardAwareScrollView`, `KeyboardStickyView`, `KeyboardToolbar`; the imperative `KeyboardController` is inert |
568
+ | `presets.unistyles()` | `react-native-unistyles`, `react-native-unistyles/reanimated` | Unistyles 3: `StyleSheet.configure`/`create` with themes, variants and compound variants, `useUnistyles`, `withUnistyles`, `UnistylesRuntime` theme switching, `mq` with `Display`/`Hide`; matches the library's own Jest mock and follows its source where that mock does less |
569
+ | `presets.skia()` | `react-native-skia`, `@shopify/react-native-skia` | Skia's own test mock (`Mock(CanvasKit)`) over CanvasKit, Skia compiled to WebAssembly: the `Skia` API computes with real Skia, `Canvas` and its drawing render as Views; plus Skia's Reanimated helpers (`usePathValue`, `useTexture`, …) and a `matchFont` that returns a font |
486
570
 
487
571
  Presets apply under **both** engines: the mock engine and `engine: 'native'` (where they shadow each
488
572
  library's native runtime — worklets, native modules — exactly as Jest does, while the surrounding tree
@@ -490,20 +574,29 @@ renders through real React Native).
490
574
 
491
575
  ### Expo
492
576
 
493
- The `expo` preset shadows the common Expo modules (`expo-constants`, `expo-status-bar`, `expo-font`,
494
- `expo-asset`, `expo-splash-screen`, `expo-linking`) the way `jest-expo` mocks them. A component that
495
- imports those renders under the native engine with no extra setup — there's a gated proof in
496
- `tests-native/expo.test.tsx`. Two honest caveats:
497
-
498
- - **Expo modules without a built-in preset** (e.g. `expo-image`, `expo-haptics`) still need a
499
- setup-file `vi.mock` at a Vite-managed boundary. The native engine externalizes native-side
500
- libraries to Node, so imports made entirely inside that externalized graph cannot be intercepted
501
- by Vitest's mocker.
577
+ Expo apps need no Expo-specific package. Under the native engine every Expo package's own
578
+ JavaScript runs for real — `expo` itself, expo-router, React Navigation and Expo modules such as
579
+ `expo-constants` or `expo-splash-screen` — and only their native modules are mocked, the way
580
+ `jest-expo` runs them. (The mock engine replaces the common Expo modules with the `expo` preset.)
581
+
582
+ - **Coming from jest-expo:** `npx vitest-native migrate --write` translates the `jest-expo`
583
+ presets (`jest-expo/android` → `platform: 'android'`) and, because jest-expo does not mock React
584
+ Navigation, turns the navigation preset off when a `@react-navigation/*` package is installed.
585
+ - **expo-router screens:** `expo-router/testing-library` (`renderRouter`, `testRouter`,
586
+ `toHavePathname`) runs as written with the jest-compat layer. From SDK 57 expo-router bundles its
587
+ own React Navigation; before SDK 57, also set `presets: { navigation: false }`.
588
+ - **Expo modules** run their real JavaScript, and their native modules answer as under
589
+ jest-expo: from the package's own `mocks/<Name>.ts` where it ships one, else from Expo's table
590
+ of the native modules (`Device.modelName` is `'mock'`). An optional native module a package
591
+ probes for, and that no installed package defines, is absent, as on a device.
502
592
  - **Expo SDK trails React Native.** Pin your `react-native` to the version your Expo SDK supports
503
- (which is within this plugin's validated range), not the newest RN release.
593
+ (SDK 54 and later are within this plugin's validated range), not the newest RN release.
504
594
 
505
- A packed Expo 56 consumer is installed from the release tarball and tested in CI. That gate covers
506
- Expo preset auto-detection, `expo-constants`, `expo-status-bar`, real RN rendering, and RNTL 13.
595
+ Packed Expo SDK 58 and SDK 57 apps are installed from the release tarball and tested in CI: Expo
596
+ module auto-detection and rendering, and expo-router's testing library against file-based `app/`
597
+ routes. The SDK 58 app also runs those routes under the configuration `migrate` generates from a
598
+ jest-expo setup. See the
599
+ [Expo guide](https://danfry1.github.io/vitest-native/guide/expo).
507
600
 
508
601
  ---
509
602
 
@@ -550,16 +643,23 @@ export default defineConfig({
550
643
  ```
551
644
 
552
645
  This is **not** a turnkey drop-in — a real suite still needs a small per-suite cleanup (convert
553
- top-level `jest.mock` → `vi.mock`, RNTL ≥ 12, re-record snapshots once with `-u`). The full recipe,
646
+ top-level `jest.mock` → `vi.mock`, RNTL ≥ 12, record snapshots for Vitest in their own directory). The full recipe,
554
647
  with a worked real-app example, is in **[docs/migrating-from-jest.md](docs/migrating-from-jest.md)**.
555
648
 
556
649
  ---
557
650
 
558
651
  ## Troubleshooting
559
652
 
653
+ **`npm error Cannot read properties of null (reading 'edgesOut')` during install**
654
+ npm 10 (the version bundled with Node 22) crashes while resolving Vite 8.2+'s optional peer
655
+ chain when the project pins Vitest 4, because that chain resolves to the latest Vitest (5.x).
656
+ This is an npm bug, fixed in npm 11 ([vitest#11245](https://github.com/vitest-dev/vitest/issues/11245)).
657
+ Upgrade npm (`npm install --global npm@11`), or use pnpm, Yarn or Bun.
658
+
560
659
  **`@react-native/babel-preset not found — using the mock engine`**
561
660
  You asked for the default (`auto`) engine but the native babel deps aren't installed, so it
562
- fell back to the mock engine. To run real React Native, install them:
661
+ fell back to the mock engine. An Expo project from SDK 56 needs nothing extra: the engine uses
662
+ Expo's preset, and `expo` brings `@babel/core`. Otherwise, to run real React Native, install them:
563
663
 
564
664
  ```bash
565
665
  npm i -D @react-native/babel-preset @babel/core
@@ -590,7 +690,10 @@ preset.
590
690
 
591
691
  **Snapshots differ after switching from Jest or the mock engine**
592
692
  Real RN computes host props (e.g. `<Text>`'s `accessible`, `allowFontScaling`) that mocks omit, so
593
- the tree is richer. Re-record once with `npx vitest -u` after migrating.
693
+ the tree is richer. Vitest also keys snapshots differently from Jest (`a > b 1`, not `a b 1`), so
694
+ it cannot check Jest's `.snap` files, and `vitest -u` rewrites them for Vitest. While both runners
695
+ run, give Vitest its own directory with `test.resolveSnapshotPath`; see
696
+ [docs/migrating-from-jest.md](docs/migrating-from-jest.md#2d-snapshots-keep-jests-and-vitests-apart-then-record-vitests).
594
697
 
595
698
  **`Worker terminated due to reaching memory limit` / `JS heap out of memory` (large native suites)**
596
699
  Every test file that renders real React Native loads a full renderer, so a large suite can grow a
@@ -604,9 +707,9 @@ export default defineConfig({
604
707
  });
605
708
  ```
606
709
 
607
- The experimental `reactNative({ hotRuntime: true })` also helps: it keeps React Native resident
608
- across files (loaded once per worker instead of per file) and recycles workers on a memory/file
609
- budget.
710
+ The hot runtime (`hotRuntime: 'auto'`, opt-in) also helps: it keeps React Native's precompiled
711
+ factory registry warm, resets its module instances from memory per file, and recycles workers on a
712
+ memory/file budget.
610
713
 
611
714
  **`Vitest caught N unhandled errors` in error-boundary tests**
612
715
  A test that intentionally throws inside a component (to exercise an error boundary) makes React log
@@ -830,15 +933,21 @@ falling back to the default (often web) file.
830
933
 
831
934
  ---
832
935
 
833
- ## Asset Stubs
936
+ ## Assets
834
937
 
835
- Image, font, video, and audio imports are automatically stubbed. The import resolves to the filename string, so code that passes asset paths through continues to work without errors.
938
+ Image, font, video, and audio imports evaluate to what Metro gives an app: a number, the id under which the asset is registered with React Native's asset registry. The registered descriptor is the one Metro builds — `name`, `type`, `scales`, `hash`, and for images `width` and `height` in points — so React Native's own asset handling works as on device:
836
939
 
837
- ```ts
838
- import logo from "./logo.png";
839
- // logo === 'logo.png'
940
+ ```tsx
941
+ const logo = require("./logo.png"); // a number
942
+ Image.resolveAssetSource(logo); // { uri, width, height, scale, __packager_asset: true }
943
+ <Image source={logo} />; // renders the resolved asset
840
944
  ```
841
945
 
946
+ - `@2x`/`@3x` variants are grouped as Metro groups them, and a `.ios`/`.android` variant is preferred for the configured platform.
947
+ - Dimensions are read from PNG, JPEG, GIF, BMP and WebP headers; other formats, and files that are not valid images, register without them.
948
+ - The native engine registers with React Native's real registry; the mock engine registers with the mock `AssetRegistry`, which its `Image.resolveAssetSource` reads.
949
+ - A snapshot shows the id (a number) where it used to show the file name. Ids follow registration order within a test file, so they are stable for that file.
950
+
842
951
  Built-in extensions include common formats like `.png`, `.jpg`, `.gif`, `.svg`, `.mp4`, `.ttf`, `.otf`, and more. To add additional extensions:
843
952
 
844
953
  ```ts
@@ -873,10 +982,17 @@ that API.
873
982
 
874
983
  Every release must pass:
875
984
 
876
- - Linux on Node 20.19 and 22.13, plus macOS and Windows on Node 22.13.
985
+ - Linux on Node 22.13 (with RNTL 12, 13 and 14) and 24, plus macOS and Windows on Node 22.13.
877
986
  - The mock, native iOS, native Android, hot-runtime, isolation, and 100-file soak suites.
878
- - React Native 0.81–0.86 against both locked and newest-supported Vitest 4.
879
- - Packed bare RN 0.83/RNTL 12, Expo 56/RNTL 13, Vite 8 monorepo/RNTL 14, and RN 0.86 consumers.
987
+ - React Native 0.81–0.88 against both locked and newest-supported Vitest, plus the Vitest 4 floor
988
+ on 0.81 and 0.88.
989
+ - Vitest's own behaviour under the defaults, on Vitest 5 and the Vitest 4 floor: config and CLI
990
+ flags, projects, watch mode, the jsdom and happy-dom environments, `--typecheck`, `vitest bench`,
991
+ and the HTML reporter.
992
+ - Packed consumers: bare RN 0.83/RNTL 12, a Jest-migration project, Expo SDK 58/RNTL 14 (with
993
+ expo-router and a `migrate`-generated jest-expo config), Expo SDK 57/RNTL 13 (with expo-router), a mock-engine RNTL 14 project, npm- and
994
+ pnpm-workspace monorepos on Vite 8, and an RN 0.86 Android project.
995
+ - Packed V8 and Istanbul coverage maps/counts matching default isolation exactly under hot workers.
880
996
  - Mock-versus-real-RN behavioral cross-checks, example-app tests, typecheck, lint, formatting, and
881
997
  package export analysis.
882
998
 
@@ -895,15 +1011,16 @@ see [versioning and stability](docs/versioning.md).
895
1011
  | -------------------------------------------- | ----------------------------------------- |
896
1012
  | `react` | >= 18; use RN's matching React version |
897
1013
  | `vite` | ^6.4.2, ^7.3.2, or ^8.0.5 |
898
- | `vitest` | 4.x |
899
- | `node` | >= 20.19; RN/RNTL may impose a higher floor |
900
- | `react-native` (native engine) | 0.81–0.86 validated |
1014
+ | `vitest` | 4.x or 5.x |
1015
+ | `node` | 22.13+ on Node 22, or 24.3+ |
1016
+ | `react-native` (native engine) | 0.81–0.88 validated |
901
1017
  | `@react-native/babel-preset` (native engine) | Match the installed React Native minor |
902
1018
  | `@testing-library/react-native` (optional) | 12.x, 13.x, or 14.x |
903
1019
 
904
- RNTL 14 requires Node 22.13 or 24+ and uses async rendering APIs. React Native 0.86 requires
905
- Node 20.19.4, 22.13, or 24.3 or newer. Those are upstream requirements; the mock engine itself
906
- continues to support Node 20.
1020
+ Node 20 reached end of life in April 2026. Node 22.13 is the first Node 22 that both RNTL 14 and the
1021
+ engine's CommonJS entry points (`require(esm)`) support. On Node 24.0–24.2, `jest.mock`,
1022
+ `jest.isolateModules` and `jest.resetModules` do not reach modules Node loads, with either loader.
1023
+ Node 23 is past its end of life. RNTL 14 uses async rendering APIs.
907
1024
 
908
1025
  ---
909
1026