Introduction to React Native Testing Library

Rate this content
Bookmark

Are you satisfied with your test suites? If you said no, you’re not alone—most developers aren’t. And testing in React Native is harder than on most platforms. How can you write JavaScript tests when the JS and native code are so intertwined? And what in the world are you supposed to do about that persistent act() warning? Faced with these challenges, some teams are never able to make any progress testing their React Native app, and others end up with tests that don’t seem to help and only take extra time to maintain.


But it doesn’t have to be this way. React Native Testing Library (RNTL) is a great library for component testing, and with the right mental model you can use it to implement tests that are low-cost and high-value. In this three-hour workshop you’ll learn the tools, techniques, and principles you need to implement tests that will help you ship your React Native app with confidence. You’ll walk away with a clear vision for the goal of your component tests and with techniques that will help you address any obstacle that gets in the way of that goal.

you will know:

- The different kinds React Native tests, and where component tests fit in

- A mental model for thinking about the inputs and outputs of the components you test

- Options for selecting text, image, and native code elements to verify and interact with them

- The value of mocks and why they shouldn’t be avoided

- The challenges with asynchrony in RNTL tests and how to handle them

- Options for handling native functions and components in your JavaScript tests


Prerequisites:

- Familiarity with building applications with React Native

- Basic experience writing automated tests with Jest or another unit testing framework

- You do not need any experience with React Native Testing Library

- Machine setup: Node 16.x or 18.x, Yarn, be able to successfully create and run a new Expo app following the instructions on https://docs.expo.dev/get-started/create-a-new-app/

This workshop has been presented at React Advanced Conference 2022, check out the latest edition of this React Conference.

FAQ

React Native Testing Library is a framework that allows you to test React Native components in a way that simulates user interaction as closely as possible, ensuring your tests focus on component behavior rather than implementation details.

You can access the React Native Testing Library repository by visiting the URL provided in the workshop, which is linked to the Git repository containing exercises and resources for learning and practicing component testing in React Native.

Using Jest with React Native Testing Library provides a powerful assertion framework that helps in writing tests that are more descriptive and easier to maintain. Jest also supports mocking and spying, which are essential for testing components in isolation.

React Native Testing Library is primarily focused on testing individual components and their integration. For end-to-end testing in React Native apps, tools like Detox or Appium are more suitable.

To handle asynchronous events, React Native Testing Library provides utilities like 'waitFor' and 'findBy' queries that allow you to wait for certain elements to appear or change state before proceeding with your assertions.

Testing the contract refers to focusing on the expected inputs and outputs of a component, ensuring that it behaves correctly with given inputs and produces expected outputs. This helps in building reliable and maintainable components.

You can mock modules using Jest by using 'jest.mock()' to replace the actual implementation with a mock function or object. This allows you to simulate module behavior and control the inputs and outputs for your tests.

Josh Justice
Josh Justice
131 min
28 Oct, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This workshop on React Native Testing Library covers various topics including testing components, user interaction events, mocking, and testing API calls. The speaker emphasizes the importance of testing the contract of a component and provides practical examples and exercises. Just module mocking is recommended as a portable solution for mocking modules. The workshop encourages exploring different testing strategies and provides additional resources for further learning.
Video transcription and chapters available for users with access.