vitest-native 0.3.0 → 0.4.1
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 +88 -0
- package/README.md +254 -11
- package/dist/chunk-CZWwpsFl.cjs +43 -0
- package/dist/{index-CJqS-y_M.d.cts → index-CJIhukN_.d.mts} +58 -4
- package/dist/{index-h4vpIkQO.d.mts → index-DEB7OFaJ.d.cts} +60 -2
- package/dist/index.cjs +252 -21
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +248 -18
- package/dist/jest-compat/interop.mjs +30 -0
- package/dist/jest-compat/jest-globals.d.mts +3 -0
- package/dist/jest-compat/jest-globals.mjs +9 -0
- package/dist/jest-compat/noop.d.mts +3 -0
- package/dist/jest-compat/noop.mjs +7 -0
- package/dist/jest-compat/setup.d.mts +3 -0
- package/dist/jest-compat/setup.mjs +41 -0
- package/dist/jest-compat.cjs +109 -0
- package/dist/jest-compat.d.cts +38 -0
- package/dist/jest-compat.d.mts +38 -0
- package/dist/jest-compat.mjs +104 -0
- package/dist/matchers.cjs +69 -0
- package/dist/matchers.d.cts +51 -0
- package/dist/matchers.d.mts +51 -0
- package/dist/matchers.mjs +66 -0
- package/dist/native/boundary.mjs +225 -0
- package/dist/native/globals.mjs +112 -0
- package/dist/native/hooks.mjs +85 -0
- package/dist/native/loader.mjs +123 -0
- package/dist/native/match.mjs +9 -0
- package/dist/native/reset.mjs +170 -0
- package/dist/native/resolve.mjs +22 -0
- package/dist/native/runner.mjs +18 -0
- package/dist/native/setup.mjs +134 -0
- package/dist/native/transform.mjs +79 -0
- package/dist/native/worker.mjs +96 -0
- package/dist/pool-B4mWN3N5.cjs +60 -0
- package/dist/pool-oPTZiSXU.mjs +58 -0
- package/dist/{preset-map-DxU6UgYS.cjs → preset-map-C3jxnkiX.cjs} +5 -1
- package/dist/{preset-map-BTpoNH3G.mjs → preset-map-OJm5IWuL.mjs} +5 -1
- package/dist/{presets-B-spxZz7.cjs → presets-2I68Ipyl.cjs} +437 -41
- package/dist/{presets-DvrcfEPz.mjs → presets-DOGXQzYT.mjs} +401 -6
- package/dist/presets.cjs +5 -1
- package/dist/presets.d.cts +2 -2
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/setup.cjs +263 -115
- package/dist/setup.mjs +259 -112
- package/docs/migrating-from-jest.md +123 -0
- package/docs/migrating-from-vitest-react-native.md +109 -0
- package/package.json +65 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,93 @@
|
|
|
1
1
|
# vitest-native
|
|
2
2
|
|
|
3
|
+
## 0.4.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
Documentation fixes (the README is consumers' primary reference):
|
|
8
|
+
|
|
9
|
+
- **Quick Start now runs as written.** It used bare `test()`/`expect()` with no
|
|
10
|
+
import, but the plugin does not enable Vitest globals — copying it produced
|
|
11
|
+
`test is not defined`. Added the `import { test, expect } from 'vitest'` and a
|
|
12
|
+
note on the `globals: true` alternative.
|
|
13
|
+
- **Installation** notes the companion dependencies the examples need
|
|
14
|
+
(`@testing-library/react-native`; a real RN app already provides `react-native`
|
|
15
|
+
+ `@react-native/babel-preset` + `@babel/core`).
|
|
16
|
+
- Corrected the RN-conformance count (118 ported: 115 passing, 3 documented skips).
|
|
17
|
+
- "Spiritual successor" → "Maintained successor" wording, with a migration link.
|
|
18
|
+
|
|
19
|
+
## 0.4.0
|
|
20
|
+
|
|
21
|
+
**The native engine is now the zero-config default.** `reactNative()` with no
|
|
22
|
+
options runs your tests against **real React Native** — the same JavaScript that
|
|
23
|
+
ships in your app — mocking only the native-module boundary. The pure-JS mock
|
|
24
|
+
engine remains as an explicit opt-in (`engine: 'mock'`). vitest-native positions
|
|
25
|
+
itself as the maintained continuation of
|
|
26
|
+
[`vitest-community/vitest-react-native`](https://github.com/vitest-community/vitest-react-native).
|
|
27
|
+
|
|
28
|
+
> Beta. The native engine is validated against real apps (react-native-paper, the
|
|
29
|
+
> obytes template, Rocket.Chat) across React Native 0.81–0.84, with a CI-gated
|
|
30
|
+
> behavioral cross-check against real RN. Some APIs may still shift before 1.0.
|
|
31
|
+
|
|
32
|
+
### Breaking Changes
|
|
33
|
+
|
|
34
|
+
- **`engine: 'auto'` (the default) now resolves to `'native'`** whenever
|
|
35
|
+
`@react-native/babel-preset` and `@babel/core` are present — i.e. in any real RN
|
|
36
|
+
app. It falls back to `'mock'` only when those deps are absent, printing one line
|
|
37
|
+
to explain why. Previously `auto` always resolved to `mock`. Set
|
|
38
|
+
`engine: 'mock'` to keep the old behavior.
|
|
39
|
+
|
|
40
|
+
### Native engine
|
|
41
|
+
|
|
42
|
+
- **Boundary hardening.** The native-module stub now honors RN's calling
|
|
43
|
+
conventions it previously broke: callback-style methods invoke the success
|
|
44
|
+
callback instead of hanging (fixes `AccessibilityInfo.*`, `Share.share`), and
|
|
45
|
+
promise-returning methods return a real `Promise` (fixes `Linking.canOpenURL`/
|
|
46
|
+
`openURL`, `Image.prefetch`/`getSize`). Backed by app-shaped stress suites
|
|
47
|
+
(`tests-native/stress*.test.tsx`) as a permanent regression gate.
|
|
48
|
+
- **`isolate: true` is the native-engine default** — the safe Vitest default.
|
|
49
|
+
Adversarial testing proved `isolate: false` leaks state across files at scale.
|
|
50
|
+
An opt-in **hot runtime** (`reactNative({ hotRuntime: true })`) reclaims the
|
|
51
|
+
speed safely via surgical per-file reset, for large suites.
|
|
52
|
+
- **`transform` allowlist** — extra `node_modules` packages whose untranspiled
|
|
53
|
+
source the native engine should strip (Flow/TS/JSX) as it loads them, for
|
|
54
|
+
third-party RN libraries (analogous to Jest's `transformIgnorePatterns`).
|
|
55
|
+
- **Presets apply under the native engine**, shadowing each library's native
|
|
56
|
+
runtime (worklets, native modules) the way Jest does — including transitively
|
|
57
|
+
imported presets — while the surrounding tree renders through real RN.
|
|
58
|
+
- **Expo**: the `expo` preset shadows the common Expo modules under the native
|
|
59
|
+
engine (gated proof in `tests-native/expo.test.tsx`).
|
|
60
|
+
|
|
61
|
+
### Trust & tooling
|
|
62
|
+
|
|
63
|
+
- **Cross-check** — a CI-gated behavioral differential that runs the same probes
|
|
64
|
+
under `mock` and `native` and diffs them against real RN as the oracle. It is
|
|
65
|
+
how mock fidelity is proven (and it found two of the mock fixes below).
|
|
66
|
+
- **Vitest × RN CI matrix** — gates the native engine across RN 0.81–0.84 ×
|
|
67
|
+
Vitest {pinned, latest}, with the latest-Vitest column as a non-blocking canary.
|
|
68
|
+
- **Jest migration tooling** — a `vitest-native/jest-compat` entry (the `jest`
|
|
69
|
+
global, `@jest/globals`, jest-native extend-expect) plus auto-hoisting of
|
|
70
|
+
top-level `jest.mock` → `vi.mock` and automatic JSX runtime. Guides:
|
|
71
|
+
`docs/migrating-from-jest.md` and `docs/migrating-from-vitest-react-native.md`.
|
|
72
|
+
|
|
73
|
+
### Presets & matchers
|
|
74
|
+
|
|
75
|
+
- `react-native-gesture-handler` preset now exports `Pressable` (mirroring RN's,
|
|
76
|
+
including suppressing press handlers when `disabled`).
|
|
77
|
+
- `toHaveAnimatedStyle` / `toHaveAnimatedProps` are auto-registered on `expect()`,
|
|
78
|
+
replacing reanimated's Jest-only `setUpTests()` matchers. Opt into types with
|
|
79
|
+
`"types": ["vitest-native/matchers"]`.
|
|
80
|
+
- New presets: `react-native-device-info`, `react-native-mmkv`, `react-native-svg`,
|
|
81
|
+
`react-native-webview`; navigation preset covers drawer/bottom-tabs/elements.
|
|
82
|
+
|
|
83
|
+
### Mock-engine fidelity fixes
|
|
84
|
+
|
|
85
|
+
- Disabled `Pressable`/`Touchable` mocks now suppress press handlers.
|
|
86
|
+
- `StyleSheet.hairlineWidth` is derived from the pixel ratio (≈`1/3` at scale 3)
|
|
87
|
+
instead of a hardcoded `0.5`, matching real RN.
|
|
88
|
+
- `Animated.Value.interpolate()` supports string output ranges (e.g.
|
|
89
|
+
`["0deg", "360deg"]`, `["0%", "100%"]`), preserving the unit/suffix.
|
|
90
|
+
|
|
3
91
|
## 0.3.0
|
|
4
92
|
|
|
5
93
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# vitest-native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
> **Beta.** The real-RN engine is validated against real apps (react-native-paper, the obytes template, Rocket.Chat) across React Native 0.81–0.84, with a CI-gated behavioral cross-check against real RN. Some APIs may still shift before 1.0.
|
|
6
|
+
>
|
|
7
|
+
> 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).
|
|
4
8
|
|
|
5
9
|
---
|
|
6
10
|
|
|
@@ -14,7 +18,11 @@ A Vitest plugin for React Native. One install, zero config.
|
|
|
14
18
|
- [React Native Test Suite Conformance](#react-native-test-suite-conformance)
|
|
15
19
|
- [Test Helpers](#test-helpers)
|
|
16
20
|
- [Auto-Detect Presets](#auto-detect-presets)
|
|
21
|
+
- [Migrating from `vitest-react-native`](#migrating-from-vitest-react-native)
|
|
22
|
+
- [Migrating from Jest](#migrating-from-jest)
|
|
23
|
+
- [Troubleshooting](#troubleshooting)
|
|
17
24
|
- [RNTL Matchers](#rntl-matchers)
|
|
25
|
+
- [Animated Matchers (Reanimated)](#animated-matchers-reanimated)
|
|
18
26
|
- [Snapshot Serializer](#snapshot-serializer)
|
|
19
27
|
- [Platform Extensions](#platform-extensions)
|
|
20
28
|
- [Asset Stubs](#asset-stubs)
|
|
@@ -30,13 +38,13 @@ Testing React Native code with Jest has traditionally required a patchwork of co
|
|
|
30
38
|
|
|
31
39
|
`vitest-native` replaces all of that with a single Vite plugin:
|
|
32
40
|
|
|
33
|
-
- **
|
|
34
|
-
- **
|
|
35
|
-
- **
|
|
36
|
-
- **Auto-detect presets.** The plugin scans your `node_modules` and
|
|
37
|
-
- **
|
|
41
|
+
- **Real React Native, by default.** `reactNative()` with no options runs React Native's real JavaScript — the same source that ships in your app — mocking only the native-module boundary. Jest's preset replaces many components and modules (`View`, `Text`, `ScrollView`, `Linking`, `AccessibilityInfo`, `Image`, …) with stubs; vitest-native runs the real ones. See [`engine`](#engine).
|
|
42
|
+
- **The runner you already use.** Already on Vitest for web? Write your React Native tests in the same runner — same config patterns, native ESM, watch mode, and UI. One test stack across web and native.
|
|
43
|
+
- **No Jest config patchwork.** Add one plugin instead of custom transformers, `transformIgnorePatterns`, and brittle `jest.setup.js` mock files.
|
|
44
|
+
- **Auto-detect presets.** The plugin scans your `node_modules` and shadows native-runtime libraries (Reanimated, Gesture Handler, Safe Area, Navigation) automatically — under both engines.
|
|
45
|
+
- **Opt-in mock engine.** Prefer no React Native at all for a pure-logic suite? `engine: 'mock'` reimplements RN in pure JS with **zero extra dependencies**. It is the escape hatch, not the headline — see [Choosing an engine](#engine).
|
|
38
46
|
|
|
39
|
-
|
|
47
|
+
> **Coming from Jest?** This is not a drop-in swap. New tests are friction-free; an existing Jest suite needs porting off `@react-native/jest-preset`, `@jest/globals`, and `jest.mock('react-native')`. See [Migrating from Jest](#migrating-from-jest). Coming from `vitest-react-native`? You are already vitest-shaped — migration is close to a package swap.
|
|
40
48
|
|
|
41
49
|
---
|
|
42
50
|
|
|
@@ -56,6 +64,16 @@ yarn add -D vitest-native
|
|
|
56
64
|
pnpm add -D vitest-native
|
|
57
65
|
```
|
|
58
66
|
|
|
67
|
+
A real React Native project already provides what the native engine needs —
|
|
68
|
+
`react-native`, `@react-native/babel-preset`, and `@babel/core`. Add
|
|
69
|
+
`@testing-library/react-native` if you don't have it yet (the examples below use it):
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npm install -D @testing-library/react-native
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
See [Requirements](#requirements) for exact versions.
|
|
76
|
+
|
|
59
77
|
---
|
|
60
78
|
|
|
61
79
|
## Quick Start
|
|
@@ -74,6 +92,7 @@ export default defineConfig({
|
|
|
74
92
|
That is it. Write a test:
|
|
75
93
|
|
|
76
94
|
```tsx
|
|
95
|
+
import { test, expect } from 'vitest';
|
|
77
96
|
import { render, screen } from '@testing-library/react-native';
|
|
78
97
|
import { View, Text } from 'react-native';
|
|
79
98
|
|
|
@@ -91,6 +110,9 @@ test('renders a greeting', () => {
|
|
|
91
110
|
});
|
|
92
111
|
```
|
|
93
112
|
|
|
113
|
+
> Prefer Jest-style globals (`test`/`expect` without imports)? Set
|
|
114
|
+
> `test: { globals: true }` in your Vitest config — vitest-native does not force it on.
|
|
115
|
+
|
|
94
116
|
Run it:
|
|
95
117
|
|
|
96
118
|
```bash
|
|
@@ -110,6 +132,7 @@ import { reactNative } from 'vitest-native';
|
|
|
110
132
|
export default defineConfig({
|
|
111
133
|
plugins: [
|
|
112
134
|
reactNative({
|
|
135
|
+
engine: 'auto', // 'auto' | 'mock' | 'native' — auto → native when RN's babel deps are present, else mock
|
|
113
136
|
platform: 'ios', // 'ios' | 'android' (default: 'ios')
|
|
114
137
|
presets: [], // Preset[] -- omit for auto-detect
|
|
115
138
|
mocks: {}, // Custom mock overrides for the react-native module
|
|
@@ -122,17 +145,58 @@ export default defineConfig({
|
|
|
122
145
|
|
|
123
146
|
| Option | Type | Default | Description |
|
|
124
147
|
|---|---|---|---|
|
|
148
|
+
| `engine` | `'auto' \| 'mock' \| 'native'` | `'auto'` | How React Native is provided to your tests. `auto` runs real RN (native) when available, else mock. See [`engine`](#engine) below. |
|
|
125
149
|
| `platform` | `'ios' \| 'android'` | `'ios'` | Target platform. Controls `Platform.OS`, version defaults, and file extension resolution. |
|
|
126
150
|
| `presets` | `Preset[]` | auto-detect | Built-in library presets. When omitted, installed packages are detected automatically. Only built-in presets are supported; for custom module mocking, use `vi.mock()` in a setup file. |
|
|
127
151
|
| `mocks` | `Record<string, any>` | `{}` | JSON-serializable overrides merged into the `react-native` module mock. Function values are not supported; use `vi.mock()` in a setup file for function-based overrides. |
|
|
128
152
|
| `diagnostics` | `boolean` | `false` | Log plugin activity to the console for debugging. |
|
|
129
153
|
| `assetExts` | `string[]` | `[]` | Additional file extensions to stub as asset imports (beyond the built-in set). |
|
|
154
|
+
| `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. |
|
|
155
|
+
|
|
156
|
+
### `engine`
|
|
157
|
+
|
|
158
|
+
Choose how React Native is provided to your tests:
|
|
159
|
+
|
|
160
|
+
- `'native'` — runs the **real** React Native JavaScript (component logic, prop
|
|
161
|
+
computation, validation, reconciliation), mocking only the native module boundary that
|
|
162
|
+
requires a device. Jest's `react-native` preset replaces many of these modules with
|
|
163
|
+
stubs, so the native engine exercises behavior Jest's mocks skip (see [Fidelity](#fidelity)
|
|
164
|
+
below). Best for components, integration, RNTL, virtualized lists, and anywhere a false
|
|
165
|
+
pass is costly. Requires `@react-native/babel-preset` and `@babel/core` (present by
|
|
166
|
+
default in React Native apps).
|
|
167
|
+
- `'mock'` — a lightweight, pure-JS reimplementation of React Native with **zero extra
|
|
168
|
+
dependencies**. Best for pure-logic/unit tests, maximum determinism, or when you can't
|
|
169
|
+
add the babel deps. Like Jest's preset, it is a simplification — see [Fidelity](#fidelity).
|
|
170
|
+
- `'auto'` *(default)* — picks automatically, and **resolves to `'native'`** whenever
|
|
171
|
+
`@react-native/babel-preset` and `@babel/core` are present (i.e. in any real RN app). It
|
|
172
|
+
falls back to `'mock'` only when those deps are absent, printing one line to explain why.
|
|
173
|
+
So `reactNative()` with no options runs real React Native. Set `engine: 'mock'` to opt out.
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
reactNative({ engine: 'native' })
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### Fidelity
|
|
180
|
+
|
|
181
|
+
Jest's `react-native` preset replaces many real modules with stubs, so behavior they
|
|
182
|
+
don't implement passes silently. The `native` engine runs the real implementations, so
|
|
183
|
+
those checks actually run. For example:
|
|
184
|
+
|
|
185
|
+
| Behavior | `engine: 'native'` (real RN) | Jest `react-native` preset |
|
|
186
|
+
|---|---|---|
|
|
187
|
+
| `Linking.openURL(123)` | throws `Invalid URL: should be a string` | accepts without validating |
|
|
188
|
+
| `Linking.openURL('')` | throws `Invalid URL: cannot be empty` | accepts without validating |
|
|
189
|
+
| `<Text>` host props | `accessible`, `allowFontScaling`, `ellipsizeMode` computed by real `Text.js` | not set |
|
|
190
|
+
|
|
191
|
+
The `mock` engine is a simplification too, so it diverges from real RN the same way — reach
|
|
192
|
+
for `native` when a false pass would be costly. These specific differences are produced by
|
|
193
|
+
the cross-check/fidelity harnesses in the repository's `bench/` directory.
|
|
130
194
|
|
|
131
195
|
---
|
|
132
196
|
|
|
133
197
|
## Mocked Components and APIs
|
|
134
198
|
|
|
135
|
-
|
|
199
|
+
Under the `mock` engine, the plugin provides a comprehensive mock of the `react-native` module. Every mocked component renders as a named host element (making snapshots readable), and every mocked API function is a Vitest `vi.fn()` spy.
|
|
136
200
|
|
|
137
201
|
### Components (21)
|
|
138
202
|
|
|
@@ -182,16 +246,16 @@ The plugin provides a complete mock of the `react-native` module. Every componen
|
|
|
182
246
|
|
|
183
247
|
## React Native Test Suite Conformance
|
|
184
248
|
|
|
185
|
-
vitest-native ports tests
|
|
249
|
+
vitest-native ports tests from React Native's own test suite (Flow stripped, Jest → Vitest) to validate mock behavioral parity — the same checks Meta uses to verify React Native itself:
|
|
186
250
|
|
|
187
251
|
- **Easing** — all 24 easing curve tests including sample data for quad, cubic, sin, exp, circle, and back
|
|
188
252
|
- **Bezier** — 9 cubic bezier mathematical property tests (symmetry, projection, boundary conditions)
|
|
189
253
|
- **flattenStyle** — 12 style merging tests covering override precedence, reference identity, and recursive flattening
|
|
190
254
|
- **processColor** — 9 color format conversion tests for named colors, RGB, RGBA, HSL, HSLA, and hex
|
|
191
|
-
- **Interpolation** —
|
|
255
|
+
- **Interpolation** — 12 numeric and string range mapping tests, including string output ranges (`deg`/`%`/arbitrary suffixes) and extrapolate modes (extend/clamp/identity)
|
|
192
256
|
- **Animated** — 36 tests for listeners, events, forkEvent, diffClamp, Color normalization, sequence chaining/interruption/restart, parallel start/stop/no-double-stop, loop iterations/indefinite/interrupt/resetBeforeIteration, delay in sequence, stagger, and value tracking
|
|
193
257
|
|
|
194
|
-
|
|
258
|
+
118 tests ported from React Native's own test suite — 115 passing, with 3 edge cases (hex-colour interpolation and infinite input ranges) skipped and documented in the test files (`vitest run tests/rn-conformance/`).
|
|
195
259
|
|
|
196
260
|
---
|
|
197
261
|
|
|
@@ -341,6 +405,112 @@ export default defineConfig({
|
|
|
341
405
|
| `presets.screens()` | react-native-screens |
|
|
342
406
|
| `presets.expo()` | Expo modules |
|
|
343
407
|
|
|
408
|
+
Presets apply under **both** engines: the mock engine and `engine: 'native'` (where they shadow each
|
|
409
|
+
library's native runtime — worklets, native modules — exactly as Jest does, while the surrounding tree
|
|
410
|
+
renders through real React Native).
|
|
411
|
+
|
|
412
|
+
### Expo
|
|
413
|
+
|
|
414
|
+
The `expo` preset shadows the common Expo modules (`expo-constants`, `expo-status-bar`, `expo-font`,
|
|
415
|
+
`expo-asset`, `expo-splash-screen`, `expo-linking`) the way `jest-expo` mocks them. A component that
|
|
416
|
+
imports those renders under the native engine with no extra setup — there's a gated proof in
|
|
417
|
+
`tests-native/expo.test.tsx`. Two honest caveats:
|
|
418
|
+
|
|
419
|
+
- **Expo modules without a preset** (e.g. `expo-image`, `expo-haptics`) still need a `vi.mock` or a
|
|
420
|
+
custom preset, just as `jest-expo` ships a mock for each. The native engine externalizes them to
|
|
421
|
+
Node, so importing the real native module would fail without a stand-in.
|
|
422
|
+
- **Expo SDK trails React Native.** Pin your `react-native` to the version your Expo SDK supports
|
|
423
|
+
(which is within this plugin's validated range), not the newest RN release.
|
|
424
|
+
|
|
425
|
+
Full Expo-app validation (a real Expo project's suite end-to-end) is on the roadmap; today the
|
|
426
|
+
guarantee is the presets above plus standard `vi.mock` for the rest.
|
|
427
|
+
|
|
428
|
+
---
|
|
429
|
+
|
|
430
|
+
## Migrating from `vitest-react-native`
|
|
431
|
+
|
|
432
|
+
Used [`vitest-community/vitest-react-native`](https://github.com/vitest-community/vitest-react-native)
|
|
433
|
+
and it broke on a newer Vitest? `vitest-native` is the maintained continuation — same core
|
|
434
|
+
architecture (externalize RN, run its real JS under Node, mock only the native boundary). For the
|
|
435
|
+
common case it's a config swap:
|
|
436
|
+
|
|
437
|
+
```diff
|
|
438
|
+
- import reactNative from 'vitest-react-native';
|
|
439
|
+
- import react from '@vitejs/plugin-react';
|
|
440
|
+
+ import { reactNative } from 'vitest-native';
|
|
441
|
+
import { defineConfig } from 'vitest/config';
|
|
442
|
+
|
|
443
|
+
export default defineConfig({
|
|
444
|
+
- plugins: [reactNative(), react()],
|
|
445
|
+
+ plugins: [reactNative()],
|
|
446
|
+
});
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
Named import, and drop `@vitejs/plugin-react` (vitest-native handles the JSX runtime itself). The
|
|
450
|
+
full walkthrough — install, gotchas, what's the same — is in
|
|
451
|
+
**[docs/migrating-from-vitest-react-native.md](docs/migrating-from-vitest-react-native.md)**.
|
|
452
|
+
|
|
453
|
+
---
|
|
454
|
+
|
|
455
|
+
## Migrating from Jest
|
|
456
|
+
|
|
457
|
+
vitest-native ships a small compat layer that clears the mechanical Jest-API coupling in an existing
|
|
458
|
+
suite (the `jest` global, `@jest/globals`, jest-native's extend-expect):
|
|
459
|
+
|
|
460
|
+
```ts
|
|
461
|
+
// vitest.config.mts
|
|
462
|
+
import { reactNative } from 'vitest-native';
|
|
463
|
+
import { jestCompatAliases, jestCompatSetup } from 'vitest-native/jest-compat';
|
|
464
|
+
|
|
465
|
+
export default defineConfig({
|
|
466
|
+
plugins: [reactNative({ engine: 'native' })],
|
|
467
|
+
resolve: { alias: { ...jestCompatAliases() } },
|
|
468
|
+
test: { globals: true, setupFiles: [jestCompatSetup] },
|
|
469
|
+
});
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
This is **not** a turnkey drop-in — a real suite still needs a small per-suite cleanup (convert
|
|
473
|
+
top-level `jest.mock` → `vi.mock`, RNTL ≥ 12, re-record snapshots once with `-u`). The full recipe,
|
|
474
|
+
with a worked real-app example, is in **[docs/migrating-from-jest.md](docs/migrating-from-jest.md)**.
|
|
475
|
+
|
|
476
|
+
---
|
|
477
|
+
|
|
478
|
+
## Troubleshooting
|
|
479
|
+
|
|
480
|
+
**`@react-native/babel-preset not found — using the mock engine`**
|
|
481
|
+
You asked for the default (`auto`) engine but the native babel deps aren't installed, so it
|
|
482
|
+
fell back to the mock engine. To run real React Native, install them:
|
|
483
|
+
|
|
484
|
+
```bash
|
|
485
|
+
npm i -D @react-native/babel-preset @babel/core
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
To silence the notice and stay on mock deliberately, set `reactNative({ engine: 'mock' })`.
|
|
489
|
+
|
|
490
|
+
**`vi.mock('some-rn-library')` doesn't take effect (native engine)**
|
|
491
|
+
Under `engine: 'native'`, React Native and its native-side libraries are *externalized* to Node,
|
|
492
|
+
so they load outside Vite's module graph — and Vitest's mocker only intercepts modules inside that
|
|
493
|
+
graph. Your **own** source modules mock normally; the gap is third-party RN-side packages. Options:
|
|
494
|
+
|
|
495
|
+
- Use a **[preset](#auto-detect-presets)** if one exists (it shadows the library the way Jest does).
|
|
496
|
+
- For a pure-JS library shipping untranspiled source, add it to the [`transform`](#plugin-options)
|
|
497
|
+
allowlist so the engine strips its Flow/TS as it loads.
|
|
498
|
+
- Or test that boundary on the **mock engine**, where `vi.mock` applies normally.
|
|
499
|
+
|
|
500
|
+
**`Unexpected token` / Flow syntax errors from a third-party library (native engine)**
|
|
501
|
+
The require hook strips Flow/TS from `react-native` and active presets, but not from arbitrary
|
|
502
|
+
packages. If a dependency ships untranspiled Flow/TS, add it to the
|
|
503
|
+
[`transform`](#plugin-options) allowlist.
|
|
504
|
+
|
|
505
|
+
**My `babel.config.js` plugin isn't running**
|
|
506
|
+
Transforms go through Vite/esbuild, not Babel, so custom Babel plugins don't apply. JSX (automatic
|
|
507
|
+
runtime) and RN/preset Flow-stripping are handled for you; for anything else, prefer a Vite plugin
|
|
508
|
+
or a preset.
|
|
509
|
+
|
|
510
|
+
**Snapshots differ after switching from Jest or the mock engine**
|
|
511
|
+
Real RN computes host props (e.g. `<Text>`'s `accessible`, `allowFontScaling`) that mocks omit, so
|
|
512
|
+
the tree is richer. Re-record once with `npx vitest -u` after migrating.
|
|
513
|
+
|
|
344
514
|
---
|
|
345
515
|
|
|
346
516
|
## RNTL Matchers
|
|
@@ -389,6 +559,77 @@ test('greeting is visible with correct style', () => {
|
|
|
389
559
|
|
|
390
560
|
---
|
|
391
561
|
|
|
562
|
+
## Animated Matchers (Reanimated)
|
|
563
|
+
|
|
564
|
+
`react-native-reanimated` ships `toHaveAnimatedStyle` and `toHaveAnimatedProps` through its Jest setup, which isn't available under Vitest. `vitest-native` registers Vitest-native equivalents automatically — no `setUpTests()` call or setup file needed.
|
|
565
|
+
|
|
566
|
+
| Matcher | Description |
|
|
567
|
+
|---|---|
|
|
568
|
+
| `toHaveAnimatedStyle(style, config?)` | Element's (flattened) style contains the given entries. Pass `{ shouldMatchAllProps: true }` to require an exact match. |
|
|
569
|
+
| `toHaveAnimatedProps(props)` | Element's animated props contain the given entries. |
|
|
570
|
+
|
|
571
|
+
Because the `reanimated` preset resolves `useAnimatedStyle`/`useAnimatedProps` synchronously, the animated values land on the rendered element and these matchers read straight from it:
|
|
572
|
+
|
|
573
|
+
```tsx
|
|
574
|
+
import { render, screen } from '@testing-library/react-native';
|
|
575
|
+
import Animated, { useSharedValue, useAnimatedStyle } from 'react-native-reanimated';
|
|
576
|
+
|
|
577
|
+
function Skeleton({ visible }: { visible: boolean }) {
|
|
578
|
+
const opacity = useSharedValue(visible ? 1 : 0);
|
|
579
|
+
const style = useAnimatedStyle(() => ({ opacity: opacity.value }));
|
|
580
|
+
return <Animated.View testID="bone" style={style} />;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
test('starts with full opacity', () => {
|
|
584
|
+
render(<Skeleton visible />);
|
|
585
|
+
expect(screen.getByTestId('bone')).toHaveAnimatedStyle({ opacity: 1 });
|
|
586
|
+
});
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
### Reanimated + Gesture Handler without manual mocks
|
|
590
|
+
|
|
591
|
+
Both libraries are auto-detected, so a component using a `Gesture` + `GestureDetector` driving an animated style needs no `vi.mock()` calls:
|
|
592
|
+
|
|
593
|
+
```tsx
|
|
594
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
595
|
+
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
|
|
596
|
+
import Animated, { useSharedValue, useAnimatedStyle } from 'react-native-reanimated';
|
|
597
|
+
|
|
598
|
+
function Card() {
|
|
599
|
+
const pressed = useSharedValue(false);
|
|
600
|
+
const style = useAnimatedStyle(() => ({ opacity: pressed.value ? 0.5 : 1 }));
|
|
601
|
+
const tap = Gesture.Tap().onStart(() => {
|
|
602
|
+
pressed.value = true;
|
|
603
|
+
});
|
|
604
|
+
return (
|
|
605
|
+
<GestureDetector gesture={tap}>
|
|
606
|
+
<Animated.View testID="card" style={style} />
|
|
607
|
+
</GestureDetector>
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
test('card renders at full opacity', () => {
|
|
612
|
+
render(<Card />);
|
|
613
|
+
expect(screen.getByTestId('card')).toHaveAnimatedStyle({ opacity: 1 });
|
|
614
|
+
});
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
> RNGH's `Pressable` is also exported by the preset and mirrors React Native's `Pressable` (including not firing `onPress` when `disabled`), so you can swap `import { Pressable } from 'react-native-gesture-handler'` straight into tests.
|
|
618
|
+
|
|
619
|
+
### TypeScript
|
|
620
|
+
|
|
621
|
+
To type the matchers on `expect()`, add the matcher types to your `tsconfig.json`:
|
|
622
|
+
|
|
623
|
+
```jsonc
|
|
624
|
+
{
|
|
625
|
+
"compilerOptions": {
|
|
626
|
+
"types": ["vitest-native/matchers"]
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
---
|
|
632
|
+
|
|
392
633
|
## Snapshot Serializer
|
|
393
634
|
|
|
394
635
|
A snapshot serializer is auto-registered that produces clean, JSX-like output for React Native components. Instead of deeply nested `ReactTestInstance` objects, snapshots read naturally:
|
|
@@ -477,6 +718,8 @@ This prints details about which presets were detected, which modules are being m
|
|
|
477
718
|
| `vite` | >= 5 |
|
|
478
719
|
| `vitest` | >= 4 |
|
|
479
720
|
| `node` | >= 20 |
|
|
721
|
+
| `react-native` (native engine) | 0.81–0.84 validated |
|
|
722
|
+
| `@react-native/babel-preset` (native engine) | `*` |
|
|
480
723
|
| `@testing-library/react-native` (optional) | >= 12 |
|
|
481
724
|
|
|
482
725
|
---
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __exportAll = (all, no_symbols) => {
|
|
9
|
+
let target = {};
|
|
10
|
+
for (var name in all) __defProp(target, name, {
|
|
11
|
+
get: all[name],
|
|
12
|
+
enumerable: true
|
|
13
|
+
});
|
|
14
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
15
|
+
return target;
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
19
|
+
key = keys[i];
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
21
|
+
get: ((k) => from[k]).bind(null, key),
|
|
22
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
//#endregion
|
|
32
|
+
Object.defineProperty(exports, "__exportAll", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function() {
|
|
35
|
+
return __exportAll;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(exports, "__toESM", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function() {
|
|
41
|
+
return __toESM;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
@@ -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 {
|
|
@@ -207,6 +205,17 @@ interface Preset {
|
|
|
207
205
|
interface VitestNativeOptions {
|
|
208
206
|
/** Target platform. Default: 'ios' */
|
|
209
207
|
platform?: "ios" | "android";
|
|
208
|
+
/**
|
|
209
|
+
* Test engine.
|
|
210
|
+
* - 'mock' — pure-JS reimplementation of React Native (fastest, lower fidelity).
|
|
211
|
+
* - 'native' — runs real React Native JS, mocking only the native boundary
|
|
212
|
+
* (Jest-level fidelity).
|
|
213
|
+
* - 'auto' — picks automatically. Currently resolves to 'mock'; when
|
|
214
|
+
* '@react-native/babel-preset' is present it recommends 'native'
|
|
215
|
+
* (becomes the default in v1).
|
|
216
|
+
* Default: 'auto'.
|
|
217
|
+
*/
|
|
218
|
+
engine?: "auto" | "mock" | "native";
|
|
210
219
|
/**
|
|
211
220
|
* Built-in third-party library presets. When provided, only these presets
|
|
212
221
|
* are used. When omitted, vitest-native auto-detects installed packages
|
|
@@ -239,6 +248,39 @@ interface VitestNativeOptions {
|
|
|
239
248
|
diagnostics?: boolean;
|
|
240
249
|
/** Additional asset file extensions to stub (e.g. ['.lottie', '.m4b']) */
|
|
241
250
|
assetExts?: string[];
|
|
251
|
+
/**
|
|
252
|
+
* `engine: 'native'` only. Names of additional node_modules packages whose
|
|
253
|
+
* source the native engine should transform (Flow/TS/JSX stripped) as it
|
|
254
|
+
* loads them. By default only `react-native` / `@react-native` are
|
|
255
|
+
* transformed; third-party RN libraries that ship untranspiled source
|
|
256
|
+
* (e.g. `react-native-reanimated`, `react-native-safe-area-context`) need to
|
|
257
|
+
* be listed here, analogous to Jest's `transformIgnorePatterns` allowlist.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* reactNative({ engine: 'native', transform: ['react-native-reanimated'] })
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
transform?: string[];
|
|
265
|
+
/**
|
|
266
|
+
* `engine: 'native'` only. **Experimental.** Run tests in persistent
|
|
267
|
+
* RN-hot workers: React Native's module graph loads once per worker and
|
|
268
|
+
* stays resident across test files, while each file still gets a fresh
|
|
269
|
+
* Vitest module runner (per-file isolation of your app/test modules).
|
|
270
|
+
* Uses a custom Vitest pool + worker entry + runner; requires Vitest >= 4.
|
|
271
|
+
*
|
|
272
|
+
* Pass an object to tune worker recycling (self-defense against suites that
|
|
273
|
+
* leak process-wide resources the per-file reset can't reclaim):
|
|
274
|
+
* - `recycleAfterFiles`: retire a worker after it has run N test files.
|
|
275
|
+
* - `memoryLimit`: retire a worker when its JS heap (bytes) after a test
|
|
276
|
+
* file meets or exceeds this value.
|
|
277
|
+
*
|
|
278
|
+
* Default: false (each file runs in a fresh worker; RN reloads per file).
|
|
279
|
+
*/
|
|
280
|
+
hotRuntime?: boolean | {
|
|
281
|
+
recycleAfterFiles?: number;
|
|
282
|
+
memoryLimit?: number;
|
|
283
|
+
};
|
|
242
284
|
}
|
|
243
285
|
//#endregion
|
|
244
286
|
//#region src/presets/reanimated.d.ts
|
|
@@ -261,8 +303,20 @@ declare function screens(): Preset;
|
|
|
261
303
|
//#endregion
|
|
262
304
|
//#region src/presets/expo.d.ts
|
|
263
305
|
declare function expo(): Preset;
|
|
306
|
+
//#endregion
|
|
307
|
+
//#region src/presets/device-info.d.ts
|
|
308
|
+
declare function deviceInfo(): Preset;
|
|
309
|
+
//#endregion
|
|
310
|
+
//#region src/presets/mmkv.d.ts
|
|
311
|
+
declare function mmkv(): Preset;
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region src/presets/svg.d.ts
|
|
314
|
+
declare function svg(): Preset;
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/presets/webview.d.ts
|
|
317
|
+
declare function webview(): Preset;
|
|
264
318
|
declare namespace index_d_exports {
|
|
265
|
-
export { asyncStorage, expo, gestureHandler, navigation, reanimated, safeAreaContext, screens };
|
|
319
|
+
export { asyncStorage, deviceInfo, expo, gestureHandler, mmkv, navigation, reanimated, safeAreaContext, screens, svg, webview };
|
|
266
320
|
}
|
|
267
321
|
//#endregion
|
|
268
|
-
export {
|
|
322
|
+
export { deviceInfo as a, asyncStorage as c, gestureHandler as d, reanimated as f, VitestNativeOptions as h, mmkv as i, navigation as l, ReactNativeMock as m, webview as n, expo as o, Preset as p, svg as r, screens as s, index_d_exports as t, safeAreaContext as u };
|