[](https://www.npmjs.com/package/@wojtekmaj/vitest-react-native)  [](https://github.com/wojtekmaj/vitest-react-native/actions)
# Vitest React Native
Run React Native unit and component tests with Vitest. The plugin transforms native JavaScript with React Native's Babel preset and uses its official test mocks in Node.js.
## tl;dr
- Install by executing `npm install --save-dev @wojtekmaj/vitest-react-native vitest vite` or `yarn add --dev @wojtekmaj/vitest-react-native vitest vite`.
- Import by adding `import reactNative from '@wojtekmaj/vitest-react-native'`.
- Use it by adding `reactNative()` to the `plugins` section of your Vitest config.
- For React Native Testing Library matcher types, use the `@wojtekmaj/vitest-react-native/testing-library` import instead.
## Getting started
### Compatibility
#### React Native
The support window follows [React Native's three maintained minor series](https://reactnative.dev/releases/overview). The latest stable release is the primary support target; compatibility with the preceding two series is maintained on a best-effort basis. CI also checks the upcoming release candidate on iOS and Android.
Use `@react-native/babel-preset` and `@react-native/jest-preset` at the same version as your application's `react-native` dependency.
#### Vitest and Vite
Vitest React Native requires Vitest 4 or newer and Vite 7 or newer.
#### Node.js
Vitest React Native requires Node.js 24.15.0 or newer within the 24.x series, or Node.js 26.0.0 or newer.
#### React Native Testing Library
React Native Testing Library 13 or newer is supported and optional. Ordinary unit tests work without it. The plugin integrates its matchers and automatic cleanup with Vitest.
### Installation
Add Vitest React Native to your project by executing `npm install --save-dev @wojtekmaj/vitest-react-native vitest vite` or `yarn add --dev @wojtekmaj/vitest-react-native vitest vite`.
If your application does not already include React Native's Babel and test presets, install their matching versions. For example, for React Native 0.87.0, execute `npm install --save-dev @react-native/babel-preset@0.87.0 @react-native/jest-preset@0.87.0` or `yarn add --dev @react-native/babel-preset@0.87.0 @react-native/jest-preset@0.87.0`.
## Usage
Create `vitest.config.ts` and add the plugin:
```ts
import { defineConfig } from 'vitest/config';
import reactNative from '@wojtekmaj/vitest-react-native';
export default defineConfig({
plugins: [reactNative()],
});
```
If your application's `package.json` does not set `"type": "module"`, name the configuration file `vitest.config.mts`.
Import test APIs such as `it` and `expect` from `vitest` in your `.spec.ts` files. Run your tests with `npx vitest run` or `yarn vitest run`.
### Component tests
Install React Native Testing Library and its renderer by executing `npm install --save-dev @testing-library/react-native test-renderer` or `yarn add --dev @testing-library/react-native test-renderer`.
For React Native Testing Library 13, use `react-test-renderer` at the same version as React instead of `test-renderer`.
In `vitest.config.ts`, use the Testing Library entry point:
```ts
import { defineConfig } from 'vitest/config';
import reactNative from '@wojtekmaj/vitest-react-native/testing-library';
export default defineConfig({
plugins: [reactNative()],
});
```
This includes native matcher types such as `toBeOnTheScreen()` and `toHaveTextContent()`. Include your Vitest configuration file in your TypeScript project.
For example, create `Pressable.spec.tsx`:
```tsx
import { expect, it, vi } from 'vitest';
import { Pressable, Text } from 'react-native';
import { fireEvent, render, screen } from '@testing-library/react-native';
it('should call the press handler', async () => {
const onPress = vi.fn();
await render(
|
|
Wojciech Maj |