vitest-native 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/README.md +268 -9
  3. package/dist/chunk-CZWwpsFl.cjs +43 -0
  4. package/dist/helpers.cjs +10 -0
  5. package/dist/helpers.d.cts +7 -1
  6. package/dist/helpers.d.mts +7 -1
  7. package/dist/helpers.mjs +10 -1
  8. package/dist/{index-CJqS-y_M.d.cts → index-CJIhukN_.d.mts} +58 -4
  9. package/dist/{index-h4vpIkQO.d.mts → index-DEB7OFaJ.d.cts} +60 -2
  10. package/dist/index.cjs +296 -35
  11. package/dist/index.d.cts +1 -1
  12. package/dist/index.d.mts +1 -1
  13. package/dist/index.mjs +292 -33
  14. package/dist/jest-compat/interop.mjs +30 -0
  15. package/dist/jest-compat/jest-globals.d.mts +3 -0
  16. package/dist/jest-compat/jest-globals.mjs +9 -0
  17. package/dist/jest-compat/noop.d.mts +3 -0
  18. package/dist/jest-compat/noop.mjs +7 -0
  19. package/dist/jest-compat/setup.d.mts +3 -0
  20. package/dist/jest-compat/setup.mjs +41 -0
  21. package/dist/jest-compat.cjs +109 -0
  22. package/dist/jest-compat.d.cts +38 -0
  23. package/dist/jest-compat.d.mts +38 -0
  24. package/dist/jest-compat.mjs +104 -0
  25. package/dist/matchers.cjs +69 -0
  26. package/dist/matchers.d.cts +51 -0
  27. package/dist/matchers.d.mts +51 -0
  28. package/dist/matchers.mjs +66 -0
  29. package/dist/native/boundary.mjs +225 -0
  30. package/dist/native/globals.mjs +112 -0
  31. package/dist/native/hooks.mjs +85 -0
  32. package/dist/native/loader.mjs +123 -0
  33. package/dist/native/match.mjs +9 -0
  34. package/dist/native/reset.mjs +170 -0
  35. package/dist/native/resolve.mjs +22 -0
  36. package/dist/native/runner.mjs +18 -0
  37. package/dist/native/setup.mjs +134 -0
  38. package/dist/native/transform.mjs +79 -0
  39. package/dist/native/worker.mjs +96 -0
  40. package/dist/pool-B4mWN3N5.cjs +60 -0
  41. package/dist/pool-oPTZiSXU.mjs +58 -0
  42. package/dist/{preset-map-Bt4dXkZO.cjs → preset-map-C3jxnkiX.cjs} +6 -1
  43. package/dist/{preset-map-D224CC5w.mjs → preset-map-OJm5IWuL.mjs} +6 -1
  44. package/dist/{presets-9Y4ssfiH.cjs → presets-2I68Ipyl.cjs} +768 -122
  45. package/dist/{presets-CWH0gtM0.mjs → presets-DOGXQzYT.mjs} +732 -87
  46. package/dist/presets.cjs +5 -1
  47. package/dist/presets.d.cts +2 -2
  48. package/dist/presets.d.mts +2 -2
  49. package/dist/presets.mjs +2 -2
  50. package/dist/setup.cjs +903 -121
  51. package/dist/setup.mjs +899 -118
  52. package/docs/migrating-from-jest.md +123 -0
  53. package/docs/migrating-from-vitest-react-native.md +109 -0
  54. package/package.json +67 -8
@@ -0,0 +1,123 @@
1
+ # Migrating an existing Jest suite to vitest-native
2
+
3
+ **Honest expectation first:** this is **not a turnkey drop-in.** Real React Native Jest suites
4
+ couple to Jest at several levels (the `jest` global, `@jest/globals`, `jest.mock('react-native')`,
5
+ `@react-native/jest-preset`, jest-native matchers, recorded snapshots). The `vitest-native/jest-compat`
6
+ layer clears the *API coupling* mechanically; the rest is a small, well-defined per-suite cleanup.
7
+
8
+ This guide is grounded in real migration runs against production apps — **react-native-paper**
9
+ (fresh component tests), the **obytes template**, and **Rocket.Chat** (existing Jest suites).
10
+
11
+ ---
12
+
13
+ ## 1. The jest-compat layer (clears API coupling)
14
+
15
+ ```ts
16
+ // vitest.config.mts
17
+ import { defineConfig } from "vitest/config";
18
+ import { reactNative } from "vitest-native";
19
+ import { jestCompatAliases, jestCompatSetup, jestMockTransform } from "vitest-native/jest-compat";
20
+
21
+ export default defineConfig({
22
+ plugins: [reactNative({ engine: "native" }), jestMockTransform()], // or engine: "mock"
23
+ resolve: {
24
+ dedupe: ["react", "react-test-renderer", "react-is"],
25
+ alias: { ...jestCompatAliases() },
26
+ },
27
+ test: {
28
+ globals: true,
29
+ environment: "node",
30
+ setupFiles: [jestCompatSetup],
31
+ },
32
+ });
33
+ ```
34
+
35
+ What each piece does:
36
+
37
+ | Piece | Clears |
38
+ |---|---|
39
+ | `jestCompatSetup` (a setup file) | Installs a `jest` global backed by Vitest's `vi`, plus the sync `jest.requireActual` / `jest.requireMock` that Vitest only ships as the async `vi.importActual`. |
40
+ | `jestMockTransform()` (a plugin) | Rewrites top-level `jest.mock(...)` to a **hoisted** `vi.mock(...)` so it actually applies — see 2a. The single biggest mechanical blocker, automated. |
41
+ | `jestCompatAliases()` → `@jest/globals` | Redirects `@jest/globals` (imported by **@testing-library/react-native < 12**) to a shim re-exporting Vitest's globals. |
42
+ | `jestCompatAliases()` → `@testing-library/jest-native/extend-expect` | No-ops it — vitest-native already registers the jest-native matchers (`toHaveStyle`, `toBeVisible`, …). |
43
+
44
+ After this, `jest.fn`, `jest.spyOn`, `jest.useFakeTimers`, `jest.requireActual`, and top-level
45
+ `jest.mock` / `jest.unmock` / `jest.doMock` calls all work unchanged.
46
+
47
+ ---
48
+
49
+ ## 2. Per-suite cleanup still required
50
+
51
+ These are the things the compat layer **cannot** do for you. Each is small and mechanical.
52
+
53
+ ### 2a. Top-level `jest.mock(...)` — automated by `jestMockTransform()`
54
+ Vitest only **hoists** mock calls made on the `vi` / `vitest` identifier. A top-level
55
+ `jest.mock('react-native', factory)` would otherwise run *after* imports and silently not apply.
56
+
57
+ **`jestMockTransform()` (section 1) handles this for you** — it rewrites top-level
58
+ `jest.mock` / `jest.unmock` / `jest.doMock` / `jest.doUnmock` to the hoisted `vi.*` form, and
59
+ runs each `mock`/`doMock` factory's return through **Jest's CommonJS interop** so the two most
60
+ common Jest manual-mock shapes resolve the way they do under Jest:
61
+
62
+ ```ts
63
+ jest.mock('./Icon', () => () => null); // function factory → usable as `import Icon from`
64
+ jest.mock('./api', () => ({ get: vi.fn() })); // named-only → `import api from` gets the object
65
+ ```
66
+
67
+ (Jest treats a factory return as `module.exports`; Vitest treats it as an ES namespace. The
68
+ wrapper bridges that. A factory already returning an ES shape — `__esModule` or an explicit
69
+ `default` — is left as-is.) So you can leave existing `jest.mock(...)` calls unchanged. It keys
70
+ on the literal `jest.mock` member form, not `jest` aliased to another local name.
71
+
72
+ …and in most cases you can **delete** third-party native-lib mocks entirely — see 2c.
73
+
74
+ ### 2b. Upgrade RNTL to ≥ 12 (recommended)
75
+ The `@jest/globals` alias unblocks RNTL < 12, but **upgrading to `@testing-library/react-native@^12`
76
+ is the cleaner fix** (12 dropped the `@jest/globals` dependency). vitest-native targets RNTL ≥ 12.
77
+
78
+ ### 2c. Delete third-party native-lib mocks — they're automatic now
79
+ You do **not** need `jest.mock('react-native-reanimated', …)`, safe-area's `jest/mock`,
80
+ gesture-handler's jestSetup, etc. vitest-native **auto-detects** installed third-party libraries and
81
+ shadows their native runtimes with built-in presets, under **both** engines. Just have the package
82
+ installed; delete the manual mock. (Reanimated, Gesture Handler, Safe Area, Navigation, Screens,
83
+ AsyncStorage, Expo are covered out of the box. Validated under the native engine —
84
+ `tests-native/third-party-stack.test.tsx`.)
85
+
86
+ ### 2d. Re-record snapshots (`vitest -u`)
87
+ This is the most common surprise. Under `engine: 'native'`, real React Native renders **real host
88
+ component names** (`RCTText`, `RCTView`, `RCTScrollView`), whereas `@react-native/jest-preset`
89
+ snapshots show mock names (`Text`, `View`). Existing snapshots will mismatch on names only. Run once
90
+ with `-u` to re-record; the rendered structure is otherwise equivalent. (In the Paper run, every
91
+ own-test passed after a single `-u`.)
92
+
93
+ > Tip: prefer explicit queries (`getByText`, `getByTestId`, `getByRole`) over large snapshots —
94
+ > they're robust across the engine's real host names and far easier to review.
95
+
96
+ ### 2e. Drop `@react-native/jest-preset` and `transform`/`transformIgnorePatterns`
97
+ Those are Jest/Metro config. vitest-native handles RN + third-party transformation itself
98
+ (`engine: 'native'` transforms real RN in Node's loader hooks; the mock engine virtualizes RN).
99
+ For *run-real pure-JS* third-party libs under the native engine, use the plugin's `transform: [...]`
100
+ allowlist instead of `transformIgnorePatterns`.
101
+
102
+ ### 2f. Jest config options
103
+ `jest.config.js` keys (`setupFilesAfterEnv`, `moduleNameMapper`, `testEnvironment`, etc.) move to the
104
+ Vitest config: `setupFiles`, `resolve.alias`, `test.environment: 'node'`. `jest.setTimeout(ms)` is a
105
+ no-op under the shim (use `test.testTimeout` in config or per-test `{ timeout }`).
106
+
107
+ ---
108
+
109
+ ## 3. Suggested migration recipe
110
+
111
+ 1. Add the jest-compat config (section 1). Upgrade RNTL to ≥ 12 (2b).
112
+ 2. Delete manual third-party native-lib mocks and `@react-native/jest-preset` (2c, 2e).
113
+ 3. Convert any **top-level** `jest.mock(...)` to `vi.mock(...)`; leave runtime `jest.*` as-is (2a).
114
+ 4. Run `vitest run -u` once to re-record snapshots (2d), then `vitest run` to confirm green.
115
+ 5. Triage remaining failures — usually a missing query update or a suite-specific mock.
116
+
117
+ ## 4. What "done" looks like
118
+
119
+ A migrated suite runs under Vitest with: the jest-compat aliases + setup, RNTL ≥ 12, no manual
120
+ third-party native mocks, top-level mocks converted, and snapshots re-recorded. You get Vitest's
121
+ speed/watch/UI while keeping `engine: 'native'` real-RN fidelity (or `engine: 'mock'` for the fast
122
+ path). The compat layer is intentionally small — it removes the mechanical Jest coupling so the only
123
+ work left is the genuinely suite-specific bits.
@@ -0,0 +1,109 @@
1
+ # Migrating from `vitest-react-native`
2
+
3
+ If you used [`vitest-community/vitest-react-native`](https://github.com/vitest-community/vitest-react-native)
4
+ and it stopped working on a newer Vitest, this is the continuation you're looking for.
5
+
6
+ **The short version:** it's the same idea, maintained. The original plugin and
7
+ `vitest-native`'s default engine share the same architecture — externalize React
8
+ Native to Node, strip its Flow types through a require hook, and mock only the
9
+ thin native boundary (`View`/`Text`/`UIManager`/`NativeModules`/…). The original
10
+ simply went unmaintained through Vitest's module-runner rewrites (v2 → v3 → v4)
11
+ and bit-rotted. `vitest-native` tracks current Vitest and React Native, and a CI
12
+ matrix gates it across both so it stays alive.
13
+
14
+ For the common case — render real RN with RNTL, mock your *own* modules with
15
+ `vi.mock` — migration is a config swap.
16
+
17
+ ---
18
+
19
+ ## 1. Swap the package
20
+
21
+ ```bash
22
+ # remove the old plugin
23
+ npm rm vitest-react-native @vitejs/plugin-react
24
+
25
+ # add this one (+ the babel preset it uses to strip RN's Flow types)
26
+ npm i -D vitest-native @react-native/babel-preset @babel/core
27
+ ```
28
+
29
+ Keep your existing `vitest`, `vite`, `react`, `react-native`, and
30
+ `@testing-library/react-native`. Requirements: **Vitest ≥ 4, Vite ≥ 5, React 19,
31
+ React Native 0.81–0.84 (validated), RNTL ≥ 12.**
32
+
33
+ ## 2. Update `vitest.config`
34
+
35
+ **Before** (`vitest-react-native`):
36
+
37
+ ```ts
38
+ import reactNative from 'vitest-react-native';
39
+ import react from '@vitejs/plugin-react';
40
+ import { defineConfig } from 'vitest/config';
41
+
42
+ export default defineConfig({
43
+ plugins: [reactNative(), react()],
44
+ });
45
+ ```
46
+
47
+ **After** (`vitest-native`):
48
+
49
+ ```ts
50
+ import { reactNative } from 'vitest-native';
51
+ import { defineConfig } from 'vitest/config';
52
+
53
+ export default defineConfig({
54
+ plugins: [reactNative()],
55
+ });
56
+ ```
57
+
58
+ Two changes:
59
+
60
+ - **Named import** — `import { reactNative }`, not a default import.
61
+ - **Drop `@vitejs/plugin-react`** — `vitest-native` configures the JSX runtime
62
+ itself (both engines). Adding `plugin-react` alongside it is unnecessary and
63
+ can double-transform.
64
+
65
+ That's it. `reactNative()` with no options resolves to the **native engine** (real
66
+ RN) whenever `@react-native/babel-preset` is installed, which it now is.
67
+
68
+ ## 3. Run
69
+
70
+ ```bash
71
+ npx vitest run
72
+ ```
73
+
74
+ Your existing tests render against real React Native exactly as before.
75
+
76
+ ---
77
+
78
+ ## What's the same
79
+
80
+ - Real RN component rendering via `@testing-library/react-native`.
81
+ - `vi.mock()` of **your own** modules (anything in your app's source graph).
82
+ - Standard Vitest everything — `expect`, `vi.fn`, coverage, watch, UI.
83
+
84
+ ## What's new (and optional)
85
+
86
+ - **Native is the zero-config default**, but a fast **mock engine** is one option
87
+ away — `reactNative({ engine: 'mock' })` — for pure-logic and environment-control
88
+ tests. A CI-gated cross-check keeps the mock behaviorally honest against real RN.
89
+ - **Presets** auto-shadow common native libraries (Reanimated, Gesture Handler,
90
+ Safe Area, Navigation, …) the way `jest` mocks them — no per-lib wiring.
91
+ - An opt-in **hot runtime** (`reactNative({ hotRuntime: true })`) keeps RN warm
92
+ across files for large suites.
93
+
94
+ ## Gotchas (the margins)
95
+
96
+ These are inherent to running RN externalized to Node — they applied to the
97
+ original plugin too:
98
+
99
+ - **`vi.mock` of an externalized RN-side library may not intercept.** Libraries
100
+ that load through Node (not your Vite source graph) bypass Vitest's mocker.
101
+ Prefer a **preset** (if one exists) or mock at the boundary. Your *own* modules
102
+ mock normally.
103
+ - **Custom Babel plugins don't run.** Transforms go through Vite/esbuild, not your
104
+ `babel.config.js`. Flow/TS stripping for RN and allow-listed packages is handled
105
+ by the require hook; use the `transform` allowlist for extra pure-JS packages
106
+ that ship untranspiled source.
107
+
108
+ If something that worked under the old plugin doesn't here, open an issue — parity
109
+ with `vitest-react-native` is a goal, and the cross-check corpus is how we prove it.
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "vitest-native",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
- "description": "A Vitest plugin for React Native. One install, zero config.",
5
+ "description": "Run real React Native tests under Vitest. One install, zero config.",
6
6
  "license": "MIT",
7
7
  "author": "Daniel Fry",
8
8
  "repository": {
@@ -77,10 +77,43 @@
77
77
  "types": "./dist/presets.d.cts",
78
78
  "default": "./dist/presets.cjs"
79
79
  }
80
+ },
81
+ "./matchers": {
82
+ "import": {
83
+ "types": "./dist/matchers.d.mts",
84
+ "default": "./dist/matchers.mjs"
85
+ },
86
+ "require": {
87
+ "types": "./dist/matchers.d.cts",
88
+ "default": "./dist/matchers.cjs"
89
+ }
90
+ },
91
+ "./jest-compat": {
92
+ "import": {
93
+ "types": "./dist/jest-compat.d.mts",
94
+ "default": "./dist/jest-compat.mjs"
95
+ },
96
+ "require": {
97
+ "types": "./dist/jest-compat.d.cts",
98
+ "default": "./dist/jest-compat.cjs"
99
+ }
100
+ },
101
+ "./jest-compat/setup": {
102
+ "types": "./dist/jest-compat/setup.d.mts",
103
+ "default": "./dist/jest-compat/setup.mjs"
104
+ },
105
+ "./jest-compat/jest-globals": {
106
+ "types": "./dist/jest-compat/jest-globals.d.mts",
107
+ "default": "./dist/jest-compat/jest-globals.mjs"
108
+ },
109
+ "./jest-compat/extend-expect-noop": {
110
+ "types": "./dist/jest-compat/noop.d.mts",
111
+ "default": "./dist/jest-compat/noop.mjs"
80
112
  }
81
113
  },
82
114
  "files": [
83
115
  "dist",
116
+ "docs",
84
117
  "LICENSE",
85
118
  "CHANGELOG.md"
86
119
  ],
@@ -95,34 +128,60 @@
95
128
  "react": ">=18",
96
129
  "vite": ">=5",
97
130
  "vitest": ">=4",
98
- "@testing-library/react-native": ">=12"
131
+ "@testing-library/react-native": ">=12",
132
+ "@react-native/babel-preset": "*",
133
+ "@babel/core": "*"
99
134
  },
100
135
  "peerDependenciesMeta": {
101
136
  "@testing-library/react-native": {
102
137
  "optional": true
138
+ },
139
+ "@react-native/babel-preset": {
140
+ "optional": true
141
+ },
142
+ "@babel/core": {
143
+ "optional": true
103
144
  }
104
145
  },
105
146
  "devDependencies": {
147
+ "@babel/core": "^7.29.7",
148
+ "@react-native/babel-preset": "^0.85.3",
149
+ "@react-navigation/native": "7.2.5",
150
+ "@react-navigation/native-stack": "7.16.0",
106
151
  "@testing-library/react-native": "^12.9.0",
107
- "@types/node": "^25.3.5",
108
- "@types/react": "^19.2.14",
152
+ "@types/node": "25.3.5",
153
+ "@types/react": "19.2.14",
154
+ "@vitest/coverage-v8": "4.0.18",
155
+ "expo-constants": "56.0.16",
156
+ "expo-status-bar": "56.0.4",
109
157
  "react": "^19.0.0",
110
158
  "react-native": "^0.84.1",
159
+ "react-native-gesture-handler": "3.0.0",
160
+ "react-native-reanimated": "4.3.1",
161
+ "react-native-safe-area-context": "5.8.0",
162
+ "react-native-worklets": "0.9.1",
111
163
  "react-test-renderer": "^19.0.0",
112
- "tsdown": "^0.21.0",
113
- "typescript": "^5.7.0",
164
+ "tsdown": "0.21.0",
165
+ "typescript": "5.9.3",
114
166
  "vite": "^6.0.0",
115
167
  "vitest": "^4.0.0"
116
168
  },
117
169
  "scripts": {
118
170
  "build": "tsdown",
119
171
  "test": "vitest run",
172
+ "test:native": "vitest run --config tests-native/vitest.config.mts",
173
+ "test:native:hot": "vitest run --config tests-native/vitest.hot.config.mts",
120
174
  "lint": "oxlint --deny-warnings src/ tests/",
121
175
  "format": "oxfmt --write src/ tests/",
122
176
  "format:check": "oxfmt --check src/ tests/",
123
177
  "check-compat": "bun scripts/check-compat.ts",
124
178
  "typecheck": "tsc --noEmit",
125
179
  "clean": "rm -rf dist",
126
- "prepublishOnly": "bun run build && bun run test && bun run lint && bun run typecheck"
180
+ "prepublishOnly": "bun run build && bun run test && bun run lint && bun run typecheck",
181
+ "crosscheck": "node scripts/crosscheck.mjs"
182
+ },
183
+ "dependencies": {
184
+ "flow-remove-types": "^2.304.0",
185
+ "magic-string": "^0.30.21"
127
186
  }
128
187
  }