Can anyone help me with extending Typescript interfaces? I have come across a situation that I am trying to solve.
In my tests, I am utilizing expect
without using Jest directly (I installed it separately and it functions properly).
Now, I am interested in integrating jest-dom
to include the expect
matchers for DOM. These matchers are incredibly beneficial and will simplify the process of writing tests.
To extend expect, I performed the following steps:
import expect from 'expect'
import * as matchers from '@testing-library/jest-dom/matchers'
expect.extend(matchers)
Although this successfully adds the matchers, Typescript is not aware of them.
I attempted to address this by installing @types/testing-library/jest-dom
, but unfortunately, it did not resolve the issue.
Upon inspecting @types/testing-library__jest-dom
, I discovered that it enhances the types of jest
, rather than the standalone expect
.
Even after creating an expect.d.ts
file with the necessary code, which includes:
import 'expect'
declare module 'expect' {
export interface Matchers<R> {
toBeInTheDOM(container?: HTMLElement | SVGElement): R
// ... other methods
}
}
Typescript continues to generate errors. Any suggestions on how to proceed?