Skip to content

Commit

Permalink
checkPropTypes: allow the same error to be logged multiple times
Browse files Browse the repository at this point in the history
`shouldLogAllTypeFailures` allows to caller to bypass
`loggedTypeFailures`, which will only call `warning` or `warningLogger`
once for each occurrence of a warning.
  • Loading branch information
rufman committed Oct 9, 2017
1 parent 0263e43 commit a358ffa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
33 changes: 33 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
8 changes: 4 additions & 4 deletions checkPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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() : '';
Expand Down

0 comments on commit a358ffa

Please sign in to comment.