You want to write maintainable tests for your React Native application. You love Kent Dodds' testing library, and you want to be able to write maintainable tests for your React Native application. You don't want to use a library that renders components to a fake DOM, and you've had a hard time finding what you need to write tests using that philosophy in React Native.
native-testing-library
is an implementation of the well-known testing-library API that works for
React Native. The primary goal is to mimic the testing library API as closely as possible while
still accounting for the differences in the platforms.
import React from 'react';
import { Button, Text, TextInput, View } from 'react-native';
import { act, fireEvent, render, wait } from 'native-testing-library';
function Example() {
const [name, setUser] = React.useState('');
const [show, setShow] = React.useState(false);
return (
<View>
<TextInput value={name} onChangeText={setUser} testID="input" />
<Button
title="Print Username"
onPress={() => {
// let's pretend this is making a server request, so it's async
// (you'd want to mock this imaginary request in your unit tests)...
setTimeout(() => {
setShow(!show);
}, Math.floor(Math.random() * 200));
}}
/>
{show && <Text testID="printed-username">{name}</Text>}
</View>
);
}
test('examples of some things', async () => {
const { getByTestId, getByText, queryByTestId, rootInstance } = render(<Example />);
const famousWomanInHistory = 'Ada Lovelace';
const input = getByTestId('input');
fireEvent.changeText(input, famousWomanInHistory);
const button = getByText('Print Username');
fireEvent.press(button);
await wait(() => expect(queryByTestId('printed-username')).toBeTruthy());
expect(getByTestId('printed-username').props.children).toBe(famousWomanInHistory);
expect(rootInstance).toMatchSnapshot();
});
The more your tests resemble the way your software is used, the more confidence they can give you.
We try to only expose methods and utilities that encourage you to write tests that closely resemble how your apps are used.
Utilities are included in this project based on the following guiding principles:
- Rendering React Native components ultimately creates native views, and those views should be what you test rather than the React component instances you rendered to make them.
- In general, test the way your users use your app. There are instances where you'll need to write unit tests, but try your best to write with this first -- the more your tests resemble the way your app works, the more confident you'll be with your app.
- Be responsible, and remember that testing exists to serve you, not the other way around. If the library isn't working for you, contribute to make it work or do something more intuitive. Make your tests work for you and your team!
In summary, we believe in the principles of dom-testing-library
and its companion libraries, and
try to adhere to them as closely as possible. Changes to this library should always consider how
they relate to what's happening in the other libraries in this family of tools.
This module should be installed in your project's devDependencies
:
npm install --save-dev native-testing-library
You will need react
and react-native
installed as dependencies in order to run this project.
You can test hooks out of the box with this package as follows:
import { renderHook } from 'native-testing-library';
Reads more about hooks on the docs site.
Huge thanks to Kent C. Dodds for evangelizing this approach to testing. We could have never come up
with this library without him π. Check out his awesome work and learn more about testing with
confidence at testingjavascript.com (you won't regret purchasing
it), and of course, use this library's big brother, react-testing-library
for your DOM
applications as well!
The hook testing ability of this library is the same implementation as
react-hooks-testing-library. The only
reason it was included in this package is because we need you to import render from us, not the
dom-testing-library
, and that's an important blocker. Some day, maybe we'll try to allow use of
that library with this one somehow.
The awesome engineers at Callstack built a similar package called
react-native-testing-library
.
If you find yourself needing things like shallow rendering and the ability to query elements
by type and props, you'll definitely want to check it out!