vitest-native 0.2.0 → 0.3.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.
- package/CHANGELOG.md +20 -0
- package/README.md +31 -1
- package/dist/helpers.cjs +10 -0
- package/dist/helpers.d.cts +7 -1
- package/dist/helpers.d.mts +7 -1
- package/dist/helpers.mjs +10 -1
- package/dist/index.cjs +58 -28
- package/dist/index.mjs +57 -28
- package/dist/{preset-map-D224CC5w.mjs → preset-map-BTpoNH3G.mjs} +1 -0
- package/dist/{preset-map-Bt4dXkZO.cjs → preset-map-DxU6UgYS.cjs} +1 -0
- package/dist/{presets-D4jrt194.cjs → presets-B-spxZz7.cjs} +431 -75
- package/dist/{presets-C9tAPD15.mjs → presets-DvrcfEPz.mjs} +431 -75
- package/dist/presets.cjs +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/setup.cjs +719 -85
- package/dist/setup.mjs +719 -85
- package/package.json +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# vitest-native
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Add RN conformance test suite — 75 tests ported from React Native's own test suite (Animated, processColor, flattenStyle, Interpolation) to validate mock behavioral parity
|
|
8
|
+
- Add Animated orchestration: `sequence` chains via callbacks, `parallel` waits for all, `loop` supports finite/indefinite iterations with `resetBeforeIteration`
|
|
9
|
+
- Add Animated value tracking: `timing`/`spring` with an `AnimatedValue` as `toValue` track source changes via listener
|
|
10
|
+
- Add Animated.Color, diffClamp tracking, interpolation extrapolate/easing, toJSON support
|
|
11
|
+
- Expand reanimated preset: 44 entering/exiting animations, 7 layout transitions, `useAnimatedReaction`, `useAnimatedKeyboard`, `useReducedMotion`, `useFrameCallback`, `makeMutable`, `SharedTransition`, `ReduceMotion`/`KeyboardState` enums
|
|
12
|
+
- Add `@react-navigation/drawer` preset with `createDrawerNavigator`
|
|
13
|
+
- Add `setInsets()` helper for safe area context testing
|
|
14
|
+
- Add inter-test isolation: `resetAllMocks()` now resets AsyncStorage store and safe area insets
|
|
15
|
+
- 1136 tests passing across 30 files
|
|
16
|
+
|
|
17
|
+
## 0.2.1
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Add missing `@react-navigation/core` re-exports to navigation preset, including `useNavigationContainerRef`, `useTheme`, `ThemeProvider`, `NavigationIndependentTree`, `useNavigationBuilder`, `BaseRouter`, and 20+ other exports. Fixes tests that depend on these being available from `@react-navigation/native`.
|
|
22
|
+
|
|
3
23
|
## 0.2.0
|
|
4
24
|
|
|
5
25
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ A Vitest plugin for React Native. One install, zero config.
|
|
|
11
11
|
- [Quick Start](#quick-start)
|
|
12
12
|
- [Plugin Options](#plugin-options)
|
|
13
13
|
- [Mocked Components and APIs](#mocked-components-and-apis)
|
|
14
|
+
- [React Native Test Suite Conformance](#react-native-test-suite-conformance)
|
|
14
15
|
- [Test Helpers](#test-helpers)
|
|
15
16
|
- [Auto-Detect Presets](#auto-detect-presets)
|
|
16
17
|
- [RNTL Matchers](#rntl-matchers)
|
|
@@ -179,6 +180,21 @@ The plugin provides a complete mock of the `react-native` module. Every componen
|
|
|
179
180
|
|
|
180
181
|
---
|
|
181
182
|
|
|
183
|
+
## React Native Test Suite Conformance
|
|
184
|
+
|
|
185
|
+
vitest-native ports tests directly from React Native's own test suite to validate mock behavioral parity. These tests are the same assertions Meta uses to verify React Native itself:
|
|
186
|
+
|
|
187
|
+
- **Easing** — all 24 easing curve tests including sample data for quad, cubic, sin, exp, circle, and back
|
|
188
|
+
- **Bezier** — 9 cubic bezier mathematical property tests (symmetry, projection, boundary conditions)
|
|
189
|
+
- **flattenStyle** — 12 style merging tests covering override precedence, reference identity, and recursive flattening
|
|
190
|
+
- **processColor** — 9 color format conversion tests for named colors, RGB, RGBA, HSL, HSLA, and hex
|
|
191
|
+
- **Interpolation** — 9 numeric range mapping tests including extrapolate modes (extend/clamp/identity) and easing
|
|
192
|
+
- **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
|
+
|
|
194
|
+
99 assertions ported from RN's own test suite, all passing.
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
182
198
|
## Test Helpers
|
|
183
199
|
|
|
184
200
|
Import helpers from `vitest-native/helpers` to control mock state during tests.
|
|
@@ -188,6 +204,7 @@ import {
|
|
|
188
204
|
setPlatform,
|
|
189
205
|
setDimensions,
|
|
190
206
|
setColorScheme,
|
|
207
|
+
setInsets,
|
|
191
208
|
mockNativeModule,
|
|
192
209
|
resetAllMocks,
|
|
193
210
|
} from 'vitest-native/helpers';
|
|
@@ -235,6 +252,19 @@ test('renders dark mode styles', () => {
|
|
|
235
252
|
});
|
|
236
253
|
```
|
|
237
254
|
|
|
255
|
+
### `setInsets(insets)`
|
|
256
|
+
|
|
257
|
+
Update the safe area insets returned by `useSafeAreaInsets()` from `react-native-safe-area-context`. Requires the `safeAreaContext` preset to be active.
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
import { setInsets } from 'vitest-native/helpers';
|
|
261
|
+
|
|
262
|
+
test('renders with no bottom inset (Android)', () => {
|
|
263
|
+
setInsets({ top: 24, bottom: 0 });
|
|
264
|
+
// useSafeAreaInsets() returns { top: 24, right: 0, bottom: 0, left: 0 }
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
238
268
|
### `mockNativeModule(name, impl)`
|
|
239
269
|
|
|
240
270
|
Register a custom native module mock. The module becomes available via `NativeModules[name]`.
|
|
@@ -254,7 +284,7 @@ test('uses a custom native module', () => {
|
|
|
254
284
|
|
|
255
285
|
### `resetAllMocks()`
|
|
256
286
|
|
|
257
|
-
Reset all mocks to their default state. Restores platform to iOS, dimensions to iPhone 14 Pro defaults (390x844), color scheme to `'light'`, clears all mock call history, and undoes any `mockNativeModule` calls.
|
|
287
|
+
Reset all mocks to their default state. Restores platform to iOS, dimensions to iPhone 14 Pro defaults (390x844), color scheme to `'light'`, safe area insets to iPhone defaults, clears AsyncStorage data, clears all mock call history, and undoes any `mockNativeModule` calls.
|
|
258
288
|
|
|
259
289
|
```ts
|
|
260
290
|
import { resetAllMocks } from 'vitest-native/helpers';
|
package/dist/helpers.cjs
CHANGED
|
@@ -35,6 +35,10 @@ function setColorScheme(scheme) {
|
|
|
35
35
|
mock.Appearance.setColorScheme(resolved);
|
|
36
36
|
if (mock.useColorScheme._setScheme) mock.useColorScheme._setScheme(resolved);
|
|
37
37
|
}
|
|
38
|
+
function setInsets(insets) {
|
|
39
|
+
const safeArea = globalThis.__vitest_native_preset_mocks?.["react-native-safe-area-context"];
|
|
40
|
+
if (safeArea?._setInsets) safeArea._setInsets(insets);
|
|
41
|
+
}
|
|
38
42
|
function mockNativeModule(name, impl) {
|
|
39
43
|
const mock = getMock();
|
|
40
44
|
if (originalNativeModules === null) originalNativeModules = mock.NativeModules;
|
|
@@ -104,10 +108,16 @@ function resetAllMocks() {
|
|
|
104
108
|
mock.NativeModules = originalNativeModules;
|
|
105
109
|
originalNativeModules = null;
|
|
106
110
|
}
|
|
111
|
+
const presetMocks = globalThis.__vitest_native_preset_mocks;
|
|
112
|
+
if (presetMocks) for (const mod of Object.values(presetMocks)) {
|
|
113
|
+
if (mod?._reset) mod._reset();
|
|
114
|
+
if (mod?._resetStore) mod._resetStore();
|
|
115
|
+
}
|
|
107
116
|
}
|
|
108
117
|
//#endregion
|
|
109
118
|
exports.mockNativeModule = mockNativeModule;
|
|
110
119
|
exports.resetAllMocks = resetAllMocks;
|
|
111
120
|
exports.setColorScheme = setColorScheme;
|
|
112
121
|
exports.setDimensions = setDimensions;
|
|
122
|
+
exports.setInsets = setInsets;
|
|
113
123
|
exports.setPlatform = setPlatform;
|
package/dist/helpers.d.cts
CHANGED
|
@@ -13,7 +13,13 @@ declare function setDimensions(dims: {
|
|
|
13
13
|
fontScale?: number;
|
|
14
14
|
}): void;
|
|
15
15
|
declare function setColorScheme(scheme: "light" | "dark" | null): void;
|
|
16
|
+
declare function setInsets(insets: {
|
|
17
|
+
top?: number;
|
|
18
|
+
right?: number;
|
|
19
|
+
bottom?: number;
|
|
20
|
+
left?: number;
|
|
21
|
+
}): void;
|
|
16
22
|
declare function mockNativeModule(name: string, impl: Record<string, any>): void;
|
|
17
23
|
declare function resetAllMocks(): void;
|
|
18
24
|
//#endregion
|
|
19
|
-
export { mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setPlatform };
|
|
25
|
+
export { mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setInsets, setPlatform };
|
package/dist/helpers.d.mts
CHANGED
|
@@ -13,7 +13,13 @@ declare function setDimensions(dims: {
|
|
|
13
13
|
fontScale?: number;
|
|
14
14
|
}): void;
|
|
15
15
|
declare function setColorScheme(scheme: "light" | "dark" | null): void;
|
|
16
|
+
declare function setInsets(insets: {
|
|
17
|
+
top?: number;
|
|
18
|
+
right?: number;
|
|
19
|
+
bottom?: number;
|
|
20
|
+
left?: number;
|
|
21
|
+
}): void;
|
|
16
22
|
declare function mockNativeModule(name: string, impl: Record<string, any>): void;
|
|
17
23
|
declare function resetAllMocks(): void;
|
|
18
24
|
//#endregion
|
|
19
|
-
export { mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setPlatform };
|
|
25
|
+
export { mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setInsets, setPlatform };
|
package/dist/helpers.mjs
CHANGED
|
@@ -34,6 +34,10 @@ function setColorScheme(scheme) {
|
|
|
34
34
|
mock.Appearance.setColorScheme(resolved);
|
|
35
35
|
if (mock.useColorScheme._setScheme) mock.useColorScheme._setScheme(resolved);
|
|
36
36
|
}
|
|
37
|
+
function setInsets(insets) {
|
|
38
|
+
const safeArea = globalThis.__vitest_native_preset_mocks?.["react-native-safe-area-context"];
|
|
39
|
+
if (safeArea?._setInsets) safeArea._setInsets(insets);
|
|
40
|
+
}
|
|
37
41
|
function mockNativeModule(name, impl) {
|
|
38
42
|
const mock = getMock();
|
|
39
43
|
if (originalNativeModules === null) originalNativeModules = mock.NativeModules;
|
|
@@ -103,6 +107,11 @@ function resetAllMocks() {
|
|
|
103
107
|
mock.NativeModules = originalNativeModules;
|
|
104
108
|
originalNativeModules = null;
|
|
105
109
|
}
|
|
110
|
+
const presetMocks = globalThis.__vitest_native_preset_mocks;
|
|
111
|
+
if (presetMocks) for (const mod of Object.values(presetMocks)) {
|
|
112
|
+
if (mod?._reset) mod._reset();
|
|
113
|
+
if (mod?._resetStore) mod._resetStore();
|
|
114
|
+
}
|
|
106
115
|
}
|
|
107
116
|
//#endregion
|
|
108
|
-
export { mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setPlatform };
|
|
117
|
+
export { mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setInsets, setPlatform };
|
package/dist/index.cjs
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_presets = require("./presets-
|
|
3
|
-
const require_preset_map = require("./preset-map-
|
|
2
|
+
const require_presets = require("./presets-B-spxZz7.cjs");
|
|
3
|
+
const require_preset_map = require("./preset-map-DxU6UgYS.cjs");
|
|
4
4
|
let node_url = require("node:url");
|
|
5
5
|
let node_path = require("node:path");
|
|
6
6
|
node_path = require_presets.__toESM(node_path);
|
|
7
7
|
let node_fs = require("node:fs");
|
|
8
8
|
node_fs = require_presets.__toESM(node_fs);
|
|
9
9
|
let node_module = require("node:module");
|
|
10
|
+
let flow_remove_types = require("flow-remove-types");
|
|
11
|
+
flow_remove_types = require_presets.__toESM(flow_remove_types);
|
|
10
12
|
//#region src/resolve.ts
|
|
11
13
|
const PLATFORM_EXTENSIONS_IOS = [
|
|
12
14
|
".ios.tsx",
|
|
@@ -68,27 +70,22 @@ const DEFAULT_ASSET_EXTS = [
|
|
|
68
70
|
function stripFsPrefix(id) {
|
|
69
71
|
return id.startsWith("/@fs/") ? id.slice(4) : id;
|
|
70
72
|
}
|
|
71
|
-
/**
|
|
72
|
-
* Check if a package is installed from the consumer's project root,
|
|
73
|
-
* not from the plugin's own node_modules. This is critical for monorepos
|
|
74
|
-
* and non-hoisted layouts (pnpm strict, yarn PnP).
|
|
75
|
-
*/
|
|
76
|
-
function isPackageInstalled(packageName, projectRoot) {
|
|
77
|
-
try {
|
|
78
|
-
(0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json")).resolve(packageName);
|
|
79
|
-
return true;
|
|
80
|
-
} catch {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
73
|
async function autoDetectPresets(diagnostics, projectRoot) {
|
|
85
74
|
const detected = [];
|
|
86
75
|
const presetFactories = await Promise.resolve().then(() => require("./presets.cjs"));
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
76
|
+
const req = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
77
|
+
for (const [pkgName, exportName] of Object.entries(require_preset_map.AUTO_DETECT_PRESETS)) {
|
|
78
|
+
let installed = false;
|
|
79
|
+
try {
|
|
80
|
+
req.resolve(pkgName);
|
|
81
|
+
installed = true;
|
|
82
|
+
} catch {}
|
|
83
|
+
if (installed) {
|
|
84
|
+
const factory = presetFactories[exportName];
|
|
85
|
+
if (typeof factory === "function") {
|
|
86
|
+
detected.push(factory());
|
|
87
|
+
if (diagnostics) console.log(`[vitest-native] Auto-detected ${pkgName} → enabled ${exportName} preset`);
|
|
88
|
+
}
|
|
92
89
|
}
|
|
93
90
|
}
|
|
94
91
|
return detected;
|
|
@@ -219,6 +216,8 @@ function reactNative(options) {
|
|
|
219
216
|
const presetModules = /* @__PURE__ */ new Map();
|
|
220
217
|
const presetExportNames = /* @__PURE__ */ new Map();
|
|
221
218
|
let assetPattern;
|
|
219
|
+
const resolveCache = /* @__PURE__ */ new Map();
|
|
220
|
+
const virtualCodeCache = /* @__PURE__ */ new Map();
|
|
222
221
|
const thisDir = node_path.default.dirname((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
|
|
223
222
|
let setupFilePath = node_path.default.resolve(thisDir, "setup.mjs");
|
|
224
223
|
if (!node_fs.default.existsSync(setupFilePath)) {
|
|
@@ -264,36 +263,67 @@ function reactNative(options) {
|
|
|
264
263
|
if (source.startsWith("react-native/")) return `\0virtual:rn-subpath:${source}`;
|
|
265
264
|
if (presetModules.has(source)) return `\0virtual:preset:${source}`;
|
|
266
265
|
if (importer && importer.includes("node_modules") && source.startsWith(".") && !node_path.default.extname(source)) {
|
|
266
|
+
const cacheKey = `${importer}\0${source}`;
|
|
267
|
+
const cached = resolveCache.get(cacheKey);
|
|
268
|
+
if (cached !== void 0) return cached;
|
|
267
269
|
const importerDir = node_path.default.dirname(importer);
|
|
268
270
|
const absolute = node_path.default.resolve(importerDir, source);
|
|
269
271
|
for (const ext of extensions) {
|
|
270
272
|
const candidate = absolute + ext;
|
|
271
|
-
if (node_fs.default.existsSync(candidate))
|
|
273
|
+
if (node_fs.default.existsSync(candidate)) {
|
|
274
|
+
resolveCache.set(cacheKey, candidate);
|
|
275
|
+
return candidate;
|
|
276
|
+
}
|
|
272
277
|
}
|
|
273
278
|
for (const ext of extensions) {
|
|
274
279
|
const candidate = node_path.default.join(absolute, `index${ext}`);
|
|
275
|
-
if (node_fs.default.existsSync(candidate))
|
|
280
|
+
if (node_fs.default.existsSync(candidate)) {
|
|
281
|
+
resolveCache.set(cacheKey, candidate);
|
|
282
|
+
return candidate;
|
|
283
|
+
}
|
|
276
284
|
}
|
|
285
|
+
resolveCache.set(cacheKey, void 0);
|
|
277
286
|
}
|
|
278
287
|
},
|
|
279
288
|
load(id) {
|
|
280
289
|
if (id === "\0virtual:react-native") return "export default {};";
|
|
281
|
-
if (id.startsWith("\0virtual:rn-subpath:"))
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
290
|
+
if (id.startsWith("\0virtual:rn-subpath:")) {
|
|
291
|
+
let code = virtualCodeCache.get("\0rn-subpath");
|
|
292
|
+
if (!code) {
|
|
293
|
+
code = [
|
|
294
|
+
`const _rn = globalThis.__vitest_native_mock || {};`,
|
|
295
|
+
...RN_EXPORT_NAMES.map((n) => `export const ${n} = _rn['${n}'];`),
|
|
296
|
+
`export default _rn;`
|
|
297
|
+
].join("\n");
|
|
298
|
+
virtualCodeCache.set("\0rn-subpath", code);
|
|
299
|
+
}
|
|
300
|
+
return code;
|
|
301
|
+
}
|
|
286
302
|
if (id.startsWith("\0virtual:preset:")) {
|
|
303
|
+
const cached = virtualCodeCache.get(id);
|
|
304
|
+
if (cached) return cached;
|
|
287
305
|
const moduleName = id.slice(16);
|
|
288
306
|
const exportNames = presetExportNames.get(moduleName) || [];
|
|
289
|
-
|
|
307
|
+
const code = [
|
|
290
308
|
`const _m = (globalThis.__vitest_native_preset_mocks || {})['${moduleName}'] || {};`,
|
|
291
309
|
...exportNames.map((n) => `export const ${n} = _m['${n}'];`),
|
|
292
310
|
`export default _m;`
|
|
293
311
|
].join("\n");
|
|
312
|
+
virtualCodeCache.set(id, code);
|
|
313
|
+
return code;
|
|
294
314
|
}
|
|
295
315
|
const fsPath = stripFsPrefix(id);
|
|
296
316
|
if (assetPattern.test(fsPath)) return `export default "${fsPath.split("/").pop() ?? fsPath}";`;
|
|
317
|
+
},
|
|
318
|
+
transform(code, id) {
|
|
319
|
+
if (!id.includes("node_modules")) return void 0;
|
|
320
|
+
if (!id.includes("react-native") || !id.endsWith(".js")) return void 0;
|
|
321
|
+
if (!code.includes("@flow")) return void 0;
|
|
322
|
+
const stripped = (0, flow_remove_types.default)(code, { all: true });
|
|
323
|
+
return {
|
|
324
|
+
code: stripped.toString(),
|
|
325
|
+
map: stripped.generateMap()
|
|
326
|
+
};
|
|
297
327
|
}
|
|
298
328
|
};
|
|
299
329
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { t as AUTO_DETECT_PRESETS } from "./preset-map-
|
|
2
|
-
import { t as presets_exports } from "./presets-
|
|
1
|
+
import { t as AUTO_DETECT_PRESETS } from "./preset-map-BTpoNH3G.mjs";
|
|
2
|
+
import { t as presets_exports } from "./presets-DvrcfEPz.mjs";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import fs from "node:fs";
|
|
7
|
+
import flowRemoveTypes from "flow-remove-types";
|
|
7
8
|
//#region src/resolve.ts
|
|
8
9
|
const PLATFORM_EXTENSIONS_IOS = [
|
|
9
10
|
".ios.tsx",
|
|
@@ -65,27 +66,22 @@ const DEFAULT_ASSET_EXTS = [
|
|
|
65
66
|
function stripFsPrefix(id) {
|
|
66
67
|
return id.startsWith("/@fs/") ? id.slice(4) : id;
|
|
67
68
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Check if a package is installed from the consumer's project root,
|
|
70
|
-
* not from the plugin's own node_modules. This is critical for monorepos
|
|
71
|
-
* and non-hoisted layouts (pnpm strict, yarn PnP).
|
|
72
|
-
*/
|
|
73
|
-
function isPackageInstalled(packageName, projectRoot) {
|
|
74
|
-
try {
|
|
75
|
-
createRequire(path.join(projectRoot, "package.json")).resolve(packageName);
|
|
76
|
-
return true;
|
|
77
|
-
} catch {
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
69
|
async function autoDetectPresets(diagnostics, projectRoot) {
|
|
82
70
|
const detected = [];
|
|
83
71
|
const presetFactories = await import("./presets.mjs");
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
72
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
73
|
+
for (const [pkgName, exportName] of Object.entries(AUTO_DETECT_PRESETS)) {
|
|
74
|
+
let installed = false;
|
|
75
|
+
try {
|
|
76
|
+
req.resolve(pkgName);
|
|
77
|
+
installed = true;
|
|
78
|
+
} catch {}
|
|
79
|
+
if (installed) {
|
|
80
|
+
const factory = presetFactories[exportName];
|
|
81
|
+
if (typeof factory === "function") {
|
|
82
|
+
detected.push(factory());
|
|
83
|
+
if (diagnostics) console.log(`[vitest-native] Auto-detected ${pkgName} → enabled ${exportName} preset`);
|
|
84
|
+
}
|
|
89
85
|
}
|
|
90
86
|
}
|
|
91
87
|
return detected;
|
|
@@ -216,6 +212,8 @@ function reactNative(options) {
|
|
|
216
212
|
const presetModules = /* @__PURE__ */ new Map();
|
|
217
213
|
const presetExportNames = /* @__PURE__ */ new Map();
|
|
218
214
|
let assetPattern;
|
|
215
|
+
const resolveCache = /* @__PURE__ */ new Map();
|
|
216
|
+
const virtualCodeCache = /* @__PURE__ */ new Map();
|
|
219
217
|
const thisDir = path.dirname(fileURLToPath(import.meta.url));
|
|
220
218
|
let setupFilePath = path.resolve(thisDir, "setup.mjs");
|
|
221
219
|
if (!fs.existsSync(setupFilePath)) {
|
|
@@ -261,36 +259,67 @@ function reactNative(options) {
|
|
|
261
259
|
if (source.startsWith("react-native/")) return `\0virtual:rn-subpath:${source}`;
|
|
262
260
|
if (presetModules.has(source)) return `\0virtual:preset:${source}`;
|
|
263
261
|
if (importer && importer.includes("node_modules") && source.startsWith(".") && !path.extname(source)) {
|
|
262
|
+
const cacheKey = `${importer}\0${source}`;
|
|
263
|
+
const cached = resolveCache.get(cacheKey);
|
|
264
|
+
if (cached !== void 0) return cached;
|
|
264
265
|
const importerDir = path.dirname(importer);
|
|
265
266
|
const absolute = path.resolve(importerDir, source);
|
|
266
267
|
for (const ext of extensions) {
|
|
267
268
|
const candidate = absolute + ext;
|
|
268
|
-
if (fs.existsSync(candidate))
|
|
269
|
+
if (fs.existsSync(candidate)) {
|
|
270
|
+
resolveCache.set(cacheKey, candidate);
|
|
271
|
+
return candidate;
|
|
272
|
+
}
|
|
269
273
|
}
|
|
270
274
|
for (const ext of extensions) {
|
|
271
275
|
const candidate = path.join(absolute, `index${ext}`);
|
|
272
|
-
if (fs.existsSync(candidate))
|
|
276
|
+
if (fs.existsSync(candidate)) {
|
|
277
|
+
resolveCache.set(cacheKey, candidate);
|
|
278
|
+
return candidate;
|
|
279
|
+
}
|
|
273
280
|
}
|
|
281
|
+
resolveCache.set(cacheKey, void 0);
|
|
274
282
|
}
|
|
275
283
|
},
|
|
276
284
|
load(id) {
|
|
277
285
|
if (id === "\0virtual:react-native") return "export default {};";
|
|
278
|
-
if (id.startsWith("\0virtual:rn-subpath:"))
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
286
|
+
if (id.startsWith("\0virtual:rn-subpath:")) {
|
|
287
|
+
let code = virtualCodeCache.get("\0rn-subpath");
|
|
288
|
+
if (!code) {
|
|
289
|
+
code = [
|
|
290
|
+
`const _rn = globalThis.__vitest_native_mock || {};`,
|
|
291
|
+
...RN_EXPORT_NAMES.map((n) => `export const ${n} = _rn['${n}'];`),
|
|
292
|
+
`export default _rn;`
|
|
293
|
+
].join("\n");
|
|
294
|
+
virtualCodeCache.set("\0rn-subpath", code);
|
|
295
|
+
}
|
|
296
|
+
return code;
|
|
297
|
+
}
|
|
283
298
|
if (id.startsWith("\0virtual:preset:")) {
|
|
299
|
+
const cached = virtualCodeCache.get(id);
|
|
300
|
+
if (cached) return cached;
|
|
284
301
|
const moduleName = id.slice(16);
|
|
285
302
|
const exportNames = presetExportNames.get(moduleName) || [];
|
|
286
|
-
|
|
303
|
+
const code = [
|
|
287
304
|
`const _m = (globalThis.__vitest_native_preset_mocks || {})['${moduleName}'] || {};`,
|
|
288
305
|
...exportNames.map((n) => `export const ${n} = _m['${n}'];`),
|
|
289
306
|
`export default _m;`
|
|
290
307
|
].join("\n");
|
|
308
|
+
virtualCodeCache.set(id, code);
|
|
309
|
+
return code;
|
|
291
310
|
}
|
|
292
311
|
const fsPath = stripFsPrefix(id);
|
|
293
312
|
if (assetPattern.test(fsPath)) return `export default "${fsPath.split("/").pop() ?? fsPath}";`;
|
|
313
|
+
},
|
|
314
|
+
transform(code, id) {
|
|
315
|
+
if (!id.includes("node_modules")) return void 0;
|
|
316
|
+
if (!id.includes("react-native") || !id.endsWith(".js")) return void 0;
|
|
317
|
+
if (!code.includes("@flow")) return void 0;
|
|
318
|
+
const stripped = flowRemoveTypes(code, { all: true });
|
|
319
|
+
return {
|
|
320
|
+
code: stripped.toString(),
|
|
321
|
+
map: stripped.generateMap()
|
|
322
|
+
};
|
|
294
323
|
}
|
|
295
324
|
};
|
|
296
325
|
}
|
|
@@ -11,6 +11,7 @@ const AUTO_DETECT_PRESETS = {
|
|
|
11
11
|
"@react-navigation/native-stack": "navigation",
|
|
12
12
|
"@react-navigation/bottom-tabs": "navigation",
|
|
13
13
|
"@react-navigation/elements": "navigation",
|
|
14
|
+
"@react-navigation/drawer": "navigation",
|
|
14
15
|
"@react-native-async-storage/async-storage": "asyncStorage",
|
|
15
16
|
"react-native-screens": "screens",
|
|
16
17
|
"expo-constants": "expo"
|
|
@@ -11,6 +11,7 @@ const AUTO_DETECT_PRESETS = {
|
|
|
11
11
|
"@react-navigation/native-stack": "navigation",
|
|
12
12
|
"@react-navigation/bottom-tabs": "navigation",
|
|
13
13
|
"@react-navigation/elements": "navigation",
|
|
14
|
+
"@react-navigation/drawer": "navigation",
|
|
14
15
|
"@react-native-async-storage/async-storage": "asyncStorage",
|
|
15
16
|
"react-native-screens": "screens",
|
|
16
17
|
"expo-constants": "expo"
|