From 8ce24787b95d9cfe8c41333269538a892f496734 Mon Sep 17 00:00:00 2001 From: Stephane Rufer Date: Wed, 12 Jul 2017 20:09:24 -0700 Subject: [PATCH] [New] checkPropTypes: add argument that allows external logging When specified, the argument `warningLogger` will be called with the same arguments as `warning`. Instead of logging errors to the fbjs warning logger, we are able to handle them externally. Fixes #34 --- .../PropTypesDevelopmentStandalone-test.js | 22 +++++++++++++++++++ checkPropTypes.js | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) 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 : '') ); }