-
Notifications
You must be signed in to change notification settings - Fork 404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Property 'toBeInTheDocument' does not exist on type 'JestMatchers<HTMLElement>' #546
Comments
For me the following seems to be solving the issue:
Update: 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. |
That's pretty good. It has to be added to the config file after installing the library. |
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
This will inform TypeScript to include the type definitions from @testing-library/jest-dom, which should resolve the type errors you're seeing. |
This too seems solid but much cleaner. Nice! |
in my case replacing import with require helped (all other fixes suggested above were already implemented and didn't help) |
|
Thanks bro it worked, none of the solutions were working |
I have tried adding the Strangely, when I open the |
|
The only solution that as work with me (I'm using Nextjs 14.0.3) is to modify the |
Woohoo, I got the tsconfig file needed the following change: from:
to:
Appears that only files at the root or This took care of VSCode error. Unfortunately I still needed |
The comment above by @Noyabronok has pointed me in the right direction. Thanks. I had Removing this seems to have resolved the issue in VS Code for me now. |
This PR updates the testing guides to use App Router and TypeScript, also updates `/examples` to show `app` and `pages` examples. ## Overview - [x] Create a new "Testing" section that is shared between `app` and `pages`. - [x] Explain the differences between E2E, unit testing, component testing, etc. - [x] Recommend E2E for `async` components as currently none of the tools support it. - [x] Update setup guides for **Cypress**, **Playwright**, and **Jest** with latest config options, and examples for `app` and `pages`. - [x] Add new guide for **Vitest** - [x] Clean up `/examples`: use TS, show `app` and `pages` examples, match docs config ## Cypress - [x] E2E Tests - [x] Component Testing - [x] Client Components - [x] Server Components - [ ] `async` components **Blockers:** - TS: `Option 'bundler' can only be used when 'module' is set to 'es2015' or later`. In **tsconfig.json** compilerOptions, Next.js uses "moduleResolution": "bundler", changing it to "node" fixes the issue but it can have repercussions. - cypress-io/cypress#27731 - Version 14 is currently not supported for component testing - cypress-io/cypress#28185 ## Playwright - [x] E2E Tests ## Jest - [x] Unit Testing - [x] Client Components - [x] Server Components - [ ] `async` components: testing-library/react-testing-library#1209 - [x] 'server-only': #54891 - [x] Snapshot Testing **Blockers:** - TS: testing-library/jest-dom#546 - None of the solutions in the issue work with Next.js v14.0.4 and TS v5 ## Vitest - [x] Unit Testing - [x] Client Components - [x] Server Components - [ ] `async` components - [x] 'server-only' - [x] Update vitest example - [x] Handles CSS, and CSS modules imports - [x] Handles next/image ## Other - #47448 - #47299
This PR updates the testing guides to use App Router and TypeScript, also updates `/examples` to show `app` and `pages` examples. ## Overview - [x] Create a new "Testing" section that is shared between `app` and `pages`. - [x] Explain the differences between E2E, unit testing, component testing, etc. - [x] Recommend E2E for `async` components as currently none of the tools support it. - [x] Update setup guides for **Cypress**, **Playwright**, and **Jest** with latest config options, and examples for `app` and `pages`. - [x] Add new guide for **Vitest** - [x] Clean up `/examples`: use TS, show `app` and `pages` examples, match docs config ## Cypress - [x] E2E Tests - [x] Component Testing - [x] Client Components - [x] Server Components - [ ] `async` components **Blockers:** - TS: `Option 'bundler' can only be used when 'module' is set to 'es2015' or later`. In **tsconfig.json** compilerOptions, Next.js uses "moduleResolution": "bundler", changing it to "node" fixes the issue but it can have repercussions. - cypress-io/cypress#27731 - Version 14 is currently not supported for component testing - cypress-io/cypress#28185 ## Playwright - [x] E2E Tests ## Jest - [x] Unit Testing - [x] Client Components - [x] Server Components - [ ] `async` components: testing-library/react-testing-library#1209 - [x] 'server-only': vercel#54891 - [x] Snapshot Testing **Blockers:** - TS: testing-library/jest-dom#546 - None of the solutions in the issue work with Next.js v14.0.4 and TS v5 ## Vitest - [x] Unit Testing - [x] Client Components - [x] Server Components - [ ] `async` components - [x] 'server-only' - [x] Update vitest example - [x] Handles CSS, and CSS modules imports - [x] Handles next/image ## Other - vercel#47448 - vercel#47299
Adding The only solution I've found is adding Update: I was able to resolve this! If, like me, you are configuring "typeRoots": ["node_modules/@types", "node_modules/@testing-library"] |
Better is just point ts compiler to checkout this comment nrwl/nx#9140 (comment) btw there is no need to install |
So I have this import inside of my
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 |
since 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
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 |
// @ts-ignore 就好了 |
I just downgraded from jest-dom 6.x to 5.x and everything is green again. |
That works for me
|
As this is still alive at this date i have to say what just worked for me right now. The way i use the jest functions in typescript is doing explicit imports like https://jestjs.io/docs/getting-started#type-definitions i couldn't get it working with |
After hours of debugging, this is what finally made the trick for me. Thanks! |
@khairalanam I was taking the same course with Dave Gray and encountered the same issue. Initially, I had errors with The reason is that |
I tried every solution from this thread, but only one solved the problem renaming p.s. |
this working for me :D |
I just realized looking at my |
This seems to have resolved the issue:
or adding |
For me, the changes to Here is my {
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": "src",
"downlevelIteration": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"typeRoots": ["./node_modules/@types"],
"paths": {
"ui-component": ["components/ui-component"],
"ui-component/*": ["components/ui-component/*"],
"components": ["components"]
},
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "jest.setup.ts"],
"exclude": ["node_modules"]
} Here is my import '@testing-library/jest-dom/jest-globals';
import '@testing-library/jest-dom'; |
Looks like Adding direct expect import to the test files fixed it for me:
|
this worked! |
Docs that helped with introducing jest into nextjs https://nextjs.org/docs/app/building-your-application/testing/jest https://github.com/vercel/next.js/tree/canary/examples/with-jest Docs that helped with issues testing-library/jest-dom#546
|
## What's the purpose of this pull request? - Setup `@testing-library/jest-dom` to help to test our components when checking an element's attributes, its text content, its css classes... - Adds `Card` component tests - Introduces accessibility tests: This won't guarantee that what we're building is accessible, but it's a first step towards it. 🥳 - Updates `CheckboxField` component test TODO: Document ## How it works? Currently, our components library do not have tests, but we want to introduce them for upcoming components. For existing components, we will gradually add tests retroactively. ## How to test it? 1. navigate to `packages/components` -> run `yarn install` 2. run `yarn test`, you will be able to see the tests running <img width="873" alt="image" src="https://github.com/user-attachments/assets/1e4c0fcc-4ded-45c5-a078-0fad99c52dba"> _____ 3. removing `<Label>` component from `CheckboxField`, when running the step 2, you will get: <img width="1005" alt="image" src="https://github.com/user-attachments/assets/898360ba-2a9a-40d5-afd4-a6c0f74d11a4"> ## References https://testing-library.com/docs/ecosystem-jest-dom testing-library/jest-dom#546 NickColley/jest-axe#79
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: See more: https://stackoverflow.com/questions/58999086/cypress-causing-type-errors-in-jest-assertions |
I've always found using the /// <reference types="@testing-library/jest-dom" /> |
This fixed my problem.
You may read about it in the Jest docs |
I fix this in the next way: tsconfig.json i had:
i changed to:
And this change fix my problem. tsconfig.json working file:
In my jest.setup.ts file, i had this line:
Example on working test with my solution:
|
Just found out that adding import { type InputHTMLAttributes } from 'react';
interface Props extends InputHTMLAttributes<HTMLInputElement> {
label: string
}
const MyInput = ({ label, ...props }: Props) => {
return (
<div>
<label>{label}</label>
<input {...props} />
</div>
);
} Also using |
Hi there ) and just to add |
I had to add that to the test file, not the setup file. For someone stuck with this. Concise article |
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:
|
@testing-library/jest-dom
version: 6.1.4node
version: 18.17.1jest
version: 29.7.0npm
version: 10.2.0Other dependencies:
Relevant code or config:
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'. 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.
The text was updated successfully, but these errors were encountered: