react native 的 Bugsnag 库的 Jest 错误

Jest Error with Bugsnag Library for react native

提问人:Abhay 提问时间:11/11/2022 更新时间:7/15/2023 访问量:166

问:

在 react native 中面临 bugsnag mocking 的问题

错误-

FAIL 测试/App-test.js ● 正确渲染

TypeError: _reactNative.default.start is not a function

  40 |     // StatusBar.setBackgroundColor(blueVersion.secondary);
  41 |     // StatusBar.setTranslucent(true);
> 42 |     Bugsnag.start();
     |             ^
  43 |     UserExperior.startRecording(CONFIG_KEYS['USER_EXPERIOR_KEY']);
  44 |     deviceData();
  45 |     SplashScreen.hide();

  at src/App.js:42:13
  at commitHookEffectListMount (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12999:26)
  at commitPassiveMountOnFiber (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14422:11)
  at commitPassiveMountEffects_complete (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14382:9)
  at commitPassiveMountEffects_begin (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14369:7)
  at commitPassiveMountEffects (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14357:3)
  at flushPassiveEffectsImpl (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16248:3)
  at flushPassiveEffects (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16197:14)
  at performSyncWorkOnRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15413:3)
  at flushSyncCallbacks (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2597:22)

测试套件:1 次失败,共 1 次 测试:1 次失败,共 1 次 快照:共 0 个 时间:1.235 秒,估计 2 秒

尝试过的解决方案 -

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;
  },
}));

开玩笑地设置 js 但不起作用。

reactjs react-native jestjs bugsnag

评论


答:

1赞 timotgl 1/18/2023 #1

在开玩笑的环境中,我也有很多打电话的问题。最终通过手动模拟它来解决它:Bugsnag__mocks__/@bugsnag/react-native.js

module.exports = {
  addOnError: jest.fn(),
  notify: jest.fn(),
  // add other functions here if you need them
};
0赞 bytehala 7/15/2023 #2

对于(又名 jest-setup.js 或 jest.setup.js),这种替代方法也有效:jestsetup.js

jest.mock("@bugsnag/react-native", () => ({
  leaveBreadcrumb: jest.fn(),
  notify: jest.fn(),
  start: jest.fn(),
}));

https://dev.to/bytehala/how-to-mock-bugsnag-in-jestsetupjs-c79