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

fix Map equality test #4404

Merged
merged 2 commits into from
Sep 14, 2017
Merged

fix Map equality test #4404

merged 2 commits into from
Sep 14, 2017

Conversation

nigelzor
Copy link
Contributor

Summary

This code should throw, but currently does not:

expect(new Map([[1, 1]])).toEqual(new Map([[1, 2]]));

#4303 added code to check Set equality in an order-independent way, but applied that logic to all iterable objects. This change limits the scope of the special-case to just Set and Map, and corrects the Map case to test that both keys and values are equal. The order-independent checks don't do deep equality on keys, but if not equal fall through to the order-dependent deep check (as before).

Test plan
Added additional tests for the toEqual() matcher, and ran yarn test. Tests failed before the change, and now pass.

@@ -114,16 +114,29 @@ export const iterableEquality = (a: any, b: any) => {
if (a.size !== undefined) {
if (a.size !== b.size) {
return false;
} else {
const args = [];
} else if (a instanceof Set) {
Copy link
Member

Choose a reason for hiding this comment

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

With this change, this branch will pass if b is a superset of a, which I don't think is intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see how that follows. b must contain all the elements of a, and be the same size. The size constraint should preclude a superset from matching (and there is a test for that).

Copy link
Member

Choose a reason for hiding this comment

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

Good point, I didn't realize that we had this check up there.

if (allFound) {
return true;
}
} else if (a instanceof Map) {
Copy link
Member

@cpojer cpojer Sep 12, 2017

Choose a reason for hiding this comment

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

Same here.

@cpojer
Copy link
Member

cpojer commented Sep 12, 2017

I think the new equality check isn't complete. If possible, can we find a way not to use instanceof for Set and Map and detect them in a different way?

@nigelzor
Copy link
Contributor Author

What would you prefer to instanceof?

  • a.constructor === Set would rule out subclasses
  • isA('Set', a) would allow objects from a different window (and subclasses)

@cpojer
Copy link
Member

cpojer commented Sep 13, 2017

The latter, we may end up with objects from different contexts.

jestjs#4303 added code to check Set equality in an order-independent way, but
applied that logic to all iterable objects. This change limits the scope of
the special-case to just Set and Map, and corrects the Map case to test
that both keys and values are equal.
@codecov-io
Copy link

Codecov Report

Merging #4404 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4404   +/-   ##
=======================================
  Coverage   56.84%   56.84%           
=======================================
  Files         186      186           
  Lines        6298     6298           
  Branches        3        3           
=======================================
  Hits         3580     3580           
  Misses       2717     2717           
  Partials        1        1

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c4844a6...a8988c6. Read the comment docs.

@cpojer cpojer merged commit 6414c5d into jestjs:master Sep 14, 2017
@cpojer
Copy link
Member

cpojer commented Sep 14, 2017

Thanks for working on this.

tabrindle pushed a commit to tabrindle/jest that referenced this pull request Oct 2, 2017
* fix Map equality test

jestjs#4303 added code to check Set equality in an order-independent way, but
applied that logic to all iterable objects. This change limits the scope of
the special-case to just Set and Map, and corrects the Map case to test
that both keys and values are equal.

* use isA instead of instanceof
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants