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

Output warnings using console.warn #245

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
76 changes: 38 additions & 38 deletions __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ function resetWarningCache() {
}

function getPropTypeWarningMessage(propTypes, object, componentName) {
if (!console.error.calls) {
spyOn(console, 'error');
if (!console.warn.calls) {
spyOn(console, 'warn');
} else {
console.error.calls.reset();
console.warn.calls.reset();
}
resetWarningCache();

PropTypes.checkPropTypes(propTypes, object, 'prop', 'testComponent');
const callCount = console.error.calls.count();
const callCount = console.warn.calls.count();
if (callCount > 1) {
throw new Error('Too many warnings.');
}
const message = console.error.calls.argsFor(0)[0] || null;
console.error.calls.reset();
const message = console.warn.calls.argsFor(0)[0] || null;
console.warn.calls.reset();

return message;
}
Expand Down Expand Up @@ -98,11 +98,11 @@ function expectWarningInDevelopment(declaration, value) {
for (let i = 0; i < 3; i++) {
declaration(props, propName, componentName, 'prop');
}
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
expect(console.warn.calls.count()).toBe(1);
expect(console.warn.calls.argsFor(0)[0]).toContain(
'You are manually calling a React.PropTypes validation ',
);
console.error.calls.reset();
console.warn.calls.reset();
}

describe('PropTypesDevelopmentReact15', () => {
Expand All @@ -112,7 +112,7 @@ describe('PropTypesDevelopmentReact15', () => {

describe('checkPropTypes', () => {
it('should warn for invalid validators', () => {
spyOn(console, 'error')
spyOn(console, 'warn')
const propTypes = { foo: undefined };
const props = { foo: 'foo' };
PropTypes.checkPropTypes(
Expand All @@ -122,14 +122,14 @@ describe('PropTypesDevelopmentReact15', () => {
'testComponent',
null,
);
expect(console.error.calls.argsFor(0)[0]).toEqual(
expect(console.warn.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`.'
);
});

it('does not return a value from a validator', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
const propTypes = {
foo(props, propName, componentName) {
return new Error('some error');
Expand All @@ -143,12 +143,12 @@ describe('PropTypesDevelopmentReact15', () => {
'testComponent',
null,
);
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
expect(console.warn.calls.argsFor(0)[0]).toContain('some error');
expect(returnValue).toBe(undefined);
});

it('does not throw if validator throws', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
const propTypes = {
foo(props, propName, componentName) {
throw new Error('some error');
Expand All @@ -162,18 +162,18 @@ describe('PropTypesDevelopmentReact15', () => {
'testComponent',
null,
);
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
expect(console.warn.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');
spyOn(console, 'warn');
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(
expect(console.warn.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`.'
);
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.array, /please/);
expectWarningInDevelopment(PropTypes.array, []);
expectWarningInDevelopment(PropTypes.array.isRequired, /please/);
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.any, null);
expectWarningInDevelopment(PropTypes.any.isRequired, null);
expectWarningInDevelopment(PropTypes.any.isRequired, undefined);
Expand Down Expand Up @@ -412,7 +412,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.arrayOf({foo: PropTypes.string}), {
foo: 'bar',
});
Expand Down Expand Up @@ -479,7 +479,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.element, [<div />, <div />]);
expectWarningInDevelopment(PropTypes.element, <div />);
expectWarningInDevelopment(PropTypes.element, 123);
Expand Down Expand Up @@ -578,7 +578,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.instanceOf(Date), {});
expectWarningInDevelopment(PropTypes.instanceOf(Date), new Date());
expectWarningInDevelopment(PropTypes.instanceOf(Date).isRequired, {});
Expand Down Expand Up @@ -678,7 +678,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.node, 'node');
expectWarningInDevelopment(PropTypes.node, {});
expectWarningInDevelopment(PropTypes.node.isRequired, 'node');
Expand Down Expand Up @@ -793,7 +793,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.objectOf({foo: PropTypes.string}), {
foo: 'bar',
});
Expand All @@ -813,12 +813,12 @@ describe('PropTypesDevelopmentReact15', () => {

describe('OneOf Types', () => {
it('should warn but not error for invalid argument', () => {
spyOn(console, 'error');
spyOn(console, 'warn');

PropTypes.oneOf('red', 'blue');

expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0]).toContain(
expect(console.warn).toHaveBeenCalled();
expect(console.warn.calls.argsFor(0)[0]).toContain(
'Invalid argument supplied to oneOf, expected an instance of array.',
);

Expand Down Expand Up @@ -868,7 +868,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.oneOf(['red', 'blue']), true);
expectWarningInDevelopment(PropTypes.oneOf(['red', 'blue']), null);
expectWarningInDevelopment(PropTypes.oneOf(['red', 'blue']), undefined);
Expand All @@ -877,33 +877,33 @@ describe('PropTypesDevelopmentReact15', () => {

describe('Union Types', () => {
it('should warn but not error for invalid argument', () => {
spyOn(console, 'error');
spyOn(console, 'warn');

PropTypes.oneOfType(PropTypes.string, PropTypes.number);

expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0]).toContain(
expect(console.warn).toHaveBeenCalled();
expect(console.warn.calls.argsFor(0)[0]).toContain(
'Invalid argument supplied to oneOfType, expected an instance of array.',
);

typeCheckPass(PropTypes.oneOf(PropTypes.string, PropTypes.number), []);
});

it('should warn but for invalid argument type', () => {
spyOn(console, 'error');
spyOn(console, 'warn');

const types = [undefined, null, false, new Date, /foo/, {}];
const expected = ['undefined', 'null', 'a boolean', 'a date', 'a regexp', 'an object'];

for (let i = 0; i < expected.length; i++) {
const type = types[i];
PropTypes.oneOfType([type]);
expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0]).toContain(
expect(console.warn).toHaveBeenCalled();
expect(console.warn.calls.argsFor(0)[0]).toContain(
'Invalid argument supplied to oneOfType. Expected an array of check functions, ' +
'but received ' + expected[i] + ' at index 0.'
);
console.error.calls.reset();
console.warn.calls.reset();
}

typeCheckPass(PropTypes.oneOf(PropTypes.string, PropTypes.number), []);
Expand Down Expand Up @@ -959,7 +959,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
[],
Expand Down Expand Up @@ -1057,7 +1057,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.shape({}), 'some string');
expectWarningInDevelopment(PropTypes.shape({foo: PropTypes.number}), {
foo: 42,
Expand Down Expand Up @@ -1164,7 +1164,7 @@ describe('PropTypesDevelopmentReact15', () => {
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
spyOn(console, 'warn');
expectWarningInDevelopment(PropTypes.exact({}), 'some string');
expectWarningInDevelopment(PropTypes.exact({foo: PropTypes.number}), {
foo: 42,
Expand Down