-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
Don't suppress jsdom error reporting in our tests #13401
Conversation
ReactDOM: size: -0.3%, gzip: -0.3% Details of bundled changes.Comparing: 69e2a0d...fffe608 react-dom
react-art
react-test-renderer
Generated by 🚫 dangerJS |
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.
And it's nice to remove the weird hack with mucking the error prototype.
Agreed!
This looks good to me.
// Manually override console.error() because our toWarn() matchers | ||
// filter out precisely the messages we want to test for in this file. | ||
const oldConsoleError = console.error; | ||
console.error = jest.fn(); |
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.
Why not do this override-and-restore stuff in beforeEach
/afterEach
? Seems more "standard"
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.
I wanted it to really jump in front of you because I think it might be easy to miss that in this file console.error
setup differs from our normal one. I don't feel strongly about this.
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.
Gotcha. I also don't feel strongly about this, was just curious.
scripts/jest/matchers/toWarnDev.js
Outdated
const createConsoleSpy = methodName => (format, ...args) => { | ||
// Ignore uncaught errors reported by jsdom | ||
// and React addendums because they're too noisy. | ||
if (methodName === 'error' && shouldIgnoreConsoleError(format, args)) { |
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.
Totally unimportant but I'm just curious– was changing consoleSpy
to createConsoleSpy
actually unnecessary? Couldn't methodName
have just used consoleMethod
?
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.
Didn't realize it was in scope lol.
Fixes #8260.
It removes the
error
event listener which caused all jsdom errors to be ignored. We still silence some of the errors, but atconsole.error
call time. The benefit of this approach compared to our existingevent.preventDefault()
call is that we still fail the test if the error was unexpected (like described in #8260). And it's nice to remove the weird hack with mucking the error prototype.I implemented silencing by adding a few special cases to our
console.error
mocks that ignore certain messages (seeshouldIgnoreConsoleError.js
). I've spent a few days trying several other explicit approaches before that. They were all too annoying and complicated, and led to too much noise in tests themselves. Especially because we have many different configurations (dev, prod, bundles). Ignoring ended up the most straightforward (and hopefully maintainable) solution. And that's what we were effectively doing anyway, except at an earlier stage.For the few cases where we were testing logging itself, I changed those to directly overwrite
console.error
and assert on the mock calls. Seems easier than remembering what gets mocked and when. I also made the test non-internal while I was at it.