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 for 4286: Compare Maps and Sets by value rather than order #4303

Merged
merged 4 commits into from
Aug 20, 2017

Conversation

kaylie-alexa
Copy link
Contributor

Summary

Fix for #4286

Modify iterableEquality method so that, if two objects have the same constructor with .size property (i.e., Map and Set), check if the sizes are same. If not, exit early and return false. If yes, perform a union and if the size is still the same, return true. Otherwise, keep checking for deep equality (cases where the elements contain another object)

Test plan
Added test cases to matchers.test.js

screen shot 2017-08-19 at 4 59 46 pm

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@cpojer
Copy link
Member

cpojer commented Aug 20, 2017

Nice!! Do you mind removing the ... because we don't transform it and still support node 4? :)

@kaylie-alexa
Copy link
Contributor Author

d'oh! of course :)

@cpojer Do you think it's worth separating out iterableEquality to handle the nested comparison for maps/sets differently from arguments object and TypedArrays? Current implementation won't work for

expect(new Set([1, 2, {1: 'match'}])).toEqual(new Set([{1: 'match'}, 1, 2]));

because sets and maps set entries via === strict equality comparison. We'd have to iterate over each set again for every object to find and do a nested comparison. Thoughts?

@cpojer cpojer merged commit a61dd27 into jestjs:master Aug 20, 2017
@cpojer
Copy link
Member

cpojer commented Aug 20, 2017

Yep, I think that makes sense.

Thank you so much for this PR and fixing issues in Jest :)

@thomas-mcdonald
Copy link

Thanks for the fix!

tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
…s#4303)

* Add check for Maps & Sets

* check for presence of .size()

* exit early if size doesn't equal

* replace spread with args array
nigelzor added a commit to nigelzor/jest that referenced this pull request Aug 31, 2017
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.
@nigelzor nigelzor mentioned this pull request Aug 31, 2017
nigelzor added a commit to nigelzor/jest that referenced this pull request Sep 13, 2017
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.
cpojer pushed a commit that referenced this pull request Sep 14, 2017
* fix Map equality test

#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
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