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

isEqual behavior given two Errors is inconsistent with documentation #3529

Closed
fvgs opened this issue May 9, 2017 · 4 comments · Fixed by renovatebot/renovate#207, renovatebot/renovate#208 or #3556

Comments

@fvgs
Copy link

fvgs commented May 9, 2017

Do you want to request a feature or report a bug?

Bug (in either the codebase or the documentation)

What is the current behavior?

const a = new Error ('foo')
const b = new Error ('foo')
// This succeeds
expect (a) . toEqual (b)

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 are Errors:

https://github.com/facebook/jest/blob/master/packages/jest-matchers/src/jasmine-utils.js#L72

if (a instanceof Error && b instanceof Error) {
  return a.message == b.message;
}

With the current implementation, toEqual will only check the message properties of the two Errors 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 the Error.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 of toEqual 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

@thymikee
Copy link
Collaborator

Would you like to improve the docs for that? :)
Until the next major release we can discuss if we'd like to change that behavior.

cpojer pushed a commit that referenced this issue May 12, 2017
* Updating docs for `.toEqual`
Fixes #3529

* Update ExpectAPI.md
@fvgs
Copy link
Author

fvgs commented May 12, 2017

@cpojer @thymikee reopen for discussion?

@thymikee
Copy link
Collaborator

What are your concerns?

@fvgs
Copy link
Author

fvgs commented Jun 18, 2017

@thymikee The concern is treating Errors as a special case when using isEqual to me seems like unexpected behavior. It also limits functionality for someone who may want to rely on the "regular" definition of deep equality.

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 Errors for equality (the behavior was undocumented at the time).

orta pushed a commit to orta/jest that referenced this issue Jul 7, 2017
* Updating docs for `.toEqual`
Fixes jestjs#3529

* Update ExpectAPI.md
tushardhole pushed a commit to tushardhole/jest that referenced this issue Aug 21, 2017
* Updating docs for `.toEqual`
Fixes jestjs#3529

* Update ExpectAPI.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants