因果报应和React,警告会导致错误 [英] Karma and React, have warnings to cause errors

查看:106
本文介绍了因果报应和React,警告会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将业力 mocha 测试我的反应组件.当PropType不匹配时,我会显示一些警告.但是,让这些警告引起实际错误,以跟踪测试并进行修复,将是非常有趣的.

I am using Karma with mocha to test my React components. I have some warnings displayed when the PropTypes are not matched. However it would be really interesting to have these warnings to cause an actual error, as to track down the test and fix it.

您知道如何实现吗?

推荐答案

您可以使用自己的方法替换console.warn方法,并在提供的消息与特定模式匹配时抛出方法.

You can replace the console.warn method with your own and throw when the message provided matches a certain pattern.

let warn = console.warn;
console.warn = function(warning) {
  if (/(Invalid prop|Failed propType)/.test(warning)) {
    throw new Error(warning);
  }
  warn.apply(console, arguments);
};

这篇关于因果报应和React,警告会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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