MCPcopy
hub / github.com/callstack/react-native-testing-library

github.com/callstack/react-native-testing-library @v14.0.1 sqlite

repository ↗ · DeepWiki ↗ · release v14.0.1 ↗
540 symbols 1,875 edges 326 files 35 documented · 6%
README

React Native Testing Library

Developer-friendly and complete React Native testing utilities that encourage good testing practices.

Version Build Status Code Coverage Downloads MIT License Sponsored by Callstack

The problem

You want to write maintainable tests for your React Native components. Your tests should avoid implementation details and focus on giving you confidence. They should remain maintainable so refactors (changes to implementation but not functionality) don't break your tests and slow you and your team down.

This solution

The React Native Testing Library (RNTL) tests React Native components. It simulates the React Native runtime on top of react-test-renderer and encourages better testing practices. Its primary guiding principle is:

The more your tests resemble the way your software is used, the more confidence they can give you.

This project is inspired by React Testing Library. Tested to work with Jest, but it should work with other test runners as well.

Installation

Open a Terminal in your project's folder and run:

# Yarn install:
yarn add --dev @testing-library/react-native

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

This library has a peerDependencies listing for react-test-renderer. Make sure that your react-test-renderer version matches exactly the react version, avoid using ^ in version number.

Additional Jest matchers

You can use the built-in Jest matchers automatically by having any import from @testing-library/react-native in your test.

Example

import { render, screen, userEvent } from '@testing-library/react-native';
import { QuestionsBoard } from '../QuestionsBoard';

// Use userEvent with fake timers
// Some events involve duration, so tests may take a long time to run.
jest.useFakeTimers();

test('form submits two answers', async () => {
  const questions = ['q1', 'q2'];
  const onSubmit = jest.fn();

  const user = userEvent.setup();
  render(<QuestionsBoard questions={questions} onSubmit={onSubmit} />);

  const answerInputs = screen.getAllByLabelText('answer input');

  // simulates the user focusing on TextInput and typing text one char at a time
  await user.type(answerInputs[0], 'a1');
  await user.type(answerInputs[1], 'a2');

  // simulates the user pressing on any pressable element
  await user.press(screen.getByRole('button', { name: 'Submit' }));

  expect(onSubmit).toHaveBeenCalledWith({
    1: { q: 'q1', a: 'a1' },
    2: { q: 'q2', a: 'a2' },
  });
});

You can find the source of QuestionsBoard component and this example here.

API / Usage

React Native Testing Library consists of following APIs:

Migration Guides

Troubleshooting

Community Resources

Check out our list of Community Resources about RNTL.

Made with ❤️ at Callstack

React Native Testing Library is an open source project and will always remain free to use. If you think it's cool, please star it 🌟. Callstack is a group of React and React Native geeks, contact us at hello@callstack.com if you need any help with these or just want to say hi!

Like the project? ⚛️ Join the team who does amazing stuff for clients and drives React Native Open Source! 🔥


Supported and used by Rally Health.

Extension points exported contracts — how you extend this code

PackageJson (Interface)
(no doc)
codemods/v14-update-deps/scripts/codemod.ts
CodemodOptions (Interface)
(no doc)
codemods/v14-async-functions/scripts/codemod.ts
RenderOptions (Interface)
(no doc)
src/render.tsx
Point (Interface)
(no doc)
src/types.ts
Screen (Interface)
(no doc)
src/screen.ts
EventEntry (Interface)
(no doc)
src/test-utils/events.ts
FindAllOptions (Interface)
(no doc)
src/helpers/find-all.ts
UserEventSetupOptions (Interface)
(no doc)
src/user-event/setup/setup.ts

Core symbols most depended-on inside this repo

toBeOnTheScreen
called by 211
src/matchers/types.ts
logEvent
called by 187
src/test-utils/events.ts
render
called by 134
src/render.tsx
toHaveAccessibilityValue
called by 49
src/matchers/types.ts
createEventLogger
called by 47
src/test-utils/events.ts
nativeEventLogger
called by 47
experiments-app/src/utils/helpers.ts
getEventsNames
called by 44
src/test-utils/events.ts
act
called by 40
src/act.ts

Shape

Function 444
Method 43
Interface 33
Class 20

Languages

TypeScript100%

Modules by API surface

codemods/v14-async-functions/scripts/codemod.ts22 symbols
src/matchers/types.ts20 symbols
src/helpers/accessibility.ts16 symbols
codemods/v14-update-deps/scripts/codemod.ts13 symbols
src/render.tsx11 symbols
src/__tests__/render.test.tsx11 symbols
src/user-event/press/press.ts10 symbols
src/user-event/setup/setup.ts9 symbols
src/queries/make-queries.ts9 symbols
src/queries/__tests__/text.test.tsx9 symbols
src/__tests__/render-hook.test.tsx9 symbols
src/wait-for.ts8 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

@babel/cli7.29.7 · 1×
@babel/core7.29.7 · 1×
@babel/plugin-transform-strict-mode7.29.7 · 1×
@babel/preset-env7.29.7 · 1×
@babel/preset-react7.29.7 · 1×
@babel/preset-typescript7.29.7 · 1×
@callstack/eslint-config15.0.0 · 1×
@callstack/rspress-theme0.6.3 · 1×
@codemod.com/jssg-types1.3.0 · 1×
@eslint/compat2.1.0 · 1×
@eslint/js10.0.1 · 1×
@expo/metro-runtime56.0.13 · 1×

For agents

$ claude mcp add react-native-testing-library \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact