Skip to content

Commit fa78c92

Browse files
committed
Update jest configuration for React Navigation
1 parent 41b3d34 commit fa78c92

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

Diff for: versioned_docs/version-7.x/testing.md

+27-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,26 @@ When writing tests, it's encouraged to write tests that closely resemble how use
1818

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

21-
## Mocking native dependencies
21+
## Setting up Jest
22+
23+
### Compiling React Navigation
24+
25+
React Navigation ships [ES modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). However, Jest does not support ES modules natively.
26+
27+
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`](https://jestjs.io/docs/configuration#transformignorepatterns-arraystring) option in your Jest configuration where you can specify a regexp pattern. To compile React Navigation packages, you can add `@react-navigation` to the regexp.
28+
29+
This is usually done in a `jest.config.js` file or the `jest` key in `package.json`:
30+
31+
```diff lang=json
32+
{
33+
"preset": "react-native",
34+
+ "transformIgnorePatterns": [
35+
+ "node_modules/(?!(@react-native|react-native|@react-navigation)/)"
36+
+ ]
37+
}
38+
```
39+
40+
### Mocking native dependencies
2241

2342
To be able to test React Navigation components, certain dependencies will need to be mocked depending on which components are being used.
2443

@@ -48,16 +67,19 @@ import { jest } from '@jest/globals';
4867
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
4968
```
5069

51-
Then we need to use this setup file in our jest config. You can add it under `setupFilesAfterEnv` option in a `jest.config.js` file or the `jest` key in `package.json`:
70+
Then we need to use this setup file in our jest config. You can add it under [`setupFilesAfterEnv`](https://jestjs.io/docs/configuration#setupfilesafterenv-array) option in a `jest.config.js` file or the `jest` key in `package.json`:
5271

53-
```json
72+
```diff lang=json
5473
{
5574
"preset": "react-native",
56-
"setupFilesAfterEnv": ["<rootDir>/jest/setup.js"]
75+
"transformIgnorePatterns": [
76+
"node_modules/(?!(@react-native|react-native|@react-navigation)/)"
77+
],
78+
+ "setupFilesAfterEnv": ["<rootDir>/jest/setup.js"]
5779
}
5880
```
5981

60-
Make sure that the path to the file in `setupFilesAfterEnv` is correct. Jest will run these files before running your tests, so it's the best place to put your global mocks.
82+
Jest will run the files specified in `setupFilesAfterEnv` before running your tests, so it's a good place to put your global mocks.
6183

6284
<details>
6385
<summary>Mocking `react-native-screens`</summary>

0 commit comments

Comments
 (0)