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.
- package/CHANGELOG.md +746 -0
- package/README.md +205 -88
- package/dist/cli.cjs +1478 -145
- package/dist/cli.d.cts +2 -3
- package/dist/cli.d.mts +2 -3
- package/dist/cli.mjs +1475 -142
- package/dist/context.d.mts +40 -0
- package/dist/context.mjs +238 -0
- package/dist/errors.d.mts +19 -0
- package/dist/helpers.cjs +6 -9
- package/dist/helpers.d.cts +8 -9
- package/dist/helpers.d.mts +8 -9
- package/dist/helpers.mjs +6 -9
- package/dist/host-D3wzmsw8.cjs +1296 -0
- package/dist/host-DCPzgC3D.mjs +1180 -0
- package/dist/{index-CPGq6p95.d.cts → index-CPEAYjaQ.d.mts} +88 -19
- package/dist/{index-CmETArCM.d.mts → index-D5cOy_iY.d.cts} +89 -18
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +1366 -210
- 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/error-equality.mjs +127 -0
- package/dist/jest-compat/interop.mjs +78 -4
- package/dist/jest-compat/jest-globals.mjs +5 -2
- package/dist/jest-compat/jest-object.mjs +348 -0
- package/dist/jest-compat/node-registry.mjs +489 -0
- package/dist/jest-compat/setup.mjs +167 -66
- package/dist/jest-compat.cjs +35 -5
- package/dist/jest-compat.d.cts +9 -5
- package/dist/jest-compat.d.mts +9 -5
- package/dist/jest-compat.mjs +35 -3
- package/dist/matchers.d.cts +9 -8
- package/dist/matchers.d.mts +9 -8
- package/dist/memory-BFxB285Z.mjs +137 -0
- package/dist/memory-bBnzadf6.cjs +155 -0
- package/dist/native/assets.d.mts +39 -0
- package/dist/native/assets.mjs +431 -0
- package/dist/native/babel-preset.d.mts +14 -0
- package/dist/native/babel-preset.mjs +56 -0
- package/dist/native/boundary.mjs +71 -8
- package/dist/native/bounded-process.mjs +165 -0
- package/dist/native/compile-cache.mjs +21 -9
- package/dist/native/expo-module-mocks.mjs +432 -0
- package/dist/native/expo-module-table-57.mjs +1925 -0
- package/dist/native/expo-module-table-58.mjs +2045 -0
- package/dist/native/expo-module-tables.mjs +6 -0
- package/dist/native/expo-native-modules.mjs +66 -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 +289 -9
- package/dist/native/hooks.mjs +392 -114
- package/dist/native/loader.mjs +346 -75
- package/dist/native/match.d.mts +13 -0
- package/dist/native/match.mjs +127 -2
- package/dist/native/metro-profile-compiler.mjs +234 -0
- package/dist/native/metro-profile.mjs +115 -0
- package/dist/native/module-reset.mjs +172 -6
- package/dist/native/native-modules.d.mts +15 -0
- package/dist/native/native-modules.mjs +228 -0
- package/dist/native/nitro.mjs +60 -0
- 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 +197 -39
- package/dist/native/reset.mjs +334 -135
- package/dist/native/resolve.d.mts +20 -3
- package/dist/native/resolve.mjs +103 -17
- package/dist/native/rntl-hooks.mjs +227 -0
- package/dist/native/rntl-text-host.mjs +64 -0
- package/dist/native/runner.mjs +33 -1
- package/dist/native/setup.mjs +284 -96
- package/dist/native/state-manifest.d.mts +22 -0
- package/dist/native/state-manifest.mjs +138 -0
- package/dist/native/transform.d.mts +2 -0
- package/dist/native/transform.mjs +270 -47
- package/dist/native/tsconfig-paths.d.mts +5 -0
- package/dist/native/tsconfig-paths.mjs +163 -0
- package/dist/native/worker.mjs +151 -40
- package/dist/pool-DUOe-n1k.mjs +125 -0
- package/dist/pool-DaTJRKJb.cjs +127 -0
- package/dist/preset-map-BlBriTA0.mjs +197 -0
- package/dist/preset-map-jSFsNy1Q.cjs +241 -0
- package/dist/preset-modules-Bt6k_Wdw.mjs +31 -0
- package/dist/preset-modules-C7gu3bPn.cjs +48 -0
- package/dist/preset-modules.d.mts +16 -0
- package/dist/preset-modules.mjs +92 -0
- package/dist/preset-ready.mjs +9 -0
- package/dist/{presets-CoNf4ByH.cjs → presets-DH7fC0YV.cjs} +1125 -102
- package/dist/{presets-DGProTaA.mjs → presets-DuXiUNAx.mjs} +1107 -103
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +1 -2
- package/dist/report-error.d.mts +20 -0
- package/dist/report-error.mjs +145 -0
- package/dist/rntl-matchers.d.ts +58 -12
- package/dist/rntl-support.d.mts +12 -0
- package/dist/rntl-support.mjs +189 -0
- 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 +463 -97
- package/dist/worker-vitest-BGPo0ek6.mjs +49 -0
- package/dist/worker-vitest-BfiQB8ND.cjs +62 -0
- package/docs/hot-default-design.md +181 -88
- package/docs/migrating-from-jest.md +74 -16
- package/docs/migrating-from-vitest-react-native.md +5 -5
- package/docs/release-readiness.md +55 -15
- package/docs/versioning.md +31 -22
- package/package.json +122 -98
- package/dist/ecosystem-CEIF_-jq.cjs +0 -659
- package/dist/ecosystem-Duw40-mO.mjs +0 -597
- package/dist/pool-BU-1ecIF.cjs +0 -122
- package/dist/pool-Bcxn9a3I.mjs +0 -118
- package/dist/preset-map-BoG19dLa.cjs +0 -35
- 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.
|
|
8
|
-
> Vite 6–8, Vitest 4, RNTL 12–14, bare apps, Expo
|
|
9
|
-
>
|
|
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
|
-
- [
|
|
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`.
|
|
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
|
-
>
|
|
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, //
|
|
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` |
|
|
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`
|
|
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
|
-
|
|
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
|
-
|
|
208
|
-
|
|
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:
|
|
276
|
+
hotRuntime: false,
|
|
214
277
|
});
|
|
215
278
|
```
|
|
216
279
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
|
255
|
-
plus a generated 100-file isolation soak
|
|
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
|
-
|
|
|
469
|
-
|
|
470
|
-
| `presets.reanimated()`
|
|
471
|
-
| `presets.
|
|
472
|
-
| `presets.
|
|
473
|
-
| `presets.
|
|
474
|
-
| `presets.
|
|
475
|
-
| `presets.screens()`
|
|
476
|
-
| `presets.
|
|
477
|
-
| `presets.
|
|
478
|
-
| `presets.
|
|
479
|
-
| `presets.
|
|
480
|
-
| `presets.
|
|
481
|
-
| `presets.
|
|
482
|
-
| `presets.
|
|
483
|
-
| `presets.
|
|
484
|
-
| `presets.
|
|
485
|
-
| `presets.
|
|
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
|
-
|
|
494
|
-
`expo
|
|
495
|
-
|
|
496
|
-
`
|
|
497
|
-
|
|
498
|
-
- **
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
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
|
-
(
|
|
593
|
+
(SDK 54 and later are within this plugin's validated range), not the newest RN release.
|
|
504
594
|
|
|
505
|
-
|
|
506
|
-
|
|
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,
|
|
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.
|
|
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.
|
|
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
|
|
608
|
-
|
|
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
|
-
##
|
|
936
|
+
## Assets
|
|
834
937
|
|
|
835
|
-
Image, font, video, and audio imports
|
|
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
|
-
```
|
|
838
|
-
|
|
839
|
-
//
|
|
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
|
|
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.
|
|
879
|
-
|
|
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` |
|
|
900
|
-
| `react-native` (native engine) | 0.81–0.
|
|
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
|
-
|
|
905
|
-
|
|
906
|
-
|
|
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
|
|