-1

我有一个关于扩展 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 仍在抱怨。

有任何想法吗?

4

1 回答 1

0

我找到了解决方案,以下工作

import 'expect/build/types'

declare module 'expect/build/types' {
  export interface Matchers<R> {
    // matchers methods here
  }
}
于 2020-11-25T21:21:53.457 回答