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

Commit

Permalink
Enhance warning message when a PropType is not correct (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcampagonzalez authored and aweary committed Aug 30, 2017
1 parent 9057907 commit 8abb8d7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
14 changes: 14 additions & 0 deletions __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ describe('PropTypesDevelopmentReact15', () => {
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
expect(returnValue).toBe(undefined);
});

it('warns if any of the propTypes is not a function', () => {
spyOn(console, 'error');
const propTypes = {
foo: PropTypes.invalid_type,
};
const props = { foo: 'foo' };
const returnValue = PropTypes.checkPropTypes(propTypes, props, 'prop', 'testComponent', null);
expect(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Failed prop type: testComponent: prop type `foo` is invalid; '
+ 'it must be a function, usually from the `prop-types` package, but received `undefined`.'
);
expect(returnValue).toBe(undefined);
});
});

describe('Primitive Types', () => {
Expand Down
14 changes: 14 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ describe('PropTypesDevelopmentStandalone', () => {
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
expect(returnValue).toBe(undefined);
});

it('warns if any of the propTypes is not a function', () => {
spyOn(console, 'error');
const propTypes = {
foo: PropTypes.invalid_type,
};
const props = { foo: 'foo' };
const returnValue = PropTypes.checkPropTypes(propTypes, props, 'prop', 'testComponent', null);
expect(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Failed prop type: testComponent: prop type `foo` is invalid; '
+ 'it must be a function, usually from the `prop-types` package, but received `undefined`.'
);
expect(returnValue).toBe(undefined);
});
});

describe('Primitive Types', () => {
Expand Down
2 changes: 1 addition & 1 deletion checkPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
try {
// This is intentionally an invariant that gets caught. It's the same
// behavior as without this statement except with a better message.
invariant(typeof typeSpecs[typeSpecName] === 'function', '%s: %s type `%s` is invalid; it must be a function, usually from ' + 'the `prop-types` package.', componentName || 'React class', location, typeSpecName);
invariant(typeof typeSpecs[typeSpecName] === 'function', '%s: %s type `%s` is invalid; it must be a function, usually from ' + 'the `prop-types` package, but received `%s`.', componentName || 'React class', location, typeSpecName, typeof typeSpecs[typeSpecName]);
error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret);
} catch (ex) {
error = ex;
Expand Down

0 comments on commit 8abb8d7

Please sign in to comment.