-
Notifications
You must be signed in to change notification settings - Fork 403
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
Export Vitest extend-expect #439
Comments
I suspect that you can already do this (the same as above, but without the import { expect } from 'vitest';
import * as matchers from '@testing-library/jest-dom/matchers';
expect.extend(matchers); Can you confirm? I tried it myself, and it worked, but it would not hurt to make sure. However, the types part is not solved. But that requires a PR to https://github.com/DefinitelyTyped/DefinitelyTyped. It'd be great if you'd contribute that for us. Can you? |
@gnapse Ah yes I missed that file. Although because of the CJS default export, it only works as import matchers from '@testing-library/jest-dom/matchers'; But that is fine. I'll put a PR into DefinitelyTyped |
We're introducing EMS exports soon (#438). Does that help? |
@gnapse ESM exports will be great. Will probably involve reworking the |
Just curious, why is that? I'm definitely open to suggestions on the ESM PR to avoid as much churn as possible. |
Was thinking it would need something like this: DefinitelyTyped/DefinitelyTyped#57195 but could be wrong. |
@AndrewLeedham Did you solve your problem? This is blocking me from using Vitest with testing-library. The above solutions only work when vitest is configured for globals. |
Hi @hornta. My Definitely Typed PR landed so you should be able to do this: // vitest-setup.ts
import { vi } from 'vitest';
import matchers, {
TestingLibraryMatchers,
} from '@testing-library/jest-dom/matchers';
declare global {
namespace Vi {
interface JestAssertion<T = any>
extends jest.Matchers<void, T>,
TestingLibraryMatchers<T, void> {}
}
}
expect.extend(matchers); |
@AndrewLeedham Tried to use your exact same solution but I always end up with weird type errors // vitest-setup.ts
import { vi } from 'vitest';
import matchers, {
TestingLibraryMatchers,
} from '@testing-library/jest-dom/matchers';
declare global {
namespace Vi {
interface JestAssertion<T = any>
extends jest.Matchers<void, T>,
TestingLibraryMatchers<T, void> {}
}
}
expect.extend(matchers);
I can see that the interface interface Matchers<R = void, T = {}> I'd appreciate any ideas on how to approach this problem. |
For those coming here after updating to vitest >= 0.31.0, the above workaround no longer works because the assertions package location changed (https://github.com/vitest-dev/vitest/releases/tag/v0.31.0) The following works for me: // vitest-setup.ts
import type { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
import matchers from "@testing-library/jest-dom/matchers";
import { expect } from "vitest";
declare module "vitest" {
interface Assertion<T = any>
extends jest.Matchers<void, T>,
TestingLibraryMatchers<T, void> {}
}
expect.extend(matchers); |
I'm still getting the following error:
versions: package manager: yarn 1.22.19 What I've tried: import '@testing-library/jest-dom'
import '@testing-library/jest-dom/extend-expect'
import matchers, { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers'
import { afterEach, beforeEach, expect, vi } from 'vitest'
declare module 'vitest' {
interface Assertion<T = any> extends jest.Matchers<void, T>, TestingLibraryMatchers<T, void> {}
}
expect.extend(matchers) |
@remy90 Just ran into the same problem. importing with |
|
For vitest, you can also follow the steps in the readme: https://github.com/testing-library/jest-dom#with-vitest |
For me, this worked and I didn't need to declare the types: // vite.config.ts
/// <reference types="vitest" />
/// <reference types="vite/client" />
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
test: {
globals: true,
environment: "jsdom",
setupFiles: ["./setupTests.ts"],
css: true,
},
}); // setupTests.ts
import "@testing-library/jest-dom/vitest"; // tsconfig.json
{
"compilerOptions": {
// ..
"types": ["vitest/globals"],
},
"include": ["src", "setupTests.ts"],
} |
I'm not able to get any of the above solutions to work when the jest-dom package is version 6 or greater |
@bradleyball please open a new issue with all the details of the problem you're having. |
…end-expect testing-library/jest-dom#439 и добавляет первый тест для Logo
Following the instructions shared here: testing-library/jest-dom#439 (comment) Signed-off-by: Stephan Renatus <[email protected]>
Following the instructions shared here: testing-library/jest-dom#439 (comment) Signed-off-by: Stephan Renatus <[email protected]>
for me nearly all worked but IDE couldnt see those matchers types, this was solution I used to have no TS issues: |
Describe the feature you'd like:
I would like to use
jest-dom
in Vitest.Suggested implementation:
Since both Jest and Vitest use the same style of matchers, and have very similiar APIs, perhaps
jest-dom
can export a secondary entry point for Vitest, or make the matchers export a first-class API.Describe alternatives you've considered:
I can manually add the matchers in a setup file using:
However, I am reaching into the
dist
folder which is not a public API, also I am using TypeScript and having to setup the types myself, rather than using@types/jest-dom
.Teachability, Documentation, Adoption, Migration Strategy:
This would just be an additional entry file just for Vitest, so should not change the functionality of
jest-dom
.The text was updated successfully, but these errors were encountered: