Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkPropTypes: add argument that allows external logging #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -262,6 +283,7 @@ describe('PropTypesDevelopmentStandalone', () => {
'testComponent',
null,
);

PropTypes.resetWarningCache();
PropTypes.checkPropTypes(
propTypes,
Expand Down
3 changes: 2 additions & 1 deletion checkPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '')
);
}
Expand Down