-
Notifications
You must be signed in to change notification settings - Fork 408
Missing type definitions for expect imported from @jest/globals #426
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
Comments
Thanks for reporting this. I have to say, I see this more as a problem with jest than with jest-dom, if really the solution involves duplicating the code that extends the interface of |
After a deeper look, I see that the types of jest-dom extend jest.Matchers Meanwhile, the types of I’d naively expect that it’s rather |
I'm importing |
In my case, I've just switched to |
I wasn't able to solve it the proper way either, but I've made a patch on the types coming from import type { TestingLibraryMatchers } from '@types/testing-library__jest-dom/matchers';
// import type { expect } from '@jest/globals'; <-- this was necessary because of my specific setup of cypress + jest, otherwise it was inferring the wrong expect
declare module '@jest/globals/node_modules/expect/build/types' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/ban-types
export interface Matchers<R = void, T = {}>
extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
} this feels a bit risky because it relies on a path that could change at any moment ( |
Is there a solution for this yet? |
I assumed this should worked but couldn't get it to work yet. import matchers from '@testing-library/jest-dom/matchers';
import { expect } from '@jest/globals';
expect.extend(matchers); |
Looks like Jest@28 should enable that |
I added the following to my import type { TestingLibraryMatchers } from '@types/testing-library__jest-dom/matchers';
declare module '@jest/expect' {
export interface Matchers<R = void, T = {}> extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
}
import type { JestExpect } from '@jest/expect';
export declare const expect: JestExpect; |
Just to add: since Jest v28 the above should work equally with By the way, it might be a good idea to include this augmentation declaration in |
I submitted a PR to DefinitelyTyped that I think should fix this issue: DefinitelyTyped/DefinitelyTyped#65981 |
Wow, thanks @Vinnl I hit this problem yesterday and your fix has got rid of my issue :) |
I'm glad that this has fixed the issue for some. Unfortunately, it seems to be a source of problem for others. See DefinitelyTyped/DefinitelyTyped#65987 (comment) And we already have a pull request proposing to revert the changes: DefinitelyTyped/DefinitelyTyped#65990 I'm not clear what to do here. Would appreciate some help. |
@gnapse Hmm, I'm also not sure. I don't know exactly how to reproduce, but looking at the error message, it looks like the problem might be that we're setting However, if I try to change it in the I've submitted a followup PR that only changes it for |
@Vinnl @gnapse We face this issue while generating dist folder with script like "build:esm":"tsc" in package.json DefinitelyTyped/DefinitelyTyped#65991 Can this reversal PR be temporarily approved to prevent any impact on the systems? |
@abhi-works Is your code public somewhere that I could try it out? Or alternatively, could you open |
@Vinnl This issue occurs specifically when utilizing the tsc (TypeScript compiler) to generate a compiled distribution bundle. Code is not public,Will try manually in my local |
Alternatively, you can replicate the issue by creating a simple React TypeScript component and its corresponding test case that makes use of @testing-library/jest-dom. Afterwards, you can generate the build using tsc to observe the problem @Vinnl |
@abhi-works Could you tell Jest and TS version you are using? If I remember it right, the type arguments if the |
@Vinnl @gnapse https://github.com/DefinitelyTyped/DefinitelyTyped/pull/65991/files Works locally |
we'd better to install |
For me I had to install |
@testing-library/jest-dom
version: 5.16.1node
version: v16.13.1yarn
version: 1.22.17Relevant code or config:
What you did:
I imported jest globals from '@jest/globals'
What happened:
I got the TypeScript error message:
TS2339: Property 'toHaveAttribute' does not exist on type 'Matchers '.
Reproduction:
Problem description:
The custom matchers provided by jest-dom cannot be used in TypeScript if the
expect
is imported from@jest/globals
. Only the globalexpect
is modified, apparently.Suggested solution:
Extend the interface of
expect
in the@jest/globals
as well.The text was updated successfully, but these errors were encountered: