Fix React Testing Library 16 install errors
Declare the DOM peer dependency to fix React Testing Library 16’s missing module error.
You bump @testing-library/react to version 16 in package.json, run your tests, and every test file that imports it fails before a single test runs. The message names a package you never installed.
FAIL src/hello.test.jsx [ src/hello.test.jsx ]
Error: Cannot find module '@testing-library/dom'
Require stack:
- .../node_modules/@testing-library/react/dist/pure.jsThe stack points at React Testing Library because it imports @testing-library/dom. Check whether your project installed that dependency.
What changed in 16
Version 16 changed who installs @testing-library/dom. The 16.0.0 release notes record the move from a direct dependency to a peer dependency.
A peer dependency is one your project has to provide. Some package manager setups install peers for you, and then you never see the error.
I set pnpm’s autoInstallPeers option to false to reproduce the missing package. The import then failed in the first test file.
In that project the install printed only Issues with peer dependencies found. The warning is easy to miss, and the failure shows up later in the test run.
Install it yourself
Add the package next to React Testing Library:
pnpm add -D @testing-library/domWith npm the command is npm install --save-dev @testing-library/dom. The Testing Library install guide lists both packages: @testing-library/react and @testing-library/dom.
Run the tests again, or reinstall with your usual command first if you edited package.json by hand. In my reproduction the same test file went from a failed suite with no tests to 1 passed.
If you use TypeScript, install compatible @types/react and @types/react-dom packages too.
Which version you get
React Testing Library declares the range it accepts in its peerDependencies field. In the 16.x copy I installed, that field asks for @testing-library/dom at ^10.0.0, so the plain pnpm add -D command gave me a 10.x release.
You can read the range yourself in node_modules/@testing-library/react/package.json. If you pin a version, select one that satisfies the declared range.
Both packages belong in devDependencies. Your app never imports either one.
After the install, git diff on package.json shows @testing-library/dom added under devDependencies. Commit it with the version bump so the next person who clones the repo does not meet the error either.