【发布时间】:2020-01-11 15:27:42
【问题描述】:
我正在尝试为我的简单 React 应用程序编写测试,该应用程序使用 API 等为狗收容所创建 UI。我已导入如下所示的模块并运行以下命令
npm install jest-dom react-testing-library --save-dev
但是,我得到了 toBeInTheDocument();红色下划线的方法和错误信息
"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();
});
});
感谢任何有关如何解决此问题的建议。
【问题讨论】:
-
听起来可能是eslint在抱怨,你有没有试过改变eslint环境?尝试将其放在测试文件的顶部
/* eslint-env jest */ -
谢谢你的工作:)
标签: reactjs testing jestjs mocha.js