jest-dom: Property 'toBeChecked' does not exist on type 'Matchers'
@testing-library/jest-dom
version: 5.16.2node
version: v14.17.4npm
(oryarn
) version: 6.14.14
dom-testing-library
version: (if applicable)react-testing-library
version: 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 2 years ago
- Reactions: 25
- Comments: 27 (1 by maintainers)
npm install --save-dev @types/testing-library__jest-dom
solved my problemTwo things fixed my issue:
@types/testing-library__jest-dom
import '@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
toBeInTheDocument
matcher.@testing-library/angular
version 10.11.1@testing-library/jest-dom
version 5.16.4node
version 14.19.3npm
version 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-dom
from 5 to^6.2.0
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 approachThis one should be the solution: https://github.com/pnpm/pnpm/issues/4920#issuecomment-1226724790
I tried adding
import "@testing-library/jest-dom/extend-expect
in 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', ] };
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 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 {
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.