diff --git a/__tests__/PropTypesDevelopmentStandalone-test.js b/__tests__/PropTypesDevelopmentStandalone-test.js index 2f84032..11e4475 100644 --- a/__tests__/PropTypesDevelopmentStandalone-test.js +++ b/__tests__/PropTypesDevelopmentStandalone-test.js @@ -196,6 +196,39 @@ describe('PropTypesDevelopmentStandalone', () => { expect(warningLogger).toBeCalledWith(false, 'Failed %s type: %s%s', 'prop', 'some error', ''); expect(returnValue).toBe(undefined); }); + + it('warns on multiple failures, if specified', () => { + spyOn(console, 'error'); + const propTypes = { + foo(props, propName, componentName) { + throw new Error('some error'); + }, + }; + const props = {foo: 'foo'}; + const returnValue = PropTypes.checkPropTypes( + propTypes, + props, + 'prop', + 'testComponent', + null, + null, + true, + ); + const returnValueSecondCall = PropTypes.checkPropTypes( + propTypes, + props, + 'prop', + 'testComponent', + null, + null, + true, + ); + + expect(console.error.calls.argsFor(0)[0]).toContain('some error'); + expect(console.error.calls.count()).toEqual(2); + expect(returnValue).toBe(undefined); + expect(returnValueSecondCall).toBe(undefined); + }); }); describe('Primitive Types', () => { diff --git a/checkPropTypes.js b/checkPropTypes.js index 492d685..d6e8aab 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, warningLogger = null) { +function checkPropTypes(typeSpecs, values, location, componentName, getStack, warningLogger = null, shouldLogAllTypeFailures = false) { if (process.env.NODE_ENV !== 'production') { for (var typeSpecName in typeSpecs) { if (typeSpecs.hasOwnProperty(typeSpecName)) { @@ -42,9 +42,9 @@ function checkPropTypes(typeSpecs, values, location, componentName, getStack, wa error = ex; } warning(!error || error instanceof Error, '%s: type specification of %s `%s` is invalid; the type checker ' + 'function must return `null` or an `Error` but returned a %s. ' + 'You may have forgotten to pass an argument to the type checker ' + 'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' + 'shape all require an argument).', componentName || 'React class', location, typeSpecName, typeof error); - if (error instanceof Error && !(error.message in loggedTypeFailures)) { - // Only monitor this failure once because there tends to be a lot of the - // same error. + if (error instanceof Error && (shouldLogAllTypeFailures || !(error.message in loggedTypeFailures))) { + // Only monitor this failure once (unless otherwise specified) + // because there tends to be a lot of the same error. loggedTypeFailures[error.message] = true; var stack = getStack ? getStack() : '';