vitest-native 0.1.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/LICENSE +21 -0
- package/README.md +456 -0
- package/dist/chunk-CjcI7cDX.mjs +15 -0
- package/dist/helpers.cjs +113 -0
- package/dist/helpers.d.cts +19 -0
- package/dist/helpers.d.mts +19 -0
- package/dist/helpers.mjs +108 -0
- package/dist/index-CJqS-y_M.d.cts +268 -0
- package/dist/index-h4vpIkQO.d.mts +266 -0
- package/dist/index.cjs +308 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.mjs +299 -0
- package/dist/preset-map-C5d8DSY7.cjs +21 -0
- package/dist/preset-map-CTAjv5aa.mjs +16 -0
- package/dist/presets-ArwboRy7.cjs +980 -0
- package/dist/presets-DgEuMxfn.mjs +896 -0
- package/dist/presets.cjs +9 -0
- package/dist/presets.d.cts +2 -0
- package/dist/presets.d.mts +2 -0
- package/dist/presets.mjs +2 -0
- package/dist/serializer.cjs +118 -0
- package/dist/serializer.d.cts +16 -0
- package/dist/serializer.d.mts +16 -0
- package/dist/serializer.mjs +117 -0
- package/dist/setup.cjs +1869 -0
- package/dist/setup.d.cts +1 -0
- package/dist/setup.d.mts +1 -0
- package/dist/setup.mjs +1869 -0
- package/package.json +124 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Fry
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
# vitest-native
|
|
2
|
+
|
|
3
|
+
A Vitest plugin for React Native. One install, zero config.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Table of Contents
|
|
8
|
+
|
|
9
|
+
- [Why vitest-native?](#why-vitest-native)
|
|
10
|
+
- [Installation](#installation)
|
|
11
|
+
- [Quick Start](#quick-start)
|
|
12
|
+
- [Plugin Options](#plugin-options)
|
|
13
|
+
- [Mocked Components and APIs](#mocked-components-and-apis)
|
|
14
|
+
- [Test Helpers](#test-helpers)
|
|
15
|
+
- [Auto-Detect Presets](#auto-detect-presets)
|
|
16
|
+
- [RNTL Matchers](#rntl-matchers)
|
|
17
|
+
- [Snapshot Serializer](#snapshot-serializer)
|
|
18
|
+
- [Platform Extensions](#platform-extensions)
|
|
19
|
+
- [Asset Stubs](#asset-stubs)
|
|
20
|
+
- [Diagnostics](#diagnostics)
|
|
21
|
+
- [Requirements](#requirements)
|
|
22
|
+
- [License](#license)
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Why vitest-native?
|
|
27
|
+
|
|
28
|
+
Testing React Native code with Jest has traditionally required a patchwork of configuration: custom transformers, manual mock files, preset packages, and brittle `jest.setup.js` scripts that break across React Native versions. Each new library (Reanimated, Gesture Handler, Safe Area, Navigation) adds another mock to maintain.
|
|
29
|
+
|
|
30
|
+
`vitest-native` replaces all of that with a single Vite plugin:
|
|
31
|
+
|
|
32
|
+
- **One line of config.** Add the plugin and every mock, resolver, and setup file is handled for you.
|
|
33
|
+
- **Vitest-native speed.** Vitest runs on Vite's dev server -- no cold starts, native ESM, and fast HMR-driven watch mode.
|
|
34
|
+
- **Comprehensive mocks.** 21 components and 25+ APIs are mocked out of the box, with full TypeScript types.
|
|
35
|
+
- **Auto-detect presets.** The plugin scans your `node_modules` and automatically mocks third-party libraries like Reanimated, Gesture Handler, and Expo modules.
|
|
36
|
+
- **Test helpers.** Switch platform, dimensions, and color scheme in a single function call -- no manual mock wiring.
|
|
37
|
+
|
|
38
|
+
If you are already using Vitest for your web projects, `vitest-native` lets you use the same tool, the same config patterns, and the same test runner for React Native.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
# bun
|
|
46
|
+
bun add -d vitest-native
|
|
47
|
+
|
|
48
|
+
# npm
|
|
49
|
+
npm install -D vitest-native
|
|
50
|
+
|
|
51
|
+
# yarn
|
|
52
|
+
yarn add -D vitest-native
|
|
53
|
+
|
|
54
|
+
# pnpm
|
|
55
|
+
pnpm add -D vitest-native
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Quick Start
|
|
61
|
+
|
|
62
|
+
Create or update your `vitest.config.ts`:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { defineConfig } from 'vitest/config';
|
|
66
|
+
import { reactNative } from 'vitest-native';
|
|
67
|
+
|
|
68
|
+
export default defineConfig({
|
|
69
|
+
plugins: [reactNative()],
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
That is it. Write a test:
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import { render, screen } from '@testing-library/react-native';
|
|
77
|
+
import { View, Text } from 'react-native';
|
|
78
|
+
|
|
79
|
+
function Greeting({ name }: { name: string }) {
|
|
80
|
+
return (
|
|
81
|
+
<View>
|
|
82
|
+
<Text>Hello, {name}</Text>
|
|
83
|
+
</View>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
test('renders a greeting', () => {
|
|
88
|
+
render(<Greeting name="World" />);
|
|
89
|
+
expect(screen.getByText('Hello, World')).toBeTruthy();
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Run it:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
npx vitest
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Plugin Options
|
|
102
|
+
|
|
103
|
+
The `reactNative()` function accepts an optional configuration object:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { defineConfig } from 'vitest/config';
|
|
107
|
+
import { reactNative } from 'vitest-native';
|
|
108
|
+
|
|
109
|
+
export default defineConfig({
|
|
110
|
+
plugins: [
|
|
111
|
+
reactNative({
|
|
112
|
+
platform: 'ios', // 'ios' | 'android' (default: 'ios')
|
|
113
|
+
presets: [], // Preset[] -- omit for auto-detect
|
|
114
|
+
mocks: {}, // Custom mock overrides for the react-native module
|
|
115
|
+
diagnostics: false, // Enable verbose logging
|
|
116
|
+
assetExts: ['.lottie'], // Additional asset extensions to stub
|
|
117
|
+
}),
|
|
118
|
+
],
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
| Option | Type | Default | Description |
|
|
123
|
+
|---|---|---|---|
|
|
124
|
+
| `platform` | `'ios' \| 'android'` | `'ios'` | Target platform. Controls `Platform.OS`, version defaults, and file extension resolution. |
|
|
125
|
+
| `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. |
|
|
126
|
+
| `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. |
|
|
127
|
+
| `diagnostics` | `boolean` | `false` | Log plugin activity to the console for debugging. |
|
|
128
|
+
| `assetExts` | `string[]` | `[]` | Additional file extensions to stub as asset imports (beyond the built-in set). |
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Mocked Components and APIs
|
|
133
|
+
|
|
134
|
+
The plugin provides a complete mock of the `react-native` module. Every component renders as a named host element (making snapshots readable), and every API function is a Vitest `vi.fn()` spy.
|
|
135
|
+
|
|
136
|
+
### Components (21)
|
|
137
|
+
|
|
138
|
+
| Component | Component | Component |
|
|
139
|
+
|---|---|---|
|
|
140
|
+
| View | Text | Image |
|
|
141
|
+
| TextInput | ScrollView | FlatList |
|
|
142
|
+
| SectionList | Modal | Pressable |
|
|
143
|
+
| TouchableOpacity | TouchableHighlight | TouchableWithoutFeedback |
|
|
144
|
+
| TouchableNativeFeedback | ActivityIndicator | Button |
|
|
145
|
+
| Switch | RefreshControl | StatusBar |
|
|
146
|
+
| SafeAreaView | KeyboardAvoidingView | ImageBackground |
|
|
147
|
+
| VirtualizedList | InputAccessoryView | DrawerLayoutAndroid |
|
|
148
|
+
|
|
149
|
+
### APIs (25+)
|
|
150
|
+
|
|
151
|
+
| API | API | API |
|
|
152
|
+
|---|---|---|
|
|
153
|
+
| Platform | Dimensions | StyleSheet |
|
|
154
|
+
| Animated | Alert | Linking |
|
|
155
|
+
| AppState | Keyboard | BackHandler |
|
|
156
|
+
| Vibration | PermissionsAndroid | Appearance |
|
|
157
|
+
| PixelRatio | LayoutAnimation | Clipboard |
|
|
158
|
+
| Share | AccessibilityInfo | InteractionManager |
|
|
159
|
+
| PanResponder | ToastAndroid | ActionSheetIOS |
|
|
160
|
+
| LogBox | Easing | I18nManager |
|
|
161
|
+
| DeviceEventEmitter | | |
|
|
162
|
+
|
|
163
|
+
### Native Bridge
|
|
164
|
+
|
|
165
|
+
| Export | Description |
|
|
166
|
+
|---|---|
|
|
167
|
+
| `NativeModules` | Proxy that returns no-op modules for any key |
|
|
168
|
+
| `TurboModuleRegistry` | Proxy with `getEnforcing` / `get` stubs |
|
|
169
|
+
| `UIManager` | Stubbed layout manager |
|
|
170
|
+
| `NativeEventEmitter` | Event emitter constructor mock |
|
|
171
|
+
| `requireNativeComponent` | Returns a named component mock |
|
|
172
|
+
|
|
173
|
+
### Hooks
|
|
174
|
+
|
|
175
|
+
| Hook | Default Value |
|
|
176
|
+
|---|---|
|
|
177
|
+
| `useColorScheme` | `'light'` |
|
|
178
|
+
| `useWindowDimensions` | `{ width: 390, height: 844, scale: 3, fontScale: 1 }` |
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## Test Helpers
|
|
183
|
+
|
|
184
|
+
Import helpers from `vitest-native/helpers` to control mock state during tests.
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
import {
|
|
188
|
+
setPlatform,
|
|
189
|
+
setDimensions,
|
|
190
|
+
setColorScheme,
|
|
191
|
+
mockNativeModule,
|
|
192
|
+
resetAllMocks,
|
|
193
|
+
} from 'vitest-native/helpers';
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### `setPlatform(os)`
|
|
197
|
+
|
|
198
|
+
Switch the platform for the current test. Updates `Platform.OS`, `Platform.Version`, and `Platform.select`.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
import { setPlatform } from 'vitest-native/helpers';
|
|
202
|
+
|
|
203
|
+
test('renders Android-specific UI', () => {
|
|
204
|
+
setPlatform('android');
|
|
205
|
+
// Platform.OS is now 'android', Platform.Version is 34
|
|
206
|
+
// Platform.select({ ios: 'A', android: 'B' }) returns 'B'
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### `setDimensions(dims)`
|
|
211
|
+
|
|
212
|
+
Update `Dimensions.get()` and the `useWindowDimensions` hook return value.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
import { setDimensions } from 'vitest-native/helpers';
|
|
216
|
+
|
|
217
|
+
test('adapts to tablet dimensions', () => {
|
|
218
|
+
setDimensions({ width: 768, height: 1024, scale: 2, fontScale: 1 });
|
|
219
|
+
// Dimensions.get('window') returns { width: 768, height: 1024, ... }
|
|
220
|
+
// useWindowDimensions() returns the same
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### `setColorScheme(scheme)`
|
|
225
|
+
|
|
226
|
+
Switch the color scheme. Affects `Appearance.getColorScheme()` and the `useColorScheme` hook.
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
import { setColorScheme } from 'vitest-native/helpers';
|
|
230
|
+
|
|
231
|
+
test('renders dark mode styles', () => {
|
|
232
|
+
setColorScheme('dark');
|
|
233
|
+
// useColorScheme() returns 'dark'
|
|
234
|
+
// Appearance.getColorScheme() returns 'dark'
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### `mockNativeModule(name, impl)`
|
|
239
|
+
|
|
240
|
+
Register a custom native module mock. The module becomes available via `NativeModules[name]`.
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
import { mockNativeModule } from 'vitest-native/helpers';
|
|
244
|
+
|
|
245
|
+
test('uses a custom native module', () => {
|
|
246
|
+
mockNativeModule('MyBridge', {
|
|
247
|
+
getValue: vi.fn().mockResolvedValue(42),
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const { NativeModules } = require('react-native');
|
|
251
|
+
await expect(NativeModules.MyBridge.getValue()).resolves.toBe(42);
|
|
252
|
+
});
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### `resetAllMocks()`
|
|
256
|
+
|
|
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.
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
import { resetAllMocks } from 'vitest-native/helpers';
|
|
261
|
+
|
|
262
|
+
afterEach(() => {
|
|
263
|
+
resetAllMocks();
|
|
264
|
+
});
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## Auto-Detect Presets
|
|
270
|
+
|
|
271
|
+
When the `presets` option is omitted, the plugin scans your `node_modules` and automatically enables mocks for installed third-party libraries:
|
|
272
|
+
|
|
273
|
+
| Package | What Gets Mocked |
|
|
274
|
+
|---|---|
|
|
275
|
+
| `react-native-reanimated` | Animated values, `useSharedValue`, `useAnimatedStyle`, layout animations |
|
|
276
|
+
| `react-native-gesture-handler` | Gesture components, `GestureHandlerRootView`, state constants |
|
|
277
|
+
| `react-native-safe-area-context` | `SafeAreaProvider`, `useSafeAreaInsets`, `SafeAreaView` |
|
|
278
|
+
| `@react-navigation/native` | `NavigationContainer`, `useNavigation`, `useRoute` |
|
|
279
|
+
| `@react-native-async-storage/async-storage` | `getItem`, `setItem`, `removeItem`, `clear`, `getAllKeys` |
|
|
280
|
+
| `react-native-screens` | `enableScreens`, screen components |
|
|
281
|
+
| `expo` | `expo-constants`, `expo-font`, `expo-asset`, `expo-splash-screen`, `expo-linking`, `expo-status-bar` |
|
|
282
|
+
|
|
283
|
+
Auto-detection means zero additional config for most projects. If you need to restrict which presets are active, pass them explicitly:
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
import { defineConfig } from 'vitest/config';
|
|
287
|
+
import { reactNative, presets } from 'vitest-native';
|
|
288
|
+
|
|
289
|
+
export default defineConfig({
|
|
290
|
+
plugins: [
|
|
291
|
+
reactNative({
|
|
292
|
+
presets: [
|
|
293
|
+
presets.reanimated(),
|
|
294
|
+
presets.gestureHandler(),
|
|
295
|
+
presets.safeAreaContext(),
|
|
296
|
+
],
|
|
297
|
+
}),
|
|
298
|
+
],
|
|
299
|
+
});
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Available Presets
|
|
303
|
+
|
|
304
|
+
| Import | Function |
|
|
305
|
+
|---|---|
|
|
306
|
+
| `presets.reanimated()` | react-native-reanimated |
|
|
307
|
+
| `presets.gestureHandler()` | react-native-gesture-handler |
|
|
308
|
+
| `presets.safeAreaContext()` | react-native-safe-area-context |
|
|
309
|
+
| `presets.navigation()` | @react-navigation/native |
|
|
310
|
+
| `presets.asyncStorage()` | @react-native-async-storage/async-storage |
|
|
311
|
+
| `presets.screens()` | react-native-screens |
|
|
312
|
+
| `presets.expo()` | Expo modules |
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
## RNTL Matchers
|
|
317
|
+
|
|
318
|
+
When `@testing-library/react-native` v12.9 or later is installed, `vitest-native` auto-registers its custom matchers. No manual `extend(matchers)` call or setup file is needed.
|
|
319
|
+
|
|
320
|
+
The following matchers become available on `expect()`:
|
|
321
|
+
|
|
322
|
+
| Matcher | Description |
|
|
323
|
+
|---|---|
|
|
324
|
+
| `toBeVisible()` | Element is visible |
|
|
325
|
+
| `toBeEmptyElement()` | Element has no children |
|
|
326
|
+
| `toBeEnabled()` | Element is not disabled |
|
|
327
|
+
| `toBeDisabled()` | Element is disabled |
|
|
328
|
+
| `toHaveTextContent(text)` | Element contains the given text |
|
|
329
|
+
| `toHaveProp(name, value?)` | Element has the specified prop |
|
|
330
|
+
| `toHaveStyle(style)` | Element has the specified styles |
|
|
331
|
+
| `toBeOnTheScreen()` | Element is in the component tree |
|
|
332
|
+
| `toContainElement(element)` | Element contains the given child |
|
|
333
|
+
| `toHaveAccessibilityState(state)` | Element has the specified accessibility state |
|
|
334
|
+
| `toHaveAccessibilityValue(value)` | Element has the specified accessibility value |
|
|
335
|
+
| `toBeSelected()` | Element is selected |
|
|
336
|
+
| `toBeChecked()` | Element is checked |
|
|
337
|
+
| `toBePartiallyChecked()` | Element is partially checked |
|
|
338
|
+
| `toBeBusy()` | Element is busy |
|
|
339
|
+
| `toBeExpanded()` | Element is expanded |
|
|
340
|
+
| `toBeCollapsed()` | Element is collapsed |
|
|
341
|
+
|
|
342
|
+
```tsx
|
|
343
|
+
import { render, screen } from '@testing-library/react-native';
|
|
344
|
+
import { View, Text } from 'react-native';
|
|
345
|
+
|
|
346
|
+
test('greeting is visible with correct style', () => {
|
|
347
|
+
render(
|
|
348
|
+
<View>
|
|
349
|
+
<Text style={{ color: 'red', fontSize: 18 }}>Hello</Text>
|
|
350
|
+
</View>
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
const text = screen.getByText('Hello');
|
|
354
|
+
expect(text).toBeVisible();
|
|
355
|
+
expect(text).toHaveStyle({ color: 'red' });
|
|
356
|
+
expect(text).toHaveTextContent('Hello');
|
|
357
|
+
});
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
## Snapshot Serializer
|
|
363
|
+
|
|
364
|
+
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:
|
|
365
|
+
|
|
366
|
+
```
|
|
367
|
+
<View>
|
|
368
|
+
<Text
|
|
369
|
+
style={
|
|
370
|
+
{
|
|
371
|
+
"fontSize": 18,
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
>
|
|
375
|
+
Hello, World
|
|
376
|
+
</Text>
|
|
377
|
+
</View>
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
No configuration is required. The serializer is active for all snapshot tests automatically.
|
|
381
|
+
|
|
382
|
+
---
|
|
383
|
+
|
|
384
|
+
## Platform Extensions
|
|
385
|
+
|
|
386
|
+
The plugin resolves platform-specific files following React Native conventions. When `platform` is set to `'ios'` (the default), imports resolve in this order:
|
|
387
|
+
|
|
388
|
+
1. `./Component.ios.ts`
|
|
389
|
+
2. `./Component.native.ts`
|
|
390
|
+
3. `./Component.ts`
|
|
391
|
+
|
|
392
|
+
When `platform` is `'android'`:
|
|
393
|
+
|
|
394
|
+
1. `./Component.android.ts`
|
|
395
|
+
2. `./Component.native.ts`
|
|
396
|
+
3. `./Component.ts`
|
|
397
|
+
|
|
398
|
+
This works for all supported extensions: `.ts`, `.tsx`, `.js`, `.jsx`.
|
|
399
|
+
|
|
400
|
+
```ts
|
|
401
|
+
// vitest.config.ts -- test Android-specific code
|
|
402
|
+
export default defineConfig({
|
|
403
|
+
plugins: [reactNative({ platform: 'android' })],
|
|
404
|
+
});
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
---
|
|
408
|
+
|
|
409
|
+
## Asset Stubs
|
|
410
|
+
|
|
411
|
+
Image, font, video, and audio imports are automatically stubbed. The import resolves to the filename string, so code that passes asset paths through continues to work without errors.
|
|
412
|
+
|
|
413
|
+
```ts
|
|
414
|
+
import logo from './logo.png';
|
|
415
|
+
// logo === 'logo.png'
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
Built-in extensions include common formats like `.png`, `.jpg`, `.gif`, `.svg`, `.mp4`, `.ttf`, `.otf`, and more. To add additional extensions:
|
|
419
|
+
|
|
420
|
+
```ts
|
|
421
|
+
reactNative({
|
|
422
|
+
assetExts: ['.lottie', '.m4b'],
|
|
423
|
+
})
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
---
|
|
427
|
+
|
|
428
|
+
## Diagnostics
|
|
429
|
+
|
|
430
|
+
Enable verbose logging to see exactly what the plugin is doing during configuration and module resolution:
|
|
431
|
+
|
|
432
|
+
```ts
|
|
433
|
+
reactNative({
|
|
434
|
+
diagnostics: true,
|
|
435
|
+
})
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
This prints details about which presets were detected, which modules are being mocked, and how imports are resolved. Useful for debugging unexpected behavior.
|
|
439
|
+
|
|
440
|
+
---
|
|
441
|
+
|
|
442
|
+
## Requirements
|
|
443
|
+
|
|
444
|
+
| Dependency | Version |
|
|
445
|
+
|---|---|
|
|
446
|
+
| `react` | >= 18 |
|
|
447
|
+
| `vite` | >= 5 |
|
|
448
|
+
| `vitest` | >= 4 |
|
|
449
|
+
| `node` | >= 18 |
|
|
450
|
+
| `@testing-library/react-native` (optional) | >= 12 |
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
## License
|
|
455
|
+
|
|
456
|
+
MIT
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __exportAll = (all, no_symbols) => {
|
|
5
|
+
let target = {};
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
11
|
+
return target;
|
|
12
|
+
};
|
|
13
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
14
|
+
//#endregion
|
|
15
|
+
export { __require as n, __exportAll as t };
|
package/dist/helpers.cjs
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/helpers.ts
|
|
3
|
+
/**
|
|
4
|
+
* Test helpers for vitest-native.
|
|
5
|
+
*
|
|
6
|
+
* These functions mutate the live mock instances created by the setup file.
|
|
7
|
+
* Import from 'vitest-native/helpers' in your test files.
|
|
8
|
+
*/
|
|
9
|
+
function getMock() {
|
|
10
|
+
const mock = globalThis.__vitest_native_mock;
|
|
11
|
+
if (!mock) throw new Error("vitest-native helpers called before setup. Ensure the vitest-native plugin is configured.");
|
|
12
|
+
return mock;
|
|
13
|
+
}
|
|
14
|
+
/** The original NativeModules instance, stored so resetAllMocks() can restore it. */
|
|
15
|
+
let originalNativeModules = null;
|
|
16
|
+
function setPlatform(os) {
|
|
17
|
+
const platform = getMock().Platform;
|
|
18
|
+
platform.OS = os;
|
|
19
|
+
platform.Version = os === "ios" ? "17.0" : 34;
|
|
20
|
+
platform.select.mockImplementation((specifics) => {
|
|
21
|
+
return specifics[os] ?? specifics.default;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function setDimensions(dims) {
|
|
25
|
+
const mock = getMock();
|
|
26
|
+
mock.Dimensions.set({
|
|
27
|
+
window: dims,
|
|
28
|
+
screen: dims
|
|
29
|
+
});
|
|
30
|
+
if (mock.useWindowDimensions._setDimensions) mock.useWindowDimensions._setDimensions(dims);
|
|
31
|
+
}
|
|
32
|
+
function setColorScheme(scheme) {
|
|
33
|
+
const mock = getMock();
|
|
34
|
+
const resolved = scheme ?? "light";
|
|
35
|
+
mock.Appearance.setColorScheme(resolved);
|
|
36
|
+
if (mock.useColorScheme._setScheme) mock.useColorScheme._setScheme(resolved);
|
|
37
|
+
}
|
|
38
|
+
function mockNativeModule(name, impl) {
|
|
39
|
+
const mock = getMock();
|
|
40
|
+
if (originalNativeModules === null) originalNativeModules = mock.NativeModules;
|
|
41
|
+
const existing = mock.NativeModules;
|
|
42
|
+
mock.NativeModules = new Proxy(existing, { get(target, prop) {
|
|
43
|
+
if (prop === name) return impl;
|
|
44
|
+
if (typeof prop === "symbol") return void 0;
|
|
45
|
+
return Reflect.get(target, prop) ?? new Proxy({}, { get(_, p) {
|
|
46
|
+
if (typeof p === "symbol") return void 0;
|
|
47
|
+
return () => {};
|
|
48
|
+
} });
|
|
49
|
+
} });
|
|
50
|
+
}
|
|
51
|
+
function clearMockFns(obj, visited = /* @__PURE__ */ new Set()) {
|
|
52
|
+
if (!obj || typeof obj !== "object" || visited.has(obj)) return;
|
|
53
|
+
visited.add(obj);
|
|
54
|
+
for (const [_key, value] of Object.entries(obj)) {
|
|
55
|
+
if (typeof value === "function" && "mockClear" in value) value.mockClear();
|
|
56
|
+
if (typeof value === "object" && value !== null) clearMockFns(value, visited);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function resetAllMocks() {
|
|
60
|
+
const mock = getMock();
|
|
61
|
+
clearMockFns(mock);
|
|
62
|
+
mock.Platform.OS = "ios";
|
|
63
|
+
mock.Platform.Version = "17.0";
|
|
64
|
+
mock.Platform.select.mockImplementation((specifics) => {
|
|
65
|
+
return specifics.ios ?? specifics.default;
|
|
66
|
+
});
|
|
67
|
+
if (mock.Dimensions._reset) mock.Dimensions._reset();
|
|
68
|
+
else mock.Dimensions.set({
|
|
69
|
+
window: {
|
|
70
|
+
width: 390,
|
|
71
|
+
height: 844,
|
|
72
|
+
scale: 3,
|
|
73
|
+
fontScale: 1
|
|
74
|
+
},
|
|
75
|
+
screen: {
|
|
76
|
+
width: 390,
|
|
77
|
+
height: 844,
|
|
78
|
+
scale: 3,
|
|
79
|
+
fontScale: 1
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
if (mock.useWindowDimensions._resetDimensions) mock.useWindowDimensions._resetDimensions({
|
|
83
|
+
width: 390,
|
|
84
|
+
height: 844,
|
|
85
|
+
scale: 3,
|
|
86
|
+
fontScale: 1
|
|
87
|
+
});
|
|
88
|
+
else if (mock.useWindowDimensions._setDimensions) mock.useWindowDimensions._setDimensions({
|
|
89
|
+
width: 390,
|
|
90
|
+
height: 844,
|
|
91
|
+
scale: 3,
|
|
92
|
+
fontScale: 1
|
|
93
|
+
});
|
|
94
|
+
if (mock.Appearance._reset) mock.Appearance._reset();
|
|
95
|
+
else mock.Appearance.setColorScheme("light");
|
|
96
|
+
if (mock.useColorScheme._resetScheme) mock.useColorScheme._resetScheme("light");
|
|
97
|
+
else if (mock.useColorScheme._setScheme) mock.useColorScheme._setScheme("light");
|
|
98
|
+
if (mock.Keyboard._reset) mock.Keyboard._reset();
|
|
99
|
+
if (mock.AppState._reset) mock.AppState._reset();
|
|
100
|
+
if (mock.BackHandler._reset) mock.BackHandler._reset();
|
|
101
|
+
if (mock.I18nManager._reset) mock.I18nManager._reset();
|
|
102
|
+
if (mock.DeviceEventEmitter._reset) mock.DeviceEventEmitter._reset();
|
|
103
|
+
if (originalNativeModules !== null) {
|
|
104
|
+
mock.NativeModules = originalNativeModules;
|
|
105
|
+
originalNativeModules = null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
exports.mockNativeModule = mockNativeModule;
|
|
110
|
+
exports.resetAllMocks = resetAllMocks;
|
|
111
|
+
exports.setColorScheme = setColorScheme;
|
|
112
|
+
exports.setDimensions = setDimensions;
|
|
113
|
+
exports.setPlatform = setPlatform;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/helpers.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Test helpers for vitest-native.
|
|
4
|
+
*
|
|
5
|
+
* These functions mutate the live mock instances created by the setup file.
|
|
6
|
+
* Import from 'vitest-native/helpers' in your test files.
|
|
7
|
+
*/
|
|
8
|
+
declare function setPlatform(os: "ios" | "android"): void;
|
|
9
|
+
declare function setDimensions(dims: {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
scale?: number;
|
|
13
|
+
fontScale?: number;
|
|
14
|
+
}): void;
|
|
15
|
+
declare function setColorScheme(scheme: "light" | "dark" | null): void;
|
|
16
|
+
declare function mockNativeModule(name: string, impl: Record<string, any>): void;
|
|
17
|
+
declare function resetAllMocks(): void;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setPlatform };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/helpers.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Test helpers for vitest-native.
|
|
4
|
+
*
|
|
5
|
+
* These functions mutate the live mock instances created by the setup file.
|
|
6
|
+
* Import from 'vitest-native/helpers' in your test files.
|
|
7
|
+
*/
|
|
8
|
+
declare function setPlatform(os: "ios" | "android"): void;
|
|
9
|
+
declare function setDimensions(dims: {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
scale?: number;
|
|
13
|
+
fontScale?: number;
|
|
14
|
+
}): void;
|
|
15
|
+
declare function setColorScheme(scheme: "light" | "dark" | null): void;
|
|
16
|
+
declare function mockNativeModule(name: string, impl: Record<string, any>): void;
|
|
17
|
+
declare function resetAllMocks(): void;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setPlatform };
|