Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 单元测试失败,无法找到元素_Reactjs_Unit Testing_Jestjs_React Testing Library_Ts Jest - Fatal编程技术网

Reactjs 单元测试失败,无法找到元素

Reactjs 单元测试失败,无法找到元素,reactjs,unit-testing,jestjs,react-testing-library,ts-jest,Reactjs,Unit Testing,Jestjs,React Testing Library,Ts Jest,这是我第一次将单元测试放在一起,需要一些帮助来理解为什么我会出现以下错误 A) TestingLibraryElement错误:找不到文本为“访问我的页面”的元素。这可能是因为文本被多个元素分割。在这种情况下,您可以为文本匹配器提供一个功能,使匹配器更加灵活 B) expect(已收到)。toBeInTheDocument() 收到的值必须是HTMLElement或SVGElement。 接收的值为空 c) TestingLibraryElement错误:无法通过以下方式找到元素:[data t

这是我第一次将单元测试放在一起,需要一些帮助来理解为什么我会出现以下错误

A) TestingLibraryElement错误:找不到文本为“访问我的页面”的元素。这可能是因为文本被多个元素分割。在这种情况下,您可以为文本匹配器提供一个功能,使匹配器更加灵活

B) expect(已收到)。toBeInTheDocument()

收到的值必须是HTMLElement或SVGElement。 接收的值为空

c) TestingLibraryElement错误:无法通过以下方式找到元素:[data testid=“aboutLink”]

这是我在AModal.test.tsx中的一个测试

 import React from "react";
import AboutModal from "../../components/AModel";
import { render, screen, fireEvent, getByTestId } from "@testing-library/react";

describe("About Modal", () => {
  it("loads", () => {
    render(<AboutModal />, {});
    // Make sure the pop up has loaded
    expect(screen.queryByText("Welcome")).toBeInTheDocument();
  });
  it("Checks if link works", () => {
// expect(
    //   screen.queryByText("Visit the About Page for more info")
    // ).toBeInTheDocument();  ERROR B is coming here
    const { container } = render(<AboutModal />, {});
    fireEvent(getByTestId(container, "aboutLink"), new MouseEvent("click")); // Error C is coming here
    expect(screen.queryByText("About my document")).toBeInTheDocument();
  });
});
从“React”导入React;
从“../../components/AModel”导入关于模式的信息;
从“@testing library/react”导入{render,screen,firevent,getByTestId};
描述(“关于模态”,()=>{
它(“加载”,()=>{
渲染(
);
};
const poputtle=欢迎;
返回(
{content()}
);
};
导出默认MyModal

在此方面的任何帮助都将不胜感激


谢谢

请尝试以下操作-

const {getByTestId} = render(<AboutModal />, {});
fireEvent.click(getByTestId("aboutLink")); 
const{getByTestId}=render(,{});
单击(getByTestId(“aboutLink”);

看起来您错过了
页面后的空白处
:尝试
getByText(容器,“访问我的页面”)
谢谢@Estus,这不起作用。还是那个错误。@Thank aquinq,添加空格也没用。我想这段代码根本没有看到那一行。因为即使我添加了“访问”它给出了相同的错误。@Estus Flask我更新了帖子。我明白了。如果模式不可见,您不能期望测试通过。Cookie会交叉污染测试,hasVisitedBefore在测试1中设置,而模式在测试2中不可见。模拟
Cookie
Cookie.load
会返回您需要的内容。单击aboutLink会返回没有意义,因为您没有运行能够加载/关于页面的服务器。
const {getByTestId} = render(<AboutModal />, {});
fireEvent.click(getByTestId("aboutLink"));