jest-dom: Property 'toBeInTheDocument' does not exist on type 'JestMatchers'
@testing-library/jest-domversion: 6.1.4nodeversion: 18.17.1jestversion: 29.7.0npmversion: 10.2.0
Other dependencies:
Relevant code or config:
import { render, screen } from "@testing-library/react";
import Home from "@/app/page";
it("should have Docs text", () => {
render(<Home />);
const myElement = screen.getByText("Docs");
expect(myElement).toBeInTheDocument();
});
What you did:
I was following a Next.js Testing tutorial by Dave Gray to learn more about React Testing. I followed the tutorial until the 17-minute mark where the error occurred.
What happened:
This is the error:
Reproduction:
The steps from the beginning until the 17th-minute mark of the tutorial will give the error.
Problem description:
Property ‘toBeInTheDocument’ does not exist on type ‘JestMatchers<HTMLElement>’. It seems that it has something to do either with TypeScript, or the Nextjs 14, or with the latest release of testing-library/jest-dom
Suggested solution:
The only solution, which seems temporary, is to roll back to an older version like 5.16.5 or 5.17. This solves the problem.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 24
- Comments: 47
Commits related to this issue
- Docs: Add App Router Testing Guides and update /examples (#59268) This PR updates the testing guides to use App Router and TypeScript, also updates `/examples` to show `app` and `pages` examples. ... — committed to vercel/next.js by delbaoliveira 3 years ago
- Docs: Add App Router Testing Guides and update /examples (#59268) This PR updates the testing guides to use App Router and TypeScript, also updates `/examples` to show `app` and `pages` examples. ... — committed to agustints/next.js by delbaoliveira 3 years ago
- ✅ test(ts): fix @testing-library/jest-dom type definitions not showing up see testing-library/jest-dom#546 — committed to m-tartari/m-tartari by m-tartari 3 years ago
- ✅ test(ts): fix @testing-library/jest-dom type definitions not showing up see testing-library/jest-dom#546 — committed to m-tartari/m-tartari by m-tartari 3 years ago
- ✅ test(ts): fix @testing-library/jest-dom type definitions not showing up see testing-library/jest-dom#546 — committed to m-tartari/m-tartari by m-tartari 3 years ago
- Introduced JEST test framework with eg test Docs that helped with introducing jest into nextjs https://nextjs.org/docs/app/building-your-application/testing/jest https://github.com/vercel/next.js/tre... — committed to AA25/hotel-app by AA25 2 years ago
- Feat: Adds jest-axe and Card component test (#2440) ## What's the purpose of this pull request? - Setup `@testing-library/jest-dom` to help to test our components when checking an element's attri... — committed to vtex/faststore by hellofanny 2 years ago
I’ve come across the issue you’re experiencing and I believe I have a solution that might help you.
If you add the following line to your
tsconfig.jsonundercompilerOptions:This will inform TypeScript to include the type definitions from @testing-library/jest-dom, which should resolve the type errors you’re seeing.
For me the following seems to be solving the issue:
setupAfterEnv.tsUpdate:
Eh maybe it is flaky because after a couple of TS server restarts now it does not work … Anyway maybe this is not the correct solution then.
import '@testing-library/jest-dom';is the only thing working for me, but I have to do it in every test file 👎Adding
compilerOptions: { "types": ["@testing-library/jest-dom"] }intsconfig.jsondidn’t work for me because it overrides my existing"typeRoots".The only solution I’ve found is adding
import "@testing-library/jest-dom";to every test file, which is a bummer.Update: I was able to resolve this!
If, like me, you are configuring
typeRootsintsconfig.jsonand the above solution using thetypesproperty doesn’t work for you, you can instead updatetypeRoots:in my case replacing import with require helped (all other fixes suggested above were already implemented and didn’t help)
const { expect, describe, it } = require('@jest/globals');The comment above by @Noyabronok has pointed me in the right direction. Thanks.
I had
"src/**/*.test.tsx"in the"exclude"array in my tsconfig.json for some reason 🤦 .Removing this seems to have resolved the issue in VS Code for me now.
Woohoo, I got the
compilerOptions: { "types": ["@testing-library/jest-dom"] }intsconfig.jsonchange working!tsconfig file needed the following change:
from:
to:
Appears that only files at the root or
srcdirectory were having tsconfig changes applied.This took care of VSCode error. Unfortunately I still needed
import '@testing-library/jest-dom'in myjest-setup.tsxfile to remove the error from test runssince this is one of the main google hits, I thought I’d share what the solution is. There are two problems: 1. jest-dom 5.x doesn’t have TS types and 2. expect needs to be extended.
First, we’ll uninstall jest-dom and install a 4.x version:
You can use the analogous npm commands instead of yarn.
Then, add this to
src/setupTests.ts:Doing these two things alone worked for me.
BTW, I stumbled upon this while following a tutorial that was based off of create-react-app, using the command
yarn create react-app myname --template typescript.// @ts-ignore 就好了
That works for me
Create/update
jest.setup.ts:Update
jest.config.ts:Add type definitions in
tsconfig.json:Thanks bro it worked, none of the solutions were working
Better is just point ts compiler to
setupTests.tsfile with import to@testing-library/jest-domby adding it to tsconfig.jsonincludeoption.checkout this comment https://github.com/nrwl/nx/issues/9140#issuecomment-1200073153 btw there is no need to install
@types/testing-library__jest-domin first stepAs this is still alive at this date i have to say what just worked for me right now. I updated @testing-library/jest-dom to the latest and in the jest setup file instead of importing it as
import '@testing-library/jest-dom/extend-expect';, I imported it now as thisimport '@testing-library/jest-dom/jest-globals';The way i use the jest functions in typescript is doing explicit imports like
import { describe, expect, test } from '@jest/globals';from the also required package@jest/globalshttps://jestjs.io/docs/getting-started#type-definitions https://github.com/testing-library/jest-dom/releases/tag/v6.0.0
i couldn’t get it working with
@types/jestbut i dont care about it, i rather use@jest/globals.Looks like
cypressis also adding someexpect()interfaces causing problems with the jest related types.Adding direct expect import to the test files fixed it for me:
import { expect } from '@jest/globals';@testing-library/js-domversion, somer version don’t contain ts file, eg. 5.16.4. If not, please update@testing-library/js-domversion to 6.x."types": [ "jest", "@testing-library/jest-dom"]The only solution that as work with me (I’m using Nextjs 14.0.3) is to modify the
tsconfig.jsonand add:"types": ["@testing-library/jest-tocompilerOptionsI have tried adding the
import '@testing-library/jest-dom/jest-globals';to thejest.setup.tsfile and adding the types to thetsconfig.jsonfile as detailed above, but I still get the type errors showing in VS Code.Strangely, when I open the
jest.setup.tsfile in VS Code, the type errors suddenly disappear and everything works as expected? Am I missing a step from my configuration?In my case the error was “Property ‘toBeInTheDocument’ does not exist on type ‘Assertion’”. The reason for the error was I was using also cypress and what fixed the error was to put this in tsconfig file:
"exclude": ["node_modules", "./cypress", "./cypress.config.ts"]this worked!
After hours of debugging, this is what finally made the trick for me. Thanks!
pnpm install --save-dev "@types/jest"i just do install types library it work for me not any config file editI also had this issue with cypress and jest, I also paste this in my jest.setup.ts:
import “@testing-library/jest-dom”; import “@testing-library/jest-dom/jest-globals”;
and it solve it!
In my project I use vitest and cypress, so cypress started overwriting the vitest types. To solve this problem, I added it to my tsconfig.json:
... "exclude": ["node_modules", "./cypress.config.ts", "cypress"] ...See more: https://stackoverflow.com/questions/58999086/cypress-causing-type-errors-in-jest-assertions
For me, the changes to
tsconfig.jsondidn’t fix the issue, but addingimport '@testing-library/jest-dom/jest-globals'worked right away.Here is my
tsconfig.json:Here is my
jest.setup.ts:I just realized looking at my
expecttype signature that my global Cypress types overrode my Jest types. Removing"cypress"from the"types"fixed it.I just downgraded from jest-dom 6.x to 5.x and everything is green again.
Underrated solution hidden away by collapsed thread view
Thanks a lot
I’ve always found using the
typesconfig option in tsconfig.json problematic. Adding this to mysetupTests.js(or whatever equivalent you have) also works, without introducing the additional issues with setting thetypesoption:This seems to have resolved the issue:
import '@testing-library/jest-dom/jest-globals'tojest.setup.ts.setupFilesAfterEnv: ['<rootDir>/tests/jest.setup.ts'],tojest.config.ts."types": ["@testing-library/jest-dom/jest-globals"]totsconfig.ts.or adding
import "@testing-library/jest-dom"to every test file.@khairalanam I was taking the same course with Dave Gray and encountered the same issue. Initially, I had errors with
itandexpectwhich were also shown in the video. I resolved these errors by runningnpm i -D @types/jestAfter addressing this, I faced the exact same error as you did, and resolved it by removingextend-expectfrom the import statement@testing-library/jest-dom/extend-expectinjest.setup.tsfile.The reason is that
@testing-library/jest-domno longer requires a separate import of@testing-library/jest-dom/extend-expectas it might have in earlier versions. Therefore, if you are usingversion 6.0 or higher, you should import@testing-library/jest-domdirectly, without including theextend-expectpath.So I have this import inside of my
setupTests.ts:import "@testing-library/jest-dom";And when I have the setupTests.ts files actually open in my IDE, it’s all good. No complaints. Now when I close the setupTests.ts file, my IDE starts complaining that
inTheDocument()doesn’t exist on JestMatchers… And then when I open setupTests.ts again, it’s all good and dandy. Anybody else had this problem?That’s pretty good. It has to be added to the config file after installing the library.