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

Don't suppress jsdom error reporting in our tests #13401

Merged
merged 2 commits into from
Aug 15, 2018

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Aug 15, 2018

Fixes #8260.

It removes the error event listener which caused all jsdom errors to be ignored. We still silence some of the errors, but at console.error call time. The benefit of this approach compared to our existing event.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 (see shouldIgnoreConsoleError.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.

@gaearon gaearon requested review from bvaughn and acdlite August 15, 2018 01:42
@gaearon gaearon changed the title Don't suppress jsdom error reporting Don't suppress jsdom error reporting in our tests Aug 15, 2018
@pull-bot
Copy link

pull-bot commented Aug 15, 2018

ReactDOM: size: -0.3%, gzip: -0.3%

Details of bundled changes.

Comparing: 69e2a0d...fffe608

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js -0.0% -0.0% 644.7 KB 644.59 KB 151.78 KB 151.76 KB UMD_DEV
react-dom.production.min.js -0.3% -0.3% 96.3 KB 96.02 KB 31.22 KB 31.12 KB UMD_PROD
react-dom.development.js -0.0% -0.0% 640.83 KB 640.73 KB 150.63 KB 150.61 KB NODE_DEV
react-dom.production.min.js -0.3% -0.4% 96.29 KB 96.01 KB 30.8 KB 30.69 KB NODE_PROD
ReactDOM-dev.js -0.0% -0.0% 646.99 KB 646.88 KB 148.57 KB 148.54 KB FB_WWW_DEV
ReactDOM-prod.js -0.0% -0.0% 279.1 KB 279.01 KB 52.3 KB 52.28 KB FB_WWW_PROD
react-dom.profiling.min.js -0.3% -0.3% 97.5 KB 97.22 KB 31.2 KB 31.1 KB NODE_PROFILING
ReactDOM-profiling.js -0.0% -0.0% 281.52 KB 281.42 KB 52.95 KB 52.92 KB FB_WWW_PROFILING

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js -0.0% -0.0% 435.53 KB 435.43 KB 98.9 KB 98.87 KB UMD_DEV
react-art.production.min.js -0.3% -0.3% 85.16 KB 84.88 KB 26.4 KB 26.33 KB UMD_PROD
react-art.development.js -0.0% -0.0% 368.05 KB 367.95 KB 81.83 KB 81.81 KB NODE_DEV
react-art.production.min.js -0.6% -0.7% 50.15 KB 49.87 KB 15.79 KB 15.69 KB NODE_PROD
ReactART-dev.js -0.0% -0.0% 357.29 KB 357.18 KB 76.39 KB 76.36 KB FB_WWW_DEV
ReactART-prod.js -0.1% -0.1% 153.2 KB 153.1 KB 26.42 KB 26.39 KB FB_WWW_PROD

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js -0.0% -0.0% 363.95 KB 363.84 KB 80.24 KB 80.21 KB UMD_DEV
react-test-renderer.production.min.js -0.6% -0.7% 49.03 KB 48.75 KB 15.16 KB 15.06 KB UMD_PROD
react-test-renderer.development.js -0.0% -0.0% 360.08 KB 359.97 KB 79.28 KB 79.25 KB NODE_DEV
react-test-renderer.production.min.js -0.6% -0.7% 48.74 KB 48.46 KB 15.02 KB 14.92 KB NODE_PROD
ReactTestRenderer-dev.js -0.0% -0.0% 363.95 KB 363.84 KB 77.89 KB 77.87 KB FB_WWW_DEV

Generated by 🚫 dangerJS

@bvaughn bvaughn self-assigned this Aug 15, 2018
Copy link
Contributor

@bvaughn bvaughn left a 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();
Copy link
Contributor

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"

Copy link
Collaborator Author

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.

Copy link
Contributor

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.

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)) {
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

@gaearon gaearon merged commit b2adcfb into facebook:master Aug 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants