Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make React PropType warnings throw errors in Jasmine/Jest
var util = require('util');
// nobody cares about warnings so lets make them errors
// keep a reference to the original console methods
var consoleWarn = console.warn;
var consoleError = console.error;
function logToError() {
throw new Error(util.format.apply(this, arguments).replace(/^Error: (?:Warning: )?/, ''));
}
jasmine.getEnv().beforeEach(function() {
// make calls to console.warn and console.error throw an error
console.warn = logToError;
console.error = logToError;
});
jasmine.getEnv().afterEach(function() {
// return console.warn and console.error to default behaviour
console.warn = consoleWarn;
console.error = consoleError;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment