我正在尝试为我的简单 React 应用程序编写测试,该应用程序使用 API 等为狗收容所创建 UI。我导入了如下所示的模块并运行以下命令
npm install jest-dom react-testing-library --save-dev
Run Code Online (Sandbox Code Playgroud)
但是,我得到了 toBeInTheDocument(); 红色下划线的方法和错误消息
"Property 'toBeInTheDocument' does not exist on type 'Matchers<any>'."
Run Code Online (Sandbox Code Playgroud)
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 …Run Code Online (Sandbox Code Playgroud) 我想在我的开玩笑测试代码中摆脱全局变量.具体来说describe,it和expect
describe('Welcome (Snapshot)', () => {
it('Welcome renders hello world', () => {
...
});
});
Run Code Online (Sandbox Code Playgroud)
所以我尝试添加
import {describe,it} from 'jest';
Run Code Online (Sandbox Code Playgroud)
和
import jest from 'jest';
jest.describe( ...
jest.it( ...
Run Code Online (Sandbox Code Playgroud)
和其他变化..
但没有运气.
我应该如何使它工作?