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

标签 react-native unit-testing jestjs bugsnag

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

最佳答案

对我有用的解决方案:

我们需要将模拟 bugsnag 拦截器添加到 jestSetupFile.js

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

关于react-native - jest react-native 和 bugsnag 有这样的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66173565/

相关文章:

ios - 如何在 React Native 中从终端启用调试器/热重新加载/实时重新加载?

android - 构建 react-native .apk 时无法解析模块,babel-plugin-module-resolver 别名

javascript - 键盘打开时我需要点击两次 -- React native

java - 我应该在测试期间模拟 Spring Cloud Config 服务器属性吗?

reactjs - 如何测试使用自定义 TypeScript React Hook 的组件?

node.js - 使用 Jest 模拟模块无法正常工作

error-handling - 用 Jest 捕捉错误时如何测试 Redux-Saga

react-native - React Native 有没有办法等到函数完成?

java - 我可以用 Java 获取 Linux "max user processes"吗?

unit-testing - Flask:重新加载时运行测试