Closed InfiniteXyy closed 3 weeks ago
This was likely introduced by the change in https://github.com/testing-library/jest-dom/pull/636.
Adding "types": ["@testing-library/jest-dom"]
to tsconfig solves this issue for me.
Adding
"types": ["@testing-library/jest-dom"]
to tsconfig solves this issue for me.
Yes, I think it's working, but in the source code of "@testing-library/jest-dom"
it's referencing jest, which is not so correct.
/// <reference types="jest" />
If you happen to have @types/jest
installed in project, (for example in a monorepo and jest is used in one project, vitest for another.) it will make the expect become jest.Expect
,
I am running into the same issue with toHaveNoViolations
matcher for vitest-axe.
@testing-library/jest-dom
version: 6.6.2node
version: 20.17.0vitest
version: 2.0.0vitest-axe
version: 0.1.0npm
version: 10.8.2// vitest-setup.js
import * as matchers from "vitest-axe/matchers";
import { expect } from "vitest";
expect.extend(matchers);
// global.d.ts
import "vitest";
import type { AxeMatchers } from "vitest-axe/matchers";
declare module "vitest" {
export interface Assertion extends AxeMatchers {}
export interface AsymmetricMatchersContaining extends AxeMatchers {}
}
I upgraded @testing-library/jest-dom
to version 6.6.2 in a project using Vitest and Vitest-Axe for accessibility testing. I followed the documented method of extending matchers in Vitest and Vitest-Axe, as shown above. After the upgrade, I encountered type errors during the build process, specifically regarding the toHaveNoViolations
matcher from vitest-axe
.
While tests ran successfully, during the build, TypeScript reported the following type errors:
TS2339: Property 'toHaveNoViolations' does not exist on type 'Assertion<AxeResults>'.
This error occurred across multiple test files where the toHaveNoViolations
matcher was used.
expect(await axe(container)).toHaveNoViolations();
in any test.@testing-library/jest-dom
to version 6.6.2.npm run build
to see the type error.After upgrading to @testing-library/jest-dom
version 6.6.2, TypeScript is unable to recognize the toHaveNoViolations
matcher provided by vitest-axe
during the build process. This is likely due to a mismatch between the TypeScript types for jest-dom
and custom matchers added by vitest-axe
. The issue prevents builds from completing successfully, despite tests passing.
The recent changes made in version 6.6.2 (https://github.com/testing-library/jest-dom/pull/636/files) on the TestingLibraryMatchers
for Vitest are most likely the culprit. It would be helpful to review those changes to ensure compatibility with Vitest's custom matchers, such as toHaveNoViolations
, and provide guidance on extending matchers in Vitest for TypeScript projects. I am already extending the matchers as documented in Vitest and Vitest-Axe, but the build errors persist.
We encountered this issue after upgrading to version 6.6.2. matchers like .toHaveTextContent
and .toBeVisible
are no longer available. For now, we’ve decided to revert to version 6.6.1 until this is resolved.
Fixed in #646
@testing-library/jest-dom
version: 6.6.2node
version: 20.10.0npm
(oryarn
) version: 10.2.3Relevant code or config / Reproduction:
minimun reproduce demo: https://stackblitz.com/edit/vitejs-vite-ydhnrx?file=src%2FApp.test.tsx
What you did:
Bootstramp a starter repo with vitest & testing-library (vitest should be in global mode, and there is no
import xxx from 'vitest'
in the whole project)Problem description / What happened:
The
toHaveTextContent
should be a matcher of expect, and also is working in runtime, but a type error is thrown.You can run
npm run build
to see the errorSuggested solution:
According to the documents of vitest, to extend a matcher, we have to import the
"vitest"
module otherwise the interface merge might not work as expected So we could add this line of code to top of the definitionYou can try adding this in the stackbliz repo's dependency (jumping to the source code by ctrl + click the import) and run
npm run build
again, the type error should be fixed