【发布时间】:2020-09-15 18:44:34
【问题描述】:
我正在尝试使用 react-testing-library 测试组件,但测试仍然失败,但不知道原因。 这是我正在使用的两个文件
组件文件:
import React from "react";
const TestElements = () => {
const [counter, setCounter] = React.useState(0);
return (
<>
<h1 data-testid="counter">{counter}</h1>
</>
);
};
export default TestElements;
测试文件:
import React from "react";
import { render, cleanup } from "@testing-library/react";
import TestElements from "./TestElements";
afterEach(cleanup);
it("should equal to 0", () => {
const { getByTestId } = render(<TestElements />);
expect(getByTestId("counter")).toHaveTextContent(0);
});
【问题讨论】:
-
它是如何失败的?请发布日志/错误的副本。
-
我遇到类型错误:TypeError: expect(...).toHaveTextContent is not a function
-
还有其他东西:测试套件:2 个失败,总共 2 个测试:1 个失败,总共 1 个快照:1 个文件过时,总共 0 个时间:6.456 秒,估计 8 秒运行所有相关的测试套件更改文件。
标签: jestjs