diff --git a/__tests__/PropTypesDevelopmentStandalone-test.js b/__tests__/PropTypesDevelopmentStandalone-test.js index ada4ea1..c9954fd 100644 --- a/__tests__/PropTypesDevelopmentStandalone-test.js +++ b/__tests__/PropTypesDevelopmentStandalone-test.js @@ -248,6 +248,27 @@ describe('PropTypesDevelopmentStandalone', () => { expectInvalidValidatorWarning(PropTypes.exact({ bar: 'true' }), 'string'); expectInvalidValidatorWarning(PropTypes.exact({ bar: null }), 'null'); }); + + 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('Failed prop type: some error'); + expect(returnValue).toBe(undefined); + }); }); describe('resetWarningCache', () => { @@ -262,6 +283,7 @@ describe('PropTypesDevelopmentStandalone', () => { 'testComponent', null, ); + PropTypes.resetWarningCache(); PropTypes.checkPropTypes( propTypes, diff --git a/checkPropTypes.js b/checkPropTypes.js index 481f2cf..95e16f0 100644 --- a/checkPropTypes.js +++ b/checkPropTypes.js @@ -80,7 +80,8 @@ function checkPropTypes(typeSpecs, values, location, componentName, getStack) { var stack = getStack ? getStack() : ''; - printWarning( + var warningLogger = arguments.length > 5 ? arguments[5] : printWarning; + warningLogger( 'Failed ' + location + ' type: ' + error.message + (stack != null ? stack : '') ); }