Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit a358ffa

Browse files
committed
checkPropTypes: allow the same error to be logged multiple times
`shouldLogAllTypeFailures` allows to caller to bypass `loggedTypeFailures`, which will only call `warning` or `warningLogger` once for each occurrence of a warning.
1 parent 0263e43 commit a358ffa

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

__tests__/PropTypesDevelopmentStandalone-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,39 @@ describe('PropTypesDevelopmentStandalone', () => {
196196
expect(warningLogger).toBeCalledWith(false, 'Failed %s type: %s%s', 'prop', 'some error', '');
197197
expect(returnValue).toBe(undefined);
198198
});
199+
200+
it('warns on multiple failures, if specified', () => {
201+
spyOn(console, 'error');
202+
const propTypes = {
203+
foo(props, propName, componentName) {
204+
throw new Error('some error');
205+
},
206+
};
207+
const props = {foo: 'foo'};
208+
const returnValue = PropTypes.checkPropTypes(
209+
propTypes,
210+
props,
211+
'prop',
212+
'testComponent',
213+
null,
214+
null,
215+
true,
216+
);
217+
const returnValueSecondCall = PropTypes.checkPropTypes(
218+
propTypes,
219+
props,
220+
'prop',
221+
'testComponent',
222+
null,
223+
null,
224+
true,
225+
);
226+
227+
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
228+
expect(console.error.calls.count()).toEqual(2);
229+
expect(returnValue).toBe(undefined);
230+
expect(returnValueSecondCall).toBe(undefined);
231+
});
199232
});
200233

201234
describe('Primitive Types', () => {

checkPropTypes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (process.env.NODE_ENV !== 'production') {
2525
* @param {?Function} getStack Returns the component stack.
2626
* @private
2727
*/
28-
function checkPropTypes(typeSpecs, values, location, componentName, getStack, warningLogger = null) {
28+
function checkPropTypes(typeSpecs, values, location, componentName, getStack, warningLogger = null, shouldLogAllTypeFailures = false) {
2929
if (process.env.NODE_ENV !== 'production') {
3030
for (var typeSpecName in typeSpecs) {
3131
if (typeSpecs.hasOwnProperty(typeSpecName)) {
@@ -42,9 +42,9 @@ function checkPropTypes(typeSpecs, values, location, componentName, getStack, wa
4242
error = ex;
4343
}
4444
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);
45-
if (error instanceof Error && !(error.message in loggedTypeFailures)) {
46-
// Only monitor this failure once because there tends to be a lot of the
47-
// same error.
45+
if (error instanceof Error && (shouldLogAllTypeFailures || !(error.message in loggedTypeFailures))) {
46+
// Only monitor this failure once (unless otherwise specified)
47+
// because there tends to be a lot of the same error.
4848
loggedTypeFailures[error.message] = true;
4949

5050
var stack = getStack ? getStack() : '';

0 commit comments

Comments
 (0)