-
Notifications
You must be signed in to change notification settings - Fork 224
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
suppress react test warnings #2961
Conversation
I think this issue could be an opportunity to clear out some tech debt rather than suppress warnings. If I was to approach this I would change the image props types file to be more explicit like promo's prop types and to not use this function because I think it just sets just check prop types and throws errors if they are incorrect which is what the prop-types does anyway. Would be worth checking with the author of example of more explicit prop types: Image.propTypes = {
blocks: arrayOf(any), // <-- this could be more detailed but I haven't check what it should be
position: arrayOf(number),
};
Image.defaultProps = {
blocks: [],
position: [1],
}; the image component only accepts 2 props and by looking at the logic in the component, both are not required so you can give them default props. You will then need to change the check that renders null if no if (!blocks) {
return null;
} would then become if (!blocks.length) {
return null;
} I tried this out locally and tests now pass with no warnings. |
@jroebu14 Adding Regarding the actual props description, using |
Hmmm i'm in 2 minds, part of me doesn't like setting defaults to please the tests, but also, writing defensive components is kinda nice too. Im not sure. I do think there is always going to be cases where you need to violate prop warnings in tests, so a helper like this is needed. But also, in some cases, perhaps code changes will cover it, and make our components better. |
@dr3 @pharingee it wasn't really to please tests but was more tailoring prop types to match the component's API.
It's also possible that if we keep it the way it is then the component can essentially silently fail i.e. not render an image. |
In that case, |
…into image-test-warnings
src/testHelpers/setupTests.js
Outdated
const warningsRegex = new RegExp( | ||
[REACT_FAILED_PROP_TYPE, ...Object.values(expectedWarnings)].join('.*'), | ||
); | ||
if (warningsRegex.test(message)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this currently doesnt allow you to supress 2 prop warnings at a time. E.g. below ImageContainer
has 2 required props, blocks
and foobar
describe('with no data', () => {
suppressPropWarnings(['blocks', 'ImageContainer']);
suppressPropWarnings(['foobar', 'ImageContainer']);
isNull('should return null', <ImageContainer />);
});
It will only supress 1 of the 2 console errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We wouldn't be able to test for that because once an error is thrown the rest of the code isn't executed. In cases where multiple props absences are detected, only one console error will be thrown so we can catch them all like this:
describe('with no data', () => {
suppressPropWarnings(['blocks', 'foo', 'bar', 'ImageContainer']);
isNull('should return null', <ImageContainer />);
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once an error is thrown the rest of the code isn't executed
Yeah it is, you can have 2 prop warning errors happen in 1 component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
happy with the technical approach here.
Resolves #2769
Overall change:
Add a function to suppress predicted prop warnings during tests.
Code changes:
suppressPropWarnings
function to add expected warnings to a global variablesetupTests
to ignore warnings that match expected warnings from global variable and reset global variable.