我有一个关于扩展类型记录接口的问题。以下是我的情况:
在没有Jest的测试中,我使用的是expect (我单独安装了它,它可以工作)。
现在,我想使用jest-dom来添加DOM的expect匹配器。(它们非常有用,将使我的测试更容易编写)。
我做了以下工作来扩展expect
import expect from 'expect'
import * as matchers from '@testing-library/jest-dom/matchers'
expect.extend(matchers)这样做可以增加匹配器,但是类型记录不知道它们。
我安装了@types/testing-library/jest-dom,但没有解决问题。
在@types/testing-library__jest-dom内部,我看到它扩展了jest的类型,而不是独立的expect。
我尝试添加具有以下内容的expect.d.ts:
import 'expect'
declare module 'expect' {
export interface Matchers<R> {
toBeInTheDOM(container?: HTMLElement | SVGElement): R
// ... other methods
}
}但打字稿仍在抱怨。
有什么想法吗?
发布于 2020-11-25 21:21:53
我找到了解决方案,下面的工作如下
import 'expect/build/types'
declare module 'expect/build/types' {
export interface Matchers<R> {
// matchers methods here
}
}https://stackoverflow.com/questions/65012078
复制相似问题