diff --git a/__tests__/PropTypesDevelopmentStandalone-test.js b/__tests__/PropTypesDevelopmentStandalone-test.js index 3a20120..2f84032 100644 --- a/__tests__/PropTypesDevelopmentStandalone-test.js +++ b/__tests__/PropTypesDevelopmentStandalone-test.js @@ -175,6 +175,27 @@ describe('PropTypesDevelopmentStandalone', () => { ); expect(returnValue).toBe(undefined); }); + + it('calls the passed in warning logger', () => { + const warningLogger = jest.fn() + const propTypes = { + foo(props, propName, componentName) { + throw new Error('some error'); + }, + }; + const props = {foo: 'foo'}; + const returnValue = PropTypes.checkPropTypes( + propTypes, + props, + 'prop', + 'testComponent', + null, + warningLogger, + ); + + expect(warningLogger).toBeCalledWith(false, 'Failed %s type: %s%s', 'prop', 'some error', ''); + expect(returnValue).toBe(undefined); + }); }); describe('Primitive Types', () => { diff --git a/checkPropTypes.js b/checkPropTypes.js index 0802c36..492d685 100644 --- a/checkPropTypes.js +++ b/checkPropTypes.js @@ -25,7 +25,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, warningLogger = null) { if (process.env.NODE_ENV !== 'production') { for (var typeSpecName in typeSpecs) { if (typeSpecs.hasOwnProperty(typeSpecName)) { @@ -49,7 +49,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 (warningLogger) { + warningLogger(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : ''); + } else { + warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : ''); + } } } }