-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
isEqual
behavior given two Error
s is inconsistent with documentation
#3529
isEqual
behavior given two Error
s is inconsistent with documentation
#3529
Comments
Would you like to improve the docs for that? :) |
What are your concerns? |
@thymikee The concern is treating It's simple(r) enough to compare the message strings if that is the behavior that is desired. That's my subjective opinion. I opened this issue because it's something I found unexpected and had to look into the first time I compared |
* Updating docs for `.toEqual` Fixes jestjs#3529 * Update ExpectAPI.md
* Updating docs for `.toEqual` Fixes jestjs#3529 * Update ExpectAPI.md
Do you want to request a feature or report a bug?
Bug (in either the codebase or the documentation)
What is the current behavior?
What is the expected behavior?
https://facebook.github.io/jest/docs/en/expect.html#toequalvalue
According to the documentation,
toEqual
should perform a "deep equal". However, the implementation is inconsistent with the documentation when the the two objects areError
s:https://github.com/facebook/jest/blob/master/packages/jest-matchers/src/jasmine-utils.js#L72
With the current implementation,
toEqual
will only check themessage
properties of the twoError
s for equality instead of checking all properties of the two objects recursively. This is inconsistent with the documentation which does not mention this special behavior. Either the documentation should be updated or the implementation should be fixed.On one hand, keeping the current implementation may be convenient because, more often than not when testing for an error, one is really just concerned with equality of the error messages as opposed to equality of other properties such as
Error.stack
. That said, the current behavior illustrated above can already be achieved more directly by comparing theError.message
strings. The current behavior also has the downside of being unexpected (even if it were documented) and ultimately being less flexible and limiting functionality. For instance, if one's goal were to compare two or more errors to see if they are the same error and occurred on the same line, the current implementation oftoEqual
might unexpectedly give a false positive because it only checks the messages for equality.I believe the better implementation is the one which offers more flexibility, does not limit functionality, and is consistent with the behavior expected by someone with a reasonable understanding of how an
Error
works.If we consider the API as defined by the documentation, such a fix would be a patch update. Though, given the nuance involved, fixing this behavior would more likely merit a major update.
Jest Version
Jest 20.0.0
The text was updated successfully, but these errors were encountered: