I'm attempting to use jest (v20.0.0
) w/ my React Native application (v0.42.0
) however when I run yarn jest
I get the following error:
yarn jest v0.27.5
$ jest
FAIL __tests__/routing/router-test.js
● Test suite failed to run
Cannot find module 'ReactNative' from 'react-native.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native.js:188:25)
Here is the jest
portion of my package.json
"jest": {
"testPathIgnorePatterns": [
"/node_modules/"
],
"transformIgnorePatterns": [
"node_modules/(?!react-native|react-native-geocoding)/"
],
"globals": {
"__DEV__": false
},
"collectCoverage": false
},
Here's the failing test file (I stripped out everything except the import
and the error persists).
import 'react-native';
import React from 'react';
describe('Router', () => {
});
To solve the "Cannot find module react or its corresponding type declarations" error, install the module and its type definitions by running the commands npm install react and npm i --save-dev @types/react . Copied! Now you should be able to import the react library with the following line of code.
Your Jest configuration is missing React Native preset:
"jest": {
"preset": "react-native"
}
It's available by default in this form since [email protected]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With