如何将 React 测试库"not wrapped in act"警告转换为 Jest 单元测试错误



我们正在使用Jest和react测试库对react应用程序进行单元测试。我们得到了一些"经典"错误:

console.error
Warning: A suspended resource finished loading inside a test, but the event was not wrapped in act(...).

When testing, code that resolves suspended data should be wrapped into act(...):

act(() => {
/* finish loading suspended data */
});
/* assert on the output */

但是有些开发人员直接忽略了这种类型的错误。因此,随着时间的推移,测试输出会因为这样的警告而变得臃肿。而且我们的测试感觉不那么自信。

在Jest或React测试库中是否有配置将这种警告变为错误并使单元测试失败?我在谷歌上搜索,发现了很多(好的)文章来解决这个警告,但没有把它变成真正的错误。

你可以使用[jest-fail-on-console]: https://www.npmjs.com/package/jest-fail-on-console npm包让测试在控制台上失败。错误,console.warning。

最新更新