vitest-native 0.4.1 → 0.6.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 (37) hide show
  1. package/CHANGELOG.md +98 -1
  2. package/README.md +270 -180
  3. package/dist/helpers.cjs +13 -0
  4. package/dist/helpers.mjs +13 -0
  5. package/dist/{index-DEB7OFaJ.d.cts → index-DGNLmGjq.d.mts} +19 -12
  6. package/dist/{index-CJIhukN_.d.mts → index-Dr3WvKWI.d.cts} +21 -10
  7. package/dist/index.cjs +106 -39
  8. package/dist/index.d.cts +1 -1
  9. package/dist/index.d.mts +1 -1
  10. package/dist/index.mjs +103 -36
  11. package/dist/jest-compat.cjs +1 -1
  12. package/dist/native/boundary.mjs +82 -32
  13. package/dist/native/globals.mjs +1 -0
  14. package/dist/native/hooks.mjs +35 -7
  15. package/dist/native/loader.mjs +18 -5
  16. package/dist/native/reset.mjs +79 -43
  17. package/dist/native/resolve.mjs +25 -7
  18. package/dist/native/runner.mjs +13 -2
  19. package/dist/native/setup.mjs +95 -4
  20. package/dist/native/transform.mjs +15 -9
  21. package/dist/native/worker.mjs +20 -3
  22. package/dist/{pool-oPTZiSXU.mjs → pool-B7LBfcMQ.mjs} +10 -1
  23. package/dist/{pool-B4mWN3N5.cjs → pool-p54QV1dQ.cjs} +11 -2
  24. package/dist/{preset-map-OJm5IWuL.mjs → preset-map-C1UwEald.mjs} +3 -1
  25. package/dist/{preset-map-C3jxnkiX.cjs → preset-map-CNBOeqAu.cjs} +3 -1
  26. package/dist/{presets-2I68Ipyl.cjs → presets-BPVVlfI2.cjs} +93 -23
  27. package/dist/{presets-DOGXQzYT.mjs → presets-DVLDrPC-.mjs} +86 -22
  28. package/dist/presets.cjs +2 -1
  29. package/dist/presets.d.cts +2 -2
  30. package/dist/presets.d.mts +2 -2
  31. package/dist/presets.mjs +2 -2
  32. package/dist/setup.cjs +71 -13
  33. package/dist/setup.mjs +69 -11
  34. package/docs/migrating-from-jest.md +6 -4
  35. package/docs/migrating-from-vitest-react-native.md +6 -3
  36. package/docs/release-readiness.md +52 -0
  37. package/package.json +31 -20
package/dist/setup.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { n as __require } from "./chunk-CjcI7cDX.mjs";
2
- import { t as AUTO_DETECT_PRESETS } from "./preset-map-OJm5IWuL.mjs";
3
- import { f as createPressableMock, p as buildPressableHostProps, t as presets_exports } from "./presets-DOGXQzYT.mjs";
2
+ import { t as AUTO_DETECT_PRESETS } from "./preset-map-C1UwEald.mjs";
3
+ import { m as buildPressableHostProps, p as createPressableMock, t as presets_exports } from "./presets-DVLDrPC-.mjs";
4
4
  import { serializer } from "./serializer.mjs";
5
5
  import { animatedMatchers } from "./matchers.mjs";
6
6
  import { createRequire } from "node:module";
@@ -253,6 +253,7 @@ function createTouchableNativeFeedbackMock() {
253
253
  }
254
254
  return React.createElement("TouchableNativeFeedback", {
255
255
  accessible: true,
256
+ ...disabled ? { pointerEvents: "none" } : {},
256
257
  ...rest,
257
258
  ...mergedA11yState ? { accessibilityState: mergedA11yState } : {},
258
259
  ref
@@ -705,13 +706,14 @@ function colorToRgba(input) {
705
706
  parseInt(n.slice(6, 8), 16) / 255
706
707
  ];
707
708
  }
708
- if (m = s.match(/^rgb\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)$/)) return [
709
+ const compact = s.replace(/\s+/g, "");
710
+ if (m = compact.match(/^rgb\(([\d.]+),([\d.]+),([\d.]+)\)$/)) return [
709
711
  +m[1],
710
712
  +m[2],
711
713
  +m[3],
712
714
  1
713
715
  ];
714
- if (m = s.match(/^rgba\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)$/)) return [
716
+ if (m = compact.match(/^rgba\(([\d.]+),([\d.]+),([\d.]+),([\d.]+)\)$/)) return [
715
717
  +m[1],
716
718
  +m[2],
717
719
  +m[3],
@@ -936,7 +938,7 @@ const namedColorMap = {
936
938
  ]
937
939
  };
938
940
  function parseColorString(color) {
939
- const rgba = color.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)$/);
941
+ const rgba = color.replace(/\s+/g, "").match(/^rgba?\((\d+),(\d+),(\d+)(?:,([\d.]+))?\)$/);
940
942
  if (rgba) return [
941
943
  parseInt(rgba[1]),
942
944
  parseInt(rgba[2]),
@@ -1054,9 +1056,39 @@ function createAnimation(onStart) {
1054
1056
  reset: vi.fn()
1055
1057
  };
1056
1058
  }
1059
+ function resolveAnimatedLeaf(v) {
1060
+ if (v instanceof AnimatedValue) return v.getValue();
1061
+ if (v && typeof v === "object" && typeof v.__getValue === "function") return v.__getValue();
1062
+ if (v && typeof v === "object" && typeof v.getValue === "function") return v.getValue();
1063
+ return v;
1064
+ }
1065
+ function resolveAnimatedStyle(style) {
1066
+ if (Array.isArray(style)) return style.map(resolveAnimatedStyle);
1067
+ const asLeaf = resolveAnimatedLeaf(style);
1068
+ if (asLeaf !== style) return asLeaf;
1069
+ if (style && typeof style === "object") {
1070
+ const out = {};
1071
+ for (const key of Object.keys(style)) {
1072
+ const val = style[key];
1073
+ if (key === "transform" && Array.isArray(val)) out[key] = val.map((t) => t && typeof t === "object" && !(t instanceof AnimatedValue) ? Object.fromEntries(Object.keys(t).map((tk) => [tk, resolveAnimatedLeaf(t[tk])])) : resolveAnimatedLeaf(t));
1074
+ else out[key] = resolveAnimatedLeaf(val);
1075
+ }
1076
+ return out;
1077
+ }
1078
+ return style;
1079
+ }
1057
1080
  function createAnimatedWrapper(displayName) {
1081
+ const hostName = displayName.replace(/^Animated\./, "");
1058
1082
  const Component = React.forwardRef((props, ref) => {
1059
- return React.createElement(displayName, {
1083
+ if (props && props.style !== void 0) {
1084
+ const { style, ...rest } = props;
1085
+ return React.createElement(hostName, {
1086
+ ...rest,
1087
+ style: resolveAnimatedStyle(style),
1088
+ ref
1089
+ });
1090
+ }
1091
+ return React.createElement(hostName, {
1060
1092
  ...props,
1061
1093
  ref
1062
1094
  });
@@ -2522,6 +2554,8 @@ function installPresetCjsBridge(moduleName, mockObject) {
2522
2554
  */
2523
2555
  const platform = process.env.VITEST_NATIVE_PLATFORM || "ios";
2524
2556
  const diagnostics = process.env.VITEST_NATIVE_DIAGNOSTICS === "true";
2557
+ const projectRoot = process.env.VITEST_NATIVE_PROJECT_ROOT || process.cwd();
2558
+ const projectReq = createRequire(path.join(projectRoot, "package.json"));
2525
2559
  let customMocks = {};
2526
2560
  if (process.env.VITEST_NATIVE_MOCKS) try {
2527
2561
  customMocks = JSON.parse(process.env.VITEST_NATIVE_MOCKS);
@@ -2568,6 +2602,12 @@ Object.defineProperties(g, {
2568
2602
  writable: true
2569
2603
  }
2570
2604
  });
2605
+ if (typeof g.expect === "undefined") Object.defineProperty(g, "expect", {
2606
+ configurable: true,
2607
+ enumerable: false,
2608
+ value: expect,
2609
+ writable: true
2610
+ });
2571
2611
  g.IS_REACT_ACT_ENVIRONMENT = true;
2572
2612
  g.IS_REACT_NATIVE_TEST_ENVIRONMENT = true;
2573
2613
  const rnMock = buildReactNativeMock(platform);
@@ -2581,14 +2621,15 @@ function loadPresetFactory(exportName) {
2581
2621
  return typeof factory === "function" ? factory : null;
2582
2622
  }
2583
2623
  function autoDetectPresets() {
2584
- const projectRoot = process.env.VITEST_NATIVE_PROJECT_ROOT || process.cwd();
2585
- const projectReq = createRequire(path.join(projectRoot, "package.json"));
2586
2624
  const detected = [];
2625
+ const enabled = /* @__PURE__ */ new Set();
2587
2626
  for (const [pkgName, exportName] of Object.entries(AUTO_DETECT_PRESETS)) try {
2588
2627
  projectReq.resolve(pkgName);
2628
+ if (enabled.has(exportName)) continue;
2589
2629
  const factory = loadPresetFactory(exportName);
2590
2630
  if (factory) {
2591
2631
  detected.push(factory());
2632
+ enabled.add(exportName);
2592
2633
  if (diagnostics) console.log(`[vitest-native] Auto-detected ${pkgName} → enabled ${exportName} preset`);
2593
2634
  }
2594
2635
  } catch {}
@@ -2611,8 +2652,13 @@ for (const preset of presets) for (const [modName, presetModule] of Object.entri
2611
2652
  if (diagnostics) console.log(`[vitest-native] Registered preset mock: ${modName} (${preset.name})`);
2612
2653
  }
2613
2654
  try {
2614
- const rntl = __require("@testing-library/react-native");
2615
- if (rntl?.configure) rntl.configure({ hostComponentNames: {
2655
+ const rntl = projectReq("@testing-library/react-native");
2656
+ let rntlMajor = 0;
2657
+ try {
2658
+ const { version } = projectReq("@testing-library/react-native/package.json");
2659
+ rntlMajor = Number(String(version).split(".")[0]) || 0;
2660
+ } catch {}
2661
+ if (rntl?.configure && rntlMajor < 14) rntl.configure({ hostComponentNames: {
2616
2662
  text: "Text",
2617
2663
  textInput: "TextInput",
2618
2664
  switch: "RCTSwitch",
@@ -2628,7 +2674,19 @@ try {
2628
2674
  if (diagnostics) console.log(`[vitest-native] @testing-library/react-native not available: ${e?.message}`);
2629
2675
  }
2630
2676
  try {
2631
- const matchers = __require("@testing-library/react-native/build/matchers");
2677
+ let matchers = null;
2678
+ let lastError;
2679
+ for (const moduleId of [
2680
+ "@testing-library/react-native/matchers",
2681
+ "@testing-library/react-native/build/matchers",
2682
+ "@testing-library/react-native/dist/matchers"
2683
+ ]) try {
2684
+ matchers = projectReq(moduleId);
2685
+ break;
2686
+ } catch (error) {
2687
+ lastError = error;
2688
+ }
2689
+ if (!matchers) throw lastError;
2632
2690
  if (matchers && Object.keys(matchers).length > 0) {
2633
2691
  const matcherFns = {};
2634
2692
  for (const [key, val] of Object.entries(matchers)) if (typeof val === "function" && key !== "__esModule") matcherFns[key] = val;
@@ -71,9 +71,11 @@ on the literal `jest.mock` member form, not `jest` aliased to another local name
71
71
 
72
72
  …and in most cases you can **delete** third-party native-lib mocks entirely — see 2c.
73
73
 
74
- ### 2b. Upgrade RNTL to ≥ 12 (recommended)
74
+ ### 2b. Upgrade RNTL to 12–14 (recommended)
75
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.
76
+ is the cleaner fix** (12 dropped the `@jest/globals` dependency). vitest-native supports RNTL
77
+ 12–14. RNTL 14 requires Node 22.13 or 24+ and makes rendering APIs async, so await `render` and
78
+ other APIs as described in its migration guide.
77
79
 
78
80
  ### 2c. Delete third-party native-lib mocks — they're automatic now
79
81
  You do **not** need `jest.mock('react-native-reanimated', …)`, safe-area's `jest/mock`,
@@ -108,7 +110,7 @@ no-op under the shim (use `test.testTimeout` in config or per-test `{ timeout }`
108
110
 
109
111
  ## 3. Suggested migration recipe
110
112
 
111
- 1. Add the jest-compat config (section 1). Upgrade RNTL to ≥ 12 (2b).
113
+ 1. Add the jest-compat config (section 1). Upgrade RNTL to a supported 12–14 release (2b).
112
114
  2. Delete manual third-party native-lib mocks and `@react-native/jest-preset` (2c, 2e).
113
115
  3. Convert any **top-level** `jest.mock(...)` to `vi.mock(...)`; leave runtime `jest.*` as-is (2a).
114
116
  4. Run `vitest run -u` once to re-record snapshots (2d), then `vitest run` to confirm green.
@@ -116,7 +118,7 @@ no-op under the shim (use `test.testTimeout` in config or per-test `{ timeout }`
116
118
 
117
119
  ## 4. What "done" looks like
118
120
 
119
- A migrated suite runs under Vitest with: the jest-compat aliases + setup, RNTL ≥ 12, no manual
121
+ A migrated suite runs under Vitest with: the jest-compat aliases + setup, RNTL 12–14, no manual
120
122
  third-party native mocks, top-level mocks converted, and snapshots re-recorded. You get Vitest's
121
123
  speed/watch/UI while keeping `engine: 'native'` real-RN fidelity (or `engine: 'mock'` for the fast
122
124
  path). The compat layer is intentionally small — it removes the mechanical Jest coupling so the only
@@ -27,8 +27,9 @@ npm i -D vitest-native @react-native/babel-preset @babel/core
27
27
  ```
28
28
 
29
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.**
30
+ `@testing-library/react-native`. Requirements: **Vitest 4.x, Vite ^6.4.2, ^7.3.2, or ^8.0.5, the React
31
+ version required by your RN release, React Native 0.81–0.86 (validated), and
32
+ RNTL 12–14.** RNTL 14 uses async rendering APIs and requires Node 22.13 or 24+.
32
33
 
33
34
  ## 2. Update `vitest.config`
34
35
 
@@ -89,7 +90,9 @@ Your existing tests render against real React Native exactly as before.
89
90
  - **Presets** auto-shadow common native libraries (Reanimated, Gesture Handler,
90
91
  Safe Area, Navigation, …) the way `jest` mocks them — no per-lib wiring.
91
92
  - An opt-in **hot runtime** (`reactNative({ hotRuntime: true })`) keeps RN warm
92
- across files for large suites.
93
+ across files for large suites while resetting app/test modules and common
94
+ process-wide pollution between files. It uses Vitest's custom worker APIs and
95
+ remains experimental.
93
96
 
94
97
  ## Gotchas (the margins)
95
98
 
@@ -0,0 +1,52 @@
1
+ # Release readiness policy
2
+
3
+ This document defines what `vitest-native` means by release-supported. It is deliberately stricter
4
+ than "the repository tests pass": published artifacts are installed into representative consumer
5
+ projects, experimental surfaces are labeled, and upstream compatibility is bounded.
6
+
7
+ ## Stability labels
8
+
9
+ | Surface | Status | Release promise |
10
+ | --- | --- | --- |
11
+ | Mock engine | Release-supported | Documented behavior and exports are gated on every pull request and release. |
12
+ | Native engine | Release-supported beta | Real RN behavior, iOS/Android resolution, presets, helpers, assets, matchers, and isolation are blocking gates. Pre-1.0 APIs can still change with release notes. |
13
+ | Hot runtime | Experimental | Correctness is gated, but it depends on Vitest's experimental custom-pool API and can require adaptation between Vitest releases. |
14
+ | Current RN edge | Canary | Tested weekly and in a pinned packed consumer once adopted; an edge failure opens a compatibility issue. |
15
+
16
+ ## Blocking compatibility matrix
17
+
18
+ - Node 20.19 on Linux.
19
+ - Node 22.13 on Linux, macOS, and Windows.
20
+ - Patched Vite lines ^6.4.2, ^7.3.2, and ^8.0.5 through packed consumers.
21
+ - Vitest 4.x, including the lockfile release and newest supported 4.x release.
22
+ - React Native 0.81–0.85 in the full native matrix.
23
+ - React Native 0.86 in a pinned packed Android consumer.
24
+ - React Native Testing Library 12, 13, and 14.
25
+ - Expo 56 and a hoisted npm-workspace monorepo.
26
+
27
+ Required Vite, Vitest, and React peer mismatches fail during configuration. Unsupported future major
28
+ versions are rejected rather than allowed to fail later inside private runner internals.
29
+
30
+ ## Blocking release gates
31
+
32
+ 1. Dependency pinning, lint, formatting, and TypeScript checks.
33
+ 2. Mock suite and React Native conformance tests.
34
+ 3. Native iOS and Android suites.
35
+ 4. Hot-runtime full suite, cross-file isolation suite, and generated 100-file soak.
36
+ 5. End-to-end memory-triggered worker recycling.
37
+ 6. Mock-versus-real-RN behavioral cross-check.
38
+ 7. Example app.
39
+ 8. `npm pack` followed by isolated installs of bare RN, Expo, monorepo, and current-RN consumers.
40
+ 9. Package export and declaration analysis with `@arethetypeswrong/cli`.
41
+ 10. Staged npm publishing with provenance; the version remains unavailable until maintainer approval.
42
+
43
+ The ATTW gate ignores legacy Node 10 resolution because the package requires Node 20+. It also
44
+ allows `cjs-resolves-to-esm` only for the three `jest-compat` runtime shims: those entries depend on
45
+ Vitest's ESM runtime and are setup-file or Vite-alias targets. All normal public APIs are gated as
46
+ dual ESM/CommonJS exports.
47
+
48
+ ## Release decision
49
+
50
+ A release is blocked by any failure in the declared support range. Canary failures do not silently
51
+ expand the support range: they produce a tracked compatibility issue and must be resolved before the
52
+ new upstream major or RN minor is advertised as supported.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest-native",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "Run real React Native tests under Vitest. One install, zero config.",
6
6
  "license": "MIT",
@@ -100,15 +100,15 @@
100
100
  },
101
101
  "./jest-compat/setup": {
102
102
  "types": "./dist/jest-compat/setup.d.mts",
103
- "default": "./dist/jest-compat/setup.mjs"
103
+ "import": "./dist/jest-compat/setup.mjs"
104
104
  },
105
105
  "./jest-compat/jest-globals": {
106
106
  "types": "./dist/jest-compat/jest-globals.d.mts",
107
- "default": "./dist/jest-compat/jest-globals.mjs"
107
+ "import": "./dist/jest-compat/jest-globals.mjs"
108
108
  },
109
109
  "./jest-compat/extend-expect-noop": {
110
110
  "types": "./dist/jest-compat/noop.d.mts",
111
- "default": "./dist/jest-compat/noop.mjs"
111
+ "import": "./dist/jest-compat/noop.mjs"
112
112
  }
113
113
  },
114
114
  "files": [
@@ -120,15 +120,20 @@
120
120
  "engines": {
121
121
  "node": ">=20"
122
122
  },
123
- "sideEffects": false,
123
+ "sideEffects": [
124
+ "./dist/setup.cjs",
125
+ "./dist/setup.mjs",
126
+ "./dist/jest-compat/setup.mjs",
127
+ "./dist/native/*.mjs"
128
+ ],
124
129
  "publishConfig": {
125
130
  "access": "public"
126
131
  },
127
132
  "peerDependencies": {
128
133
  "react": ">=18",
129
- "vite": ">=5",
130
- "vitest": ">=4",
131
- "@testing-library/react-native": ">=12",
134
+ "vite": "^6.4.2 || ^7.3.2 || ^8.0.5",
135
+ "vitest": ">=4 <5",
136
+ "@testing-library/react-native": ">=12 <15",
132
137
  "@react-native/babel-preset": "*",
133
138
  "@babel/core": "*"
134
139
  },
@@ -145,39 +150,45 @@
145
150
  },
146
151
  "devDependencies": {
147
152
  "@babel/core": "^7.29.7",
153
+ "@react-native-vector-icons/common": "13.0.1",
154
+ "@react-native-vector-icons/material-icons": "13.1.2",
148
155
  "@react-native/babel-preset": "^0.85.3",
149
156
  "@react-navigation/native": "7.2.5",
150
157
  "@react-navigation/native-stack": "7.16.0",
151
158
  "@testing-library/react-native": "^12.9.0",
152
159
  "@types/node": "25.3.5",
153
160
  "@types/react": "19.2.14",
154
- "@vitest/coverage-v8": "4.0.18",
161
+ "@vitest/coverage-v8": "4.1.8",
155
162
  "expo-constants": "56.0.16",
156
163
  "expo-status-bar": "56.0.4",
157
- "react": "^19.0.0",
158
- "react-native": "^0.84.1",
164
+ "react": "^19.2.3",
165
+ "react-native": "^0.85.3",
159
166
  "react-native-gesture-handler": "3.0.0",
160
167
  "react-native-reanimated": "4.3.1",
161
168
  "react-native-safe-area-context": "5.8.0",
162
169
  "react-native-worklets": "0.9.1",
163
- "react-test-renderer": "^19.0.0",
164
- "tsdown": "0.21.0",
165
- "typescript": "5.9.3",
166
- "vite": "^6.0.0",
167
- "vitest": "^4.0.0"
170
+ "react-test-renderer": "^19.2.3",
171
+ "tsdown": "0.21.10",
172
+ "typescript": "6.0.3",
173
+ "vite": "^8.0.5",
174
+ "vitest": "^4.1.8"
168
175
  },
169
176
  "scripts": {
170
177
  "build": "tsdown",
171
178
  "test": "vitest run",
172
179
  "test:native": "vitest run --config tests-native/vitest.config.mts",
180
+ "test:native:android": "vitest run --config tests-native/vitest.android.config.mts",
173
181
  "test:native:hot": "vitest run --config tests-native/vitest.hot.config.mts",
174
- "lint": "oxlint --deny-warnings src/ tests/",
175
- "format": "oxfmt --write src/ tests/",
176
- "format:check": "oxfmt --check src/ tests/",
182
+ "test:native:hot:isolation": "vitest run --config tests-native/vitest.hot-isolation.config.mts",
183
+ "test:native:hot:soak": "node scripts/test-hot-soak.mjs",
184
+ "test:consumers": "node scripts/test-consumers.mjs",
185
+ "lint": "oxlint --deny-warnings src/ tests/ tests-native/ crosscheck/ consumer-tests/ scripts/",
186
+ "format": "oxfmt --write src/ tests/ consumer-tests/ scripts/",
187
+ "format:check": "oxfmt --check src/ tests/ consumer-tests/ scripts/",
177
188
  "check-compat": "bun scripts/check-compat.ts",
178
189
  "typecheck": "tsc --noEmit",
179
190
  "clean": "rm -rf dist",
180
- "prepublishOnly": "bun run build && bun run test && bun run lint && bun run typecheck",
191
+ "prepublishOnly": "bun run build && bun run test && bun run test:native && bun run test:native:android && bun run test:native:hot && bun run test:native:hot:isolation && bun run test:native:hot:soak && bun run crosscheck && bun run test:consumers && bun run lint && bun run typecheck",
181
192
  "crosscheck": "node scripts/crosscheck.mjs"
182
193
  },
183
194
  "dependencies": {