diff --git a/__tests__/PropTypesDevelopmentStandalone-test.js b/__tests__/PropTypesDevelopmentStandalone-test.js index 9684d56..8e92516 100644 --- a/__tests__/PropTypesDevelopmentStandalone-test.js +++ b/__tests__/PropTypesDevelopmentStandalone-test.js @@ -146,6 +146,31 @@ describe('PropTypesDevelopmentStandalone', () => { expect(console.error.calls.argsFor(0)[0]).toContain('some error'); expect(returnValue).toBe(undefined); }); + + it('throws an error if `throwError` is set', () => { + const error = 'some error'; + const propTypes = { + foo(props, propName, componentName) { + throw new Error(error); + }, + }; + const props = {foo: 'foo'}; + + let returnValue; + const checkFn = () => { + returnValue = PropTypes.checkPropTypes( + propTypes, + props, + 'prop', + 'testComponent', + null, + true, + ); + }; + + expect(checkFn).toThrow(error); + expect(returnValue).toBe(undefined); + }); }); describe('Primitive Types', () => { diff --git a/checkPropTypes.js b/checkPropTypes.js index c2b536f..c07c5e4 100644 --- a/checkPropTypes.js +++ b/checkPropTypes.js @@ -27,7 +27,7 @@ if (process.env.NODE_ENV !== 'production') { * @param {?Function} getStack Returns the component stack. * @private */ -function checkPropTypes(typeSpecs, values, location, componentName, getStack) { +function checkPropTypes(typeSpecs, values, location, componentName, getStack, throwError) { if (process.env.NODE_ENV !== 'production') { for (var typeSpecName in typeSpecs) { if (typeSpecs.hasOwnProperty(typeSpecName)) { @@ -51,7 +51,11 @@ function checkPropTypes(typeSpecs, values, location, componentName, getStack) { var stack = getStack ? getStack() : ''; - warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : ''); + if (throwError) { + throw error; + } else { + warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : ''); + } } } } diff --git a/checkPropTypesWithErrors.js b/checkPropTypesWithErrors.js new file mode 100644 index 0000000..6416975 --- /dev/null +++ b/checkPropTypesWithErrors.js @@ -0,0 +1,11 @@ +'use strict'; + +if (process.env.NODE_ENV !== 'production') { + var checkPropTypes = require('./checkPropTypes'); +} + +function checkPropTypesWithErrors(typeSpecs, values, location, componentName, getStack) { + checkPropTypes(typeSpecs, values, location, componentName, getStack, true); +} + +module.exports = checkPropTypesWithErrors; diff --git a/factoryWithThrowingShims.js b/factoryWithThrowingShims.js index 840f68e..cb2944a 100644 --- a/factoryWithThrowingShims.js +++ b/factoryWithThrowingShims.js @@ -53,6 +53,7 @@ module.exports = function() { }; ReactPropTypes.checkPropTypes = emptyFunction; + ReactPropTypes.checkPropTypesWithErrors = emptyFunction; ReactPropTypes.PropTypes = ReactPropTypes; return ReactPropTypes; diff --git a/factoryWithTypeCheckers.js b/factoryWithTypeCheckers.js index 4da647e..a8a27b2 100644 --- a/factoryWithTypeCheckers.js +++ b/factoryWithTypeCheckers.js @@ -15,6 +15,7 @@ var warning = require('fbjs/lib/warning'); var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret'); var checkPropTypes = require('./checkPropTypes'); +var checkPropTypesWithErrors = require('./checkPropTypesWithErrors'); module.exports = function(isValidElement, throwOnDirectAccess) { /* global Symbol */ @@ -506,6 +507,7 @@ module.exports = function(isValidElement, throwOnDirectAccess) { } ReactPropTypes.checkPropTypes = checkPropTypes; + ReactPropTypes.checkPropTypesWithErrors = checkPropTypesWithErrors; ReactPropTypes.PropTypes = ReactPropTypes; return ReactPropTypes;