• This repository has been archived on 31/Jul/2020
  • Stars
    star
    516
  • Rank 82,777 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🐳 Simple and complete React Native testing utilities that encourage good testing practices.

⚠️ Deprecation notice ⚠️

This repository has been deprecated in favor of https://github.com/callstack/react-native-testing-library and the @testing-library/react-native npm package will from now on (since v7.0) will be sourced from there. Please consult the migration guide.

Native Testing Library

whale

Simple and complete React Native testing utilities that encourage good testing practices.

Read The Docs | Edit the docs


Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Code of Conduct Discord

Watch on GitHub Star on GitHub

Table of Contents

The problem

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.

This solution

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.

Example

import React from 'react';
import { Button, Text, TextInput, View } from 'react-native';
import { fireEvent, render, wait } from '@testing-library/react-native';

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, baseElement } = 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(baseElement).toMatchSnapshot();
});

Installation

This module should be installed in your project's devDependencies:

npm install --save-dev @testing-library/react-native

You will need react and react-native installed as dependencies in order to run this project.

Hooks

If you are interested in testing a custom hook, check out react-hooks-testing-library.

Other Solutions

Guiding principles

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:

  1. If it relates to rendering components, it deals with native views rather than component instances, nor should it encourage dealing with component instances.
  2. It should be generally useful for testing the application components in the way the user would use it. We are making some trade-offs here because we're using a computer and often a simulated environment, but in general, utilities should encourage tests that use the components the way they're intended to be used.
  3. Utility implementations and APIs should be simple and flexible.

In summary, we believe in the principles of testing-library, and adhere to them as closely as possible. At the end of the day, what we want is for this library to be pretty light-weight, simple, and understandable.

Inspiration

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.

Contributors

Thanks goes to these wonderful people (emoji key):


Brandon Carroll

πŸ’» πŸ“– πŸš‡ ⚠️

Tommy Graves

πŸ€” 🚧 πŸ‘€

Kent C. Dodds

πŸ€”

Piotr Szlachciak

πŸ’»

mcgloneleviROOT

πŸ› πŸ’»

Kevin Sullivan

πŸ“–

Ely Alvarado

πŸ’»

Lewis Barnes

πŸ’» πŸ’¬

James DiGioia

πŸ’»

mana

πŸ’»

Mateusz MΔ™drek

πŸ’»

Ismail Ghallou

πŸ“–

jeffreyffs

πŸ’»

Sophie Au

πŸ’»

andy

πŸ’» πŸ“–

Aiham

πŸ’»

Sibelius Seraphini

πŸ’»

Alex Egan

πŸ’»

Dave Olsen

πŸ’» ⚠️ πŸ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

Docs

Read The Docs | Edit the docs

More Repositories

1

react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
JavaScript
18,655
star
2

react-hooks-testing-library

🐏 Simple and complete React hooks testing utilities that encourage good testing practices.
TypeScript
5,083
star
3

jest-dom

πŸ¦‰ Custom jest matchers to test the state of the DOM
JavaScript
4,303
star
4

dom-testing-library

πŸ™ Simple and complete DOM testing utilities that encourage good testing practices.
JavaScript
3,234
star
5

user-event

πŸ• Simulate user events
TypeScript
2,119
star
6

cypress-testing-library

πŸ… Simple and complete custom Cypress commands and utilities that encourage good testing practices.
JavaScript
1,782
star
7

vue-testing-library

🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.
JavaScript
1,047
star
8

eslint-plugin-testing-library

ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library
TypeScript
958
star
9

testing-playground

Simple and complete DOM testing playground that encourage good testing practices.
JavaScript
770
star
10

angular-testing-library

πŸ¦” Simple and complete Angular testing utilities that encourage good testing practices
TypeScript
626
star
11

svelte-testing-library

🐿️ Simple and complete Svelte DOM testing utilities that encourage good testing practices
JavaScript
601
star
12

testing-library-docs

docs site for @testing-library/*
JavaScript
442
star
13

jest-native

πŸ¦… Custom jest matchers to test the state of React Native
TypeScript
432
star
14

eslint-plugin-jest-dom

eslint rules for use with jest-dom
JavaScript
352
star
15

pptr-testing-library

puppeteer + dom-testing-library = πŸ’–
TypeScript
283
star
16

playwright-testing-library

πŸ” Find elements in Playwright with queries from Testing Library
TypeScript
246
star
17

testing-library-recorder-extension

Testing Library Extension for Chrome DevTools Recorder
TypeScript
143
star
18

preact-testing-library

Simple and complete Preact DOM testing utilities that encourage good testing practices.
JavaScript
139
star
19

which-query

🦩 Which query should I use?
CSS
124
star
20

testcafe-testing-library

πŸ‚ Simple and complete custom Selectors for Testcafe that encourage good testing practices.
TypeScript
71
star
21

preact-hooks-testing-library

Simple and complete Preact hooks testing utilities that encourage good testing practices.
TypeScript
56
star
22

jasmine-dom

πŸ¦₯ Custom Jasmine matchers to test the state of the DOM
JavaScript
45
star
23

nightwatch-testing-library

πŸ¦‡Simple and complete custom queries for Nightwatch that encourage good testing practices.
JavaScript
31
star
24

dom-testing-library-template

Template repository for bug reports to @testing-library/dom, @testing-library/react, and @testing-library/jest-dom
JavaScript
17
star
25

webdriverio-testing-library

πŸ•·οΈ Simple and complete WebdriverIO DOM testing utilities that encourage good testing practices.
TypeScript
16
star
26

native-testing-library-docs

🐳 Docs site for native-testing-library
JavaScript
16
star
27

react-testing-library-help

Fork this repo to reproduce your issue
HTML
12
star
28

web-testing-library

πŸ™ Experimental Web testing utilities that encourage good testing practices.
JavaScript
3
star