After upgrading @testing-library/jest-dom from v5 to v6, TypeScript can no longer find its types. I’m getting this error when running tsc --noEmit --skipLibCheck
:
error TS2688: Cannot find type definition file for '@testing-library/jest-dom'.
My tsconfig.json
includes "types": ["jest", "@testing-library/jest-dom"]
, and the package is listed in devDependencies
.
This setup used to work fine with v5, but after the upgrade to v6, it fails unless I manually import @testing-library/jest-dom
in my test files.
I’d like to understand why this change occurred and how to configure TypeScript to load jest dom types globally again, without needing manual imports in every test file.
Is there a new setup required in v6 to make TypeScript aware of jest-dom
types?
I ran into the exact same issue after upgrading to v6. Turns out, @testing-library/jest-dom v6
no longer ships with ambient type declarations, they switched to using ESM exports only.
That’s why TypeScript can’t pick it up globally just from “types” in tsconfig.
What worked for me was creating a setupTests.ts (or .d.ts) file and explicitly importing it:
import '@testing-library/jest-dom';
Then I referenced this file in my tsconfig.json under “include” so it’s always part of the compilation.
That way, TypeScript sees the types project-wide again without having to import them in every test.
I faced this recently while updating our test setup too. The fix that worked on my end was removing @testing-library/jest-dom
from the “types” array in tsconfig.json completely.
It sounds counterintuitive, but because v6 exports types differently, listing it in “types” causes TypeScript to look for a non-existent @types
package, which throws that TS2688 error.
Instead, I added a custom global type declaration file (jest-dom.d.ts) in my types folder with:
import '@testing-library/jest-dom';
Then added the folder to “include” in tsconfig. That gave me global access to all the matchers again, and the error went away.
I bumped into this after upgrading across multiple projects. The key change with v6 is that jest-dom
no longer exposes types the old global way, so TypeScript won’t see them unless you explicitly import them somewhere.
My solution: I created a jest.setup.ts file that already contained other setup logic, and just added:
import '@testing-library/jest-dom';
Then I updated my vitest.config.ts (we use Vitest, but similar idea works for Jest) to include that setup file. This kept the import in one place and made the types magically available again across all test files, without cluttering each one with imports.