我有一个关于扩展 Typescript 接口的问题。这是我的情况:
我expect
在没有 Jest 的情况下在我的测试中使用(我单独安装它并且它可以工作)。
现在我想用它jest-dom
来为 DOM 添加expect
匹配器。(它们非常有帮助,会让我的测试更容易编写)。
我做了以下扩展期望
import expect from 'expect'
import * as matchers from '@testing-library/jest-dom/matchers'
expect.extend(matchers)
这有效并添加了匹配器,但 Typescript 不知道它们。
我安装了@types/testing-library/jest-dom
,但没有解决问题。
往里@types/testing-library__jest-dom
看,我发现它扩展了的类型jest
而不是独立的expect
.
我尝试添加expect.d.ts
具有以下内容的 a:
import 'expect'
declare module 'expect' {
export interface Matchers<R> {
toBeInTheDOM(container?: HTMLElement | SVGElement): R
// ... other methods
}
}
但 Typescript 仍在抱怨。
有任何想法吗?