You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/testing.md
+27-5
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,26 @@ When writing tests, it's encouraged to write tests that closely resemble how use
18
18
19
19
Following these principles will help you write tests that are more reliable and easier to maintain by avoiding testing implementation details.
20
20
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`:
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`:
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.
0 commit comments