-
Notifications
You must be signed in to change notification settings - Fork 405
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
Upgrading to v6.0.0 remove the toBeInTheDocument
interface with vitest
#515
Comments
What version of vitest, and where is the import statement located for @testing-library/jest-dom/vitest? |
fwiw this is happening to me in Jest too. |
ah sorry I thought that I added the vitest version. Vitest -> 0.34.2 I added the import statement in the vitest.setup.ts file (that is pointed from my vite.config.ts like this) /// <reference types="vitest" />
import react from '@vitejs/plugin-react-swc';
import { defineConfig } from 'vite';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['test/vitest.setup.ts'],
include: ['**/?(*.)test.ts?(x)'],
},
}); |
@qchuchu Have you added
/// <reference types="vitest" />
import '@testing-library/jest-dom/vitest`;
{
"compilerOptions": {},
"include": ["src", "test/vitest.setup.ts"]
} |
So it does work by adding the setup file in the tsconfig.json, but only if I stay with the original configuration
import '@testing-library/jest-dom'; If I add /vitest at the end, i'm facing the issue again. |
Hmm, I think that's because you're using Vitest in "globals" mode. There isn't really a difference between what you have now and using |
Getting this error trying to import // setup.ts
import matchers from '@testing-library/jest-dom/vitest'; Error: File '/Users/x/y/node_modules/@testing-library/jest-dom/vitest.d.ts' is not a module.ts(2306) |
is adding the setup file to the tsconfig the only way to get this to work? import {defineVitestConfig} from 'shared-workspace-vitest-setup';
export default defineVitestConfig({
// custom options here that get merged with the default config
// default config contains a setupFile adding dom matchers
}); i don't really want to add something like edit: "compilerOptions": {
"types": ["@testing-library/jest-dom/vitest"]
} or put it in a .d.ts file that's included like src/jest-dom-types.d.ts /// <reference types="@testing-library/jest-dom/vitest.d.ts" /> |
Matchers are not exported from the import * as matchers from '@testing-library/jest-dom/matchers' Or if you want to automatically extend Vitest's expect, do this: import '@testing-library/jest-dom/vitest' |
@dominikg Thanks for pointing out the alternate ways of getting the types recognized by TS. We should probably add that to the readme. |
I can also confirm this is also happening with Jest as well. Don't know how to handle those errors yet :( |
For those using Jest, make sure you are importing like this:
Many people are using an older deprecated |
Thanks @jgoz I can confirm this fixes the issue for me. |
@jgoz Sorry for asking help in this issue, i was checking my setup files and i see that i'm importing almost the same way as the library's documentation: I have a .setup folder that groups some mocks globally, and also the You have any suggestion of what might be causing my errors? When i'm importing on each test, the errors goes away (of course this is a solution, but would be better for me importing this globally in the setup hehe) EDIT: I found one possible solution, in my case, i tried to create e second setup file outside this folder (just in the root dir normally), and mentioned in the jest config file, and it worked! But, following my old setup file, can't it be inside any folder anymore? |
@DeveloperMatheus Is your setup file included in tsconfig.json? Like, in the |
@jgoz It's like this:
It doesn't have the files property EDIT: My setup file is linked in my jest.config.js |
@DeveloperMatheus it might be because your setup file is in a "include": [...],
"files": [".jest/setup.ts"] |
@jgoz Many many many thanks for your help! It worked! |
So while adding
|
import '@testing-library/jest-dom/vitest'; |
This is my current setupTests.ts
I've tried with both I'm starting to think it might be something to do with yarn PnP and WebStorm. I have updated TS, ensured I only import /vitest and my IDE still shows errors. Interestingly the tests still pass despite the error. |
And |
Also using pnpm, I'm getting the type error The tests pass, but still get the typescript error when typechecking. test/setup-test-env.ts:
tsconfig includes tsconfig:
|
Hey guys, I also ran into the same issue but finally got a working setup with v6.1.3, vitest 0.34.6 and pnpm. I have a React project created by Vite. My setup: // src/__tests__/extend-expect.ts
import "@testing-library/jest-dom/vitest"; Remember to add the above to // src/vitest.d.ts
import { type TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
import {
type Assertion,
type AsymmetricMatchersContaining,
type expect,
} from "vitest";
type CustomMatchers<R = unknown> = TestingLibraryMatchers<
ReturnType<typeof expect.stringContaining>,
R
>;
declare module "vitest" {
interface Assertion<T = unknown> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
} For I think the problem is that the types being shipped for |
To make it work if you are using the vitest:
|
Had the same issue and can confirm this. When having "globals" set to But after taking a look into docs from vitest config for globals, I saw that you need to add |
Thank you!! I can confirm this also worked for me! |
Just bumped in this issue. For me, after following the setup instructions found on the testing-library docs, I also had to add jsdom types: |
create a setupTest.ts file in src folder and add In vitest.config.ts file
And finally install types/testing-library. This worked for me |
According to the comments above, So this error can be fixed by using the TypeScript's Triple-Slash Directive: // https://github.com/testing-library/jest-dom/issues/515
/// <reference types="@testing-library/jest-dom" />
// https://github.com/testing-library/jest-dom?tab=readme-ov-file#with-vitest
import '@testing-library/jest-dom/vitest' Update: PR #589 can also solve this problem. |
"@testing-library/jest-dom": "^6.4.2", i have one project create with next.js
can't tell why it worked in first , but didn't in second |
@RinalinDS I just ran into this same issue this morning! Try this: import * as matchers from '@testing-library/jest-dom/matchers';
import '@testing-library/jest-dom/vitest';
import { cleanup } from '@testing-library/react';
import { afterEach, expect } from 'vitest';
expect.extend(matchers);
afterEach(() => {
cleanup();
}); The To get vscode to "get in the van", so to speak, I had to run |
To make it work if you are using the vitest:
|
Wanna give this a bump, as this is still unresolved in my codebase (using the latest versions) |
I actually resolved my issue. For those wondering, I had jest globals enabled in my project for some reason so i was never actually importing / using Vitest. I'd check to see if you're experiencing the same problem |
After upgrading Vitest to v2.1 and this lib to v6.6 I ran into this problem again. For reference I do not use vitest in globals mode, so more things broke after the upgrade. This is what worked for me: /// <reference types="@testing-library/jest-dom" />
import { expect } from "vitest";
import * as extensions from "@testing-library/jest-dom/matchers"; Importing the vitest sub-module of this library to augment TS just did not work for me. |
Ran into the same issue. Create a Include the file in your vitest config. export default defineConfig({
plugins: [react()],
test: {
environment: "jsdom",
setupFiles: "vitest.setup.ts",
},
}); To resolve type errors in your test files e.g. Property 'toBeInTheDocument' does not exist on type 'Assertion<HTMLElement>'. add ...
"compilerOptions": {
"types": ["@testing-library/jest-dom/vitest"],
},
... |
Only trouble I had was that I used to see red underlines in my editor for toBeInDocument or toHaveValue etc, as an error indicating:
Functionally tests were running fine. I already had "vitest.setup.ts" with below import to extend capability of expect: import for @testing-library/jest-dom/vitest; And my tsconfig.json was also updated with {
"compilerOptions": {
...
"types": ["@testing-library/jest-dom/vitest"],
...
},
"include": ["src", "./vitest.setup.ts"],
"exclude": ["node_modules"]
} What fixed that tserror for me is updating mapped types in vitest.config.ts |
@testing-library/jest-dom
version: 6.0.0node
version: 18.16npm
(oryarn
) version: 9.5.1Relevant code or config:
What you did:
Upgraded from v5 to v6
What happened:
I don't know why, but upgrading the package to the last version using vitest caused all my "toBeInTheDocument" matchers to be undefined, although I can see them in the type definition of the package.
Reproduction:
TO-DO
Problem description:
Suggested solution:
I guess this is due to an upgrade on the dependency of jest types but I'm not sure. Still digging.
The text was updated successfully, but these errors were encountered: