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

Fixed potential false-positive in toWarnDev matcher #11898

Merged
merged 8 commits into from
Jan 5, 2018

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented Dec 20, 2017

Spying on console.error or console.warn will now fail tests by default, with an error suggesting the use of the new toWarnDev matcher instead. (This has been removed.)

Updated .toWarnDev() matcher to report a failed expectation to Jest even if an Error was thrown by its callback. Giving precedence to failed expectations prevents a thrown error from potentially masking failed expectations (and removes a source of potential false-positives from our tests).

As of this point, all but 2 of our tests have been updated to the new matcher:

  • ReactIncrementalErrorLogging-test.internal: There's one test case that I'm unsure of how to update. I'll look at it again tomorrow.
  • ReactDOMServerIntegrationTestUtils: It's possible to update this test util but it will take time since several, procedurally-generated tests use it. For the time being, I've added a TODO comment for following up on it.

const noop = function() {};
const spyOn = function(object, methodName) {
if (object === console) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: Some of our tests spy on console.log (eg ReactDOMComponent-test) so this would also need to check if methodName === "warn" || methodName === "error"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also change them to use jest.fn or something directly.

@bvaughn bvaughn changed the title [WIP] Disallow spying on the console Disallow spying on the console Jan 4, 2018
@bvaughn bvaughn changed the title Disallow spying on the console [wip] Disallow spying on the console Jan 4, 2018
@bvaughn bvaughn requested review from gaearon and removed request for gaearon January 4, 2018 22:08
Also updated (most of) ReactIncrementalErrorLogging-test.internal to use the new matcher
@bvaughn bvaughn changed the title [wip] Disallow spying on the console Fixed potential false-positive in toWarnDev matcher Jan 4, 2018
@bvaughn bvaughn requested a review from gaearon January 4, 2018 22:47
Copy link
Collaborator

@gaearon gaearon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense to me

@bvaughn bvaughn merged commit c94b4b8 into facebook:master Jan 5, 2018
@bvaughn bvaughn deleted the spyOn-console-warning branch January 5, 2018 17:44
yenshih pushed a commit to yenshih/react that referenced this pull request Jan 6, 2018
* Warn about spying on the console

* Added suppress warning flag for spyOn(console)

* Nits

* Removed spy-on-console guard

* Fixed a potential source of false-positives in toWarnDev() matcher
Also updated (most of) ReactIncrementalErrorLogging-test.internal to use the new matcher

* Removed unused third param to spyOn

* Improved clarity of inline comments

* Removed unused normalizeCodeLocInfo() method
ManasJayanth pushed a commit to ManasJayanth/react that referenced this pull request Jan 12, 2018
* Warn about spying on the console

* Added suppress warning flag for spyOn(console)

* Nits

* Removed spy-on-console guard

* Fixed a potential source of false-positives in toWarnDev() matcher
Also updated (most of) ReactIncrementalErrorLogging-test.internal to use the new matcher

* Removed unused third param to spyOn

* Improved clarity of inline comments

* Removed unused normalizeCodeLocInfo() method
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.

None yet

3 participants