Skip to main content
Version: 8.x

Writing tests

React Navigation components can be tested in a similar way to other React components. This guide will cover how to write tests for components using React Navigation using Jest.

Guiding principles

When writing tests, it's encouraged to write tests that closely resemble how users interact with your app. Keeping this in mind, here are some guiding principles to follow:

  • Test the result, not the action: Instead of checking if a specific navigation action was called, check if the expected components are rendered after navigation.
  • Avoid mocking React Navigation: Mocking React Navigation components can lead to tests that don't match the actual logic. Instead, use a real navigator in your tests.

Following these principles will help you write tests that are more reliable and easier to maintain by avoiding testing implementation details.

Setting up Jest

Compiling React Navigation

React Navigation ships ES modules. However, Jest does not support ES modules natively.

It's necessary to transform the code to CommonJS to use them in tests. The react-native preset for Jest does not transform the code in node_modules by default. To enable this, you need to add the transformIgnorePatterns option in your Jest configuration where you can specify a regexp pattern. To compile React Navigation packages, you can add @react-navigation