我正在尝试为我的简单 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) 我试图写一个map/ reduce以获得数组中每个数组的平均值。
例如。
[[1][2,3][4,5,6,7]] => [1, 2.5, 5.5]
Run Code Online (Sandbox Code Playgroud)
现在这是我的代码,结果是数组数组:
result.map(array => {
return array.reduce((a, b) => (a + b)) / array.length;
})
Run Code Online (Sandbox Code Playgroud)
[[1][2,3][4,5,6,7]] => [1, 2.5, 5.5]
Run Code Online (Sandbox Code Playgroud)
非常感谢获得所需输出的任何帮助。就目前而言,我的输出减少到的数组NaN而不是平均值。