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

YellowBox.ignoreWarnings(ignoredYellowBox);


但是我不知道如何通过开玩笑的​​测试使它们静音。有一些过时的警告,我们不能处理atm,它们使我们的测试非常嘈杂。我宁愿不要从测试中阻止每个YellowBox警告,但是如果有必要,那也可以。

最佳答案

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

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


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

09-30 16:26