react-testing-library: Cannot find module '@testing-library/react'.ts(2307)
react-testing-libraryversion: >= 9.1.2-alpha.0reactversion: 16.8.4nodeversion: unknownnpm(oryarn) version: unknown
Relevant code or config:
import { render } from "@testing-library/react";
What you did:
Import library
What happened:
Typescript error: Cannot find module '@testing-library/react'.ts(2307)
Reproduction:
https://codesandbox.io/s/mystifying-lamport-wytox
Problem description:
Tests run just fine, but no types, and typescript error
Suggested solution:
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 17 (3 by maintainers)
Sometimes after running
npm installVSCode for some reason doesn’t pick the changes! So in my case it was the “old” reastart trick that did it… I just restarted VSCode and it disappeared 😉Solution:
npm install --save-dev @testing-library/reactchange from “moduleResolution”: “node”, to “moduleResolution”: “NodeNext”, Wich available in tsconfig.json
I solved this issue with this option in the tsconfig.json file:
"moduleResolution": "node",Sorry, that codesandbox link takes me to a 404 page. A lot of people are using this library and it’s working fine, so I’m guessing your issues are something specific to your project. If you’re still having trouble please start a thread on http://spectrum.chat/testing-library/help-react
Ran into a similar issue on a project, solved with a force reinstall
yarn install --forcerestart work for me.
@soiswis the good old restart worked for me. Thanks.
Hi Ronny
Many thanks for your reply.
I already had @types/jest installed
However, I have since worked out that it also needs to be added to setupTests.ts, below the @testing-library/jest-dom
import @./jest-dom’; import @./jest’
With that in place the error I had before went away.
It also cured the toBeInTheDocument error
const linkElement = screen.getByText(/learn react/i); expect(linkElement).toBeInTheDocument();
Regards
Paul Simon
From: Ronny Cardona @.> Sent: Monday, September 11, 2023 9:35 PM To: testing-library/react-testing-library @.> Cc: Paul SImon @.>; Mention @.> Subject: Re: [testing-library/react-testing-library] Cannot find module @.***/react’.ts(2307) (#472)
@paulsimongbhttps://github.com/paulsimongb if you upgraded to v6 AND you’re importing import @.***/jest-dom’, then you’ll have to install @types/jest. Please check the release notes on v6https://github.com/testing-library/jest-dom/releases/tag/v6.0.0.
— Reply to this email directly, view it on GitHubhttps://github.com/testing-library/react-testing-library/issues/472#issuecomment-1714540280, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AGHWSVSVHSHILHMS3FBEGRLXZ5YW5ANCNFSM4ISQVJHA. You are receiving this because you were mentioned.Message ID: @.@.>>
Check if the name of your file is with .tsx extension or you have missed the x
Upgrading to “@types/react”: “^16.8.18” with npm i @types/react@latest fixed it.