Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

jest-dom extend-expect import fails after upgrading to v6

ErrorCannot 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.

  • Applies to Jest 30, @testing-library/jest-dom 6.x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.json
  2. 2Match what you see, then jump to its fix

    1. 1import '@testing-library/jest-dom/extend-expect' and jest-dom at version 6 or newerThe setup file still uses the removed extend-expect pathFix
    2. 2You 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

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-dom

2Fix

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 jest

Confirmed by github.com

Did this fix it?

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.json

2Fix

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 jest

Confirmed by github.com

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.