本文介绍了类型"Matchers< any>"上不存在属性"toBeInTheDocument"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的简单React应用编写测试,该应用使用API​​等为狗舍创建UI.我已经导入了以下所示的模块并运行了以下命令

I'm trying to write tests for my simple React App that creates a UI for a dog Shelter using API etc. I have imported the modules shown below and ran the following command

npm install jest-dom react-testing-library --save-dev

但是,我得到了toBeInTheDocument();方法加红色下划线并显示错误消息

However, I'm getting the toBeInTheDocument(); method underlined in red and the error message

"Property 'toBeInTheDocument' does not exist on type 'Matchers<any>'."
import "react-testing-library/cleanup-after-each";
import "jest-dom/extend-expect";

import * as React from "react";
import PetCard from "./PetCard";
import Pet from "./Pet";
import { render } from "react-testing-library";

const petMock = {
  id: "225c5957d7f450baec75a67ede427e9",
  name: "Fido",
  status: "available",
  kind: "dog",
  breed: "Labrador",
} as Pet;

describe("PetCard", () => {
  it("should render the name", () => {
    const { getByText } = render(<PetCard pet={petMock} />);
    expect(getByText("Fido")).toBeInTheDocument();
  });

});

任何有关如何解决此问题的建议都将受到赞赏.

Any advice on how I can resolve this is appreciated.

推荐答案

请确保在您的项目中安装了正确的类型.即

Please make sure that the correct types are installed in your project. i.e.

npm i -D @ testing-library/jest-dom @ ^ 4.2.4

根据我的经验,版本5.x似乎缺少Typescript类型

From my experience the Typescript types seem to be missing from version 5.x

这篇关于类型"Matchers&lt; any&gt;"上不存在属性"toBeInTheDocument"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 14:35