【问题标题】:How to use jest-dom without Jest如何在没有 Jest 的情况下使用 jest-dom
【发布时间】:2020-11-25 20:14:39
【问题描述】:

我有一个关于扩展 Typescript 接口的问题。 这是我的情况:

我在没有 Jest 的情况下在我的测试中使用 expect(我单独安装它并且它可以工作)。

现在我想使用 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

import 'expect'

declare module 'expect' {
  export interface Matchers<R> {
    toBeInTheDOM(container?: HTMLElement | SVGElement): R
    // ... other methods
  }
}

但 Typescript 仍在抱怨。

有什么想法吗?

【问题讨论】:

    标签: typescript react-testing-library jest-dom


    【解决方案1】:

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

    import 'expect/build/types'
    
    declare module 'expect/build/types' {
      export interface Matchers<R> {
        // matchers methods here
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      相关资源
      最近更新 更多