jest-dom: Property 'toBeChecked' does not exist on type 'Matchers'
@testing-library/jest-domversion: 5.16.2nodeversion: v14.17.4npm(oryarn) version: 6.14.14
dom-testing-libraryversion: (if applicable)react-testing-libraryversion: 12.1.3
Relevant code or config:
expect(document.querySelector('input')).toBeChecked()
What you did:
Trying to test if an input is checked
What happened:
Typescript shows the error Property 'toBeChecked' does not exist on type 'Matchers<void, Element>' although the test runs correctly.
What I have tried:
- I have tried
import "jest-dom/extend-expect";and `import ‘@testing-library/jest-dom/extend-expect’; as recommended here - Adding
["jest-styled-components", "@types/testing-library__jest-dom"]to the types intsconfig - Adding
import '@testing-library/jest-dom/extend-expect';tosrc/setupTests.ts - Replacing
document.querySelector('input')withdocument.querySelector('input')!ordocument.querySelector('input') as HTMLInputElement
No matter what I do, I always get the same or a similar issue
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 26
- Comments: 28 (1 by maintainers)
npm install --save-dev @types/testing-library__jest-domsolved my problemTwo things fixed my issue:
@types/testing-library__jest-domimport '@testing-library/jest-dom/extend-expect';to my jest setup fileNote: either of those solutions worked on their own. I did not need to combine them.
@gnapse I also got this error when using the
toBeInTheDocumentmatcher.@testing-library/angularversion 10.11.1@testing-library/jest-domversion 5.16.4nodeversion 14.19.3npmversion 6.14.17For me, the problem got resolved with
yarn add -D @testing-library/dom. I don’t know why, maybe because we are using Yarn 3 PnP.Setup now is:
None of the tips above helped me in 2024. This below ended up working:
Updating
@testing-library/jest-domfrom 5 to^6.2.0I tried adding
import "@testing-library/jest-dom/extend-expectin jest setup file but it didn’t work for me. Below steps worked for me.setupFilesAfterEnv: [ "./jest-setup.ts" ], moduleFileExtensions: [ "js", "mjs", "cjs", "jsx", "ts", "tsx", "d.ts", // "json", // "node" ]"include": [ "./jest-setup.ts"],module.exports = { presets: [ '@babel/preset-typescript', '@babel/preset-env', '@babel/preset-react', ] };This one should be the solution: https://github.com/pnpm/pnpm/issues/4920#issuecomment-1226724790
I’ve fixed it including in the jest-setup.ts also the react package (I’m using yarn 3)
// jest.setup.ts import '@testing-library/jest-dom'; import '@testing-library/react';and adding the types @types/testing-library__jest-dom in the tsconfig
"compilerOptions": { ... "types": ["jest", "node", "@types/jest", "@types/testing-library__jest-dom"], }but I’m not sure if it’s the right approachFor me I tried adding the following imports to my jest setup file, didnot work
Even adding “@types/testing-library__jest-dom” to types in tsconfig didnot help.
But just importing the following in a single test file
import '@testing-library/jest-dom'fixes it in all test files.
I do not know how this works, but I have a file types/window.ts and in that I put this in there, and it works. window.ts exports: export interface Window {
If you are using pnpm this might work for you.
I have same error.
error TS2339: Property ‘toBeInTheDocument’ does not exist on type ‘Matchers<void, HTMLElement> & SnapshotMatchers<void, HTMLElement> & Inverse<JestMatchers<void, HTMLElement>> & PromiseMatchers<…>’.
tsconfig.json
jest.config.js
jest-setup.ts
I am also getting similar errors. Have tried pretty much all the things mentioned above. I am using Next JS.
So far I’ve:
import '@testing-library/jest-dom/extend-expect';to my jest.setup.tstypesarray in tsconfig"jest-setup.ts"to theincludearray in tsconfig.But nothing so far is getting rid of the errors. The tests all work fine, so for now I have found the easiest solution for now is just to add
"**/*.test.tsx"the myexcludearray in tsconfig - which is obviously FAR from an serious solution.Here is what a typical test looks like:
and here are my error messages:
If anyone has any tips on the right way to go about fixing this I would appreciate it. I am still new at typescript but lately it fells lik > 50% of my time is spent googling solutions to type errors like this…
LINK TO REPO: https://github.com/PaulHaze/ott-testing
As a follow up to my previous comment, this package is very unstable when using jest-cucumber. Neither of the solutions I previously posted work to allow the matchers to be found consistently. There appears to be some sort of race condition as the matchers are sometimes found. Hopefully this information helps as it would be great to use these matchers, but as it stands this package is unusable in my project.