【问题标题】:How to disable React Native YellowBox messages in jest tests如何在玩笑测试中禁用 React Native YellowBox 消息
【发布时间】:2019-07-17 15:13:13
【问题描述】:

我知道在设备中运行时如何禁用 YellowBox 警告:

YellowBox.ignoreWarnings(ignoredYellowBox);

但我不知道如何在开玩笑的测试中让他们沉默。有一些我们无法处理 atm 的弃用警告,它们使我们的测试非常嘈杂。我不想阻止我们测试中的每个 YellowBox 警告,但如果有必要,这也可以。

【问题讨论】:

    标签: react-native jestjs


    【解决方案1】:

    这是一件很烦人的事情。这是我们想出的:

    const warnings = [
      'Warning: NetInfo has been extracted from react-native core and will be removed in a future release.',
      'inside a test was not wrapped in act(...).',
    ];
    const oldError = console.error;
    jest.spyOn(console, 'error').mockImplementation((...args) => {
      const string = args.join(' ');
      if (warnings.some(warning => string.match(warning))) return;
      oldError(...args);
    });
    

    将该 sn-p 添加到您的 jest 设置文件中,并根据您的情况编辑 warnings 数组。

    【讨论】:

    • 谢谢,兄弟。当您不想禁用所有错误和警告时,这种方式更加完整。
    【解决方案2】:

    对于未来的读者,有一个简单的方法可以做到这一点。

    jest.spyOn(console, 'warn').mockImplementation(() => {})
    jest.spyOn(console, 'error').mockImplementation(() => {})
    

    对于打字稿:

    jest.spyOn(console, 'warn').mockImplementation(() => undefined)
    jest.spyOn(console, 'error').mockImplementation(() => undefined)
    

    请记住,通过使用这样的方法,您将禁用所有控制台错误和警告。 如果您想保留其他错误和警告,只需使用此问题中的第一个答案即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-28
      • 2021-08-01
      • 2022-01-13
      • 2017-08-20
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      相关资源
      最近更新 更多