https://callstack.github.io/react-native-testing-library/docs/react-navigation
Setting up
$ yarn add @react-native-community/masked-view @react-navigation/native @react-navigation/stack react-native-gesture-handler react-native-reanimated react-native-safe-area-context react-native-screensSetting up the test environment
Install required dev dependencies:
$ yarn add -D jest @testing-library/react-nativeCreate your jest.config.js file (or place the following properties in your package.json as a "jest" property)
module.exports = {
preset: 'react-native',
setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'],
transformIgnorePatterns: [
'node_modules/(?!(jest-)?react-native|@react-native-community|@react-navigation)',
],
};Notice the 2 entries that don't come with the default React Native project:
setupFiles– an array of files that Jest is going to execute before running your tests. In this case, we runreact-native-gesture-handler/jestSetup.jswhich sets up necessary mocks forreact-native-gesture-handlernative moduletransformIgnorePatterns– an array of paths that Jest ignores when transforming code. In this case, the negative lookahead regular expression is used, to tell Jest to transform (with Babel) every package insidenode_modules/that starts withreact-native,@react-native-communityor@react-navigation(added by us, the rest is inreact-nativepreset by default, so you don't have to worry about it).