Karma and React, have warnings to cause errors
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);};
Small improvements to accepted answer: console.error
instead of console.warn
as spain-train mentioned, added 'Failed prop type' to regex, as only then it works with React 15.3.1, and made the code more strict eslint friendly.
const error = console.error;console.error = function(warning, ...args) { if (/(Invalid prop|Failed prop type)/.test(warning)) { throw new Error(warning); } error.apply(console, [warning, ...args]);};
2021 update:
const consoleError = console.error;console.error = function (...args) { if (/(Invalid prop|Failed propType|Failed .+ type)/.test(args[0])) { const errorMessage = args.reduce((p, c) => p.replace(/%s/, c)); throw new Error(errorMessage); } consoleError.apply(console, args);};
Failed prop type
is now Failed %s type: %s%s
. It uses string substitutions to write to console. Here is the code in React.