jest-dom extend-expect import fails after upgrading to v6
Cannot find module '@testing-library/jest-dom/extend-expect' from 'jest.setup.js'Short answer
Version 6 of @testing-library/jest-dom removed the extend-expect script. A setup file that still imports it cannot resolve the module. Import @testing-library/jest-dom instead for Jest, the jest-globals path if you import from @jest/globals, or the vitest path for Vitest, and register the file in setupFilesAfterEnv.
Fix it fast
1Find out which cause you have
See which version is installed and which line in the setup file triggers the error.
npm ls @testing-library/jest-dom grep -rn "jest-dom" jest.setup.* jest.config.* package.json2Match what you see, then jump to its fix
- 1
import '@testing-library/jest-dom/extend-expect' and jest-dom at version 6 or newerThe setup file still uses the removed extend-expect pathFix - 2
You use @jest/globals with injectGlobals: false, or Vitest, and the root import does not extend your expectThe test runner is not Jest with global expectFix
- 1
What does the error look like?
Cannot find module '@testing-library/jest-dom/extend-expect' from 'jest.setup.js'
Cause 1 of 2
The setup file still uses the removed extend-expect path
1Check
The v6.0.0 release lists the removal of the extend-expect script as a breaking change.
npm ls @testing-library/jest-dom2Fix
Change the import to the package root, which extends Jest's expect automatically.
// jest.setup.js
- import '@testing-library/jest-dom/extend-expect';
+ import '@testing-library/jest-dom';3Verify
Run one test that uses a jest-dom matcher such as toBeInTheDocument.
npx jestConfirmed by github.com
Cause 2 of 2
The test runner is not Jest with global expect
1Check
The release names one import per runner: the package root for Jest with @types/jest, jest-globals for @jest/globals, and vitest for Vitest.
grep -n "injectGlobals\|vitest" jest.config.* vite.config.* package.json2Fix
Use the import that matches your runner, and list the setup file in the runner's config (setupFilesAfterEnv for Jest, setupFiles for Vitest).
// Jest with injectGlobals: false
import '@testing-library/jest-dom/jest-globals';
// Vitest
import '@testing-library/jest-dom/vitest';
// jest.config.js
module.exports = { setupFilesAfterEnv: ['<rootDir>/jest.setup.js'] };3Verify
Run the test suite and confirm matchers are available.
npx jestConfirmed by github.com
How do I stop it coming back?
- Read the release notes when a testing-library major version changes.
- Keep jest-dom imports in one setup file, not scattered across tests.
- If you write TypeScript, name the setup file .ts and include it in tsconfig.json as the jest-dom README says.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
TypeScript cannot find toBeInTheDocument on expect()
TS2339 on expect(...).toBeInTheDocument() while the test itself runs
The jest-dom matchers and their types only exist after the package is imported once in a test setup file.
4 causes, each with a fixWeb apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fix