GitHub

Caution

This package is deprecated and is no longer actively maintained.

We encourage you to migrate to React Native Testing Library, v12.4 or later, which includes modern built-in Jest matchers based on the matchers for this repository.

The migration process should be relatively straightforward, we have a migration guide available.

eagle

Custom jest matchers to test the state of React Native.


version Code Coverage Build downloads PRs Welcome Discord Star on GitHub

Table of Contents

The problem

You want to use jest to write tests that assert various things about the state of a React Native app. As part of that goal, you want to avoid all the repetitive patterns that arise in doing so like checking for a native element's props, its text content, its styles, and more.

This solution

The jest-native library provides a set of custom jest matchers that you can use to extend jest. These will make your tests more declarative, clear to read and to maintain.

Compatibility

These matchers should, for the most part, be agnostic enough to work with any React Native testing utilities, but they are primarily intended to be used with React Native Testing Library. Any issues raised with existing matchers or any newly proposed matchers must be viewed through compatibility with that library and its guiding principles first.

Installation

This module should be installed as one of your project's devDependencies:

Using yarn

yarn add --dev @testing-library/jest-native

Using npm

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

You will need react-test-renderer, react, and react-native installed in order to use this package.

Usage

Extending Jest matchers

Import @testing-library/jest-native/extend-expect once (for instance in your tests setup file) and you're good to go:

import '@testing-library/jest-native/extend-expect';

Alternatively, you can selectively import only the matchers you intend to use, and extend jest's expect yourself:

import { toBeEmptyElement, toHaveTextContent } from '@testing-library/jest-native';
expect.extend({ toBeEmptyElement, toHaveTextContent });

TypeScript support

In order to setup proper TypeScript type checking use either one of the following approches.

1. Use TypeScript Jest setup file.

Use jest-setup.ts file (instead of jest-setup.js file) which is added to Jest config's setupFilesAfterEnv option.

The Jest setup file should contain following line:

import '@testing-library/jest-native/extend-expect';

This should enable TypeScript checkign for both tsc and VS Code intellisense.

2. Use declarations.d.ts file

Alternatively, create declarations.d.ts file at the root level of your project, if it does not exist already.

Add following line at the top of your declarations.d.ts:

/// <reference types="@testing-library/jest-native" />

This should enable TypeScript checkign for both tsc and VS Code intellisense.

Matchers

jest-native has only been tested to work with React Native Testing Library. Keep in mind that these queries are intended only to work with elements corresponding to host components.

toBeDisabled

toBeDisabled();

Check whether or not an element is disabled from a user perspective.

This matcher will check if the element or its parent has any of the following props :

  • disabled
  • accessibilityState={{ disabled: true }}
  • editable={false} (for TextInput only)

Examples

"const { getByTestId } = render(

Read the original on github.com ↗