【问题标题】:Had so issue with jest react-native and bugsnag开玩笑的 react-native 和 bugsnag 有这么大的问题
【发布时间】:2021-02-12 14:24:57
【问题描述】:

我尝试使用 Bugsnag 在 React-Native 上运行 Jest 单元测试,但出现错误:

 The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/en/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.
    
    ReferenceError: window is not defined
      1 | import React from 'react';
    > 2 | import Bugsnag from '@bugsnag/react-native';

【问题讨论】:

    标签: react-native unit-testing jestjs bugsnag


    【解决方案1】:

    对我有用的解决方案:

    我们需要在 jestSetupFile.js 中添加模拟 bugsnag 拦截器

    jest.mock('@bugsnag/react-native', () => ({
      use(plugin) {
        const boundary = plugin.init();
        // we don't need the error boundary to swallow the errors, we want jest see them and fail the test
        delete boundary.prototype.componentDidCatch;
        return boundary;
      }
    })); 
    

    【讨论】:

      【解决方案2】:

      我只是通过嘲笑 Bugsnag 来修复它:jest.mock("@bugsnag/react-native", () => jest.fn());

      【讨论】:

        猜你喜欢
        • 2017-08-25
        • 1970-01-01
        • 2021-09-06
        • 1970-01-01
        • 2020-03-25
        • 1970-01-01
        • 2023-03-27
        • 2022-08-20
        • 2018-12-26
        相关资源
        最近更新 更多