React.js/testing-library/react =>;类型错误:无法读取 null 的属性(读取“addEventListener”)

发布于 2025-01-11 16:45:54 字数 1120 浏览 0 评论 0原文

我正在尝试对一些反应组件进行单元测试,并且在一些组件测试中出现了此错误。我尝试了很多解决方案,但没有任何结果

TypeError: Cannot read properties of null (reading 'addEventListener')
  • 测试代码
    import { fireEvent, render, screen } from "@testing-library/react";
    import { act } from "react-test-renderer";
    import "@testing-library/jest-dom";
    import { Provider } from "react-redux";
    import configureStore from "redux-mock-store";
    import { initialState } from "./mock";
    import "jest-location-mock";
    import TabsPanel from "../TabsPanel";
    import { usei18n } from "../i18n";
    
    const i18n = usei18n("en");
    
    describe("TabsPanel component", () => {
      const mockStore = configureStore();
      let store = mockStore(initialState);
    
      test("renders TabsPanel without crashing", async () => {
        const { getByTestId } = render(
          <Provider store={store}>
            <TabsPanel />
          </Provider>
        );
    
        expect(getByTestId("TabsPanel-component")).toBeTruthy();
      });
    });

I'm trying to unit test some of react components, and I got this error with some of the component tests. I have tried many solutions but without any result

TypeError: Cannot read properties of null (reading 'addEventListener')
  • Test code
    import { fireEvent, render, screen } from "@testing-library/react";
    import { act } from "react-test-renderer";
    import "@testing-library/jest-dom";
    import { Provider } from "react-redux";
    import configureStore from "redux-mock-store";
    import { initialState } from "./mock";
    import "jest-location-mock";
    import TabsPanel from "../TabsPanel";
    import { usei18n } from "../i18n";
    
    const i18n = usei18n("en");
    
    describe("TabsPanel component", () => {
      const mockStore = configureStore();
      let store = mockStore(initialState);
    
      test("renders TabsPanel without crashing", async () => {
        const { getByTestId } = render(
          <Provider store={store}>
            <TabsPanel />
          </Provider>
        );
    
        expect(getByTestId("TabsPanel-component")).toBeTruthy();
      });
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文