是否可以从Jest禁用特定的React警告(使用Create React App) [英] Is it possible to disable specific React warnings from Jest (using Create React App)

查看:201
本文介绍了是否可以从Jest禁用特定的React警告(使用Create React App)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,React开始为 componentWillReceiveProps 生命周期方法提供折旧警告。我正在使用一个利用此功能的库,维护人员尚未更新其代码库。

Recently, React started giving depreciation warnings for componentWillReceiveProps lifecycle method. I am using a library that utilized this function and the maintainers haven't updated their codebase yet.

当前,无论何时运行测试,无论是开发中的还是开发中的,在CI中,对于维护者提供的每个组件,我都会不断收到约30行的折旧警告。

Currently, any time I run my tests, whether it is in development or in CI, I keep getting ~30 lines of depreciation warnings for each component that the maintainer provides.

是否有一种方法可以抑制这些警告(至少在开发中)?

Is there a way to suppress these warnings (at least in development)?

编辑:

如果愿意,我愿意在文件中添加某些注释以禁用来自特定软件包的警告有机会:

I am willing to add certain comments in my files to disable warnings from a specific package if there is a chance:

// some line to disable warnings for this package
import { DateRangePicker } from 'react-dates';


推荐答案

如果要禁用所有满足某些条件的警告,并针对所有测试保留所有其他警告:

If you want to disable all warnings that meet some condition, keeping all other warnings, for all tests:

const originalWarn = console.warn.bind(console.warn)
beforeAll(() => {
  console.warn = (msg) => 
    !msg.toString().includes('componentWillReceiveProps') && originalWarn(msg)
}
afterAll(() => {
  console.warn = originalWarn
})






反应代码库还包含 expect(render(...))。toWarnDev(...) ,但笑话文档中未包含该信息,如果您想使用它,可能需要进行更多调查功能。


React codebase also contains expect(render(...)).toWarnDev(...), but that's not included in Jest documentation, you might need to investigate more if you want to use that feature.

这篇关于是否可以从Jest禁用特定的React警告(使用Create React App)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆