When running yarn test
in package.json
which is
CI=true react-app-rewired test --coverage
I got following error
Test suite failed to run
Cannot find module 'react' from 'pure.js'
However, Jest was able to find:
'./pure.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (../../node_modules/@testing-library/react/dist/pure.js:28:37)
Not sure why getting react
from pure.js
, any idea?
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.
To solve the error "Module not found: Error: Can't resolve 'react-dom'", make sure to install the react-dom package by opening your terminal in your project's root directory and running the command npm install react-dom react and restart your development server.
To solve the error "Cannot find module 'react/jsx-runtime' or its corresponding type declarations", make sure to install the typings for react running the command npm install --save-dev @types/react@latest @types/react-dom@latest and restart your dev server.
The error "could not find declaration file for module 'react'" occurs when TypeScript cannot find the type declaration for a react-related module. To solve the error install the types for the module by running the command from the error message, e.g. npm install -D @types/react .
I was also facing the same issue, then I installed "react-test-renderer": "^16.13.1"
and the issue is resolved.
So you can try this also just installed this dependency by command npm install react-test-renderer --save
.
As you can see in the below image:
Jest is unable to find 'react'
because it is either not installed or the closest package.json
does not have react as a dependency (if it's a monorepo).
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