有这样的问题与jest react-native和bugsnag

jc3wubiy  于 12个月前  发布在  Jest
关注(0)|答案(3)|浏览(131)

我试着用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';

字符串

ikfrs5lh

ikfrs5lh1#

为我工作的解决方案:
我们需要在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;
  }
}));

字符串

hrysbysz

hrysbysz2#

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

cfh9epnr

cfh9epnr3#

对我来说,起作用的是:

jest.mock('@bugsnag/react-native', () => {
  return {
    isStarted: () => false,
    start: () => false,
    getPlugin: () => {
      return {
        createErrorBoundary:
          () =>
          ({ children }: { children: JSX.Element }) =>
            children,
      };
    },
  };
});

字符串

相关问题