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

toEqual Map/Set sort order with object keys/values #6138

Closed
chrisblossom opened this issue May 5, 2018 · 4 comments
Closed

toEqual Map/Set sort order with object keys/values #6138

chrisblossom opened this issue May 5, 2018 · 4 comments

Comments

@chrisblossom
Copy link
Contributor

chrisblossom commented May 5, 2018

🐛 Bug Report

Set as an object and Map with an object key does not ignore sort order.

Relevant issues and PR: #4286 #4303 (comment)

To Reproduce

test('Map order does not matter with arrayed keys', () => {
    const example = new Map([
        [['one'], 1],
        [['two'], 2],
        [['three'], 3],
    ]);

    const expected = new Map([
        [['three'], 3],
        [['one'], 1],
        [['two'], 2],
    ]);

    expect(example).toEqual(expected);
});

test('Set order does not matter with arrays', () => {
    const example = new Set([
        ['one'],
        ['two'],
        ['three'],
    ]);

    const expected = new Set([
        ['three'],
        ['one'],
        ['two'],
    ]);

    expect(example).toEqual(expected);
});

Expected behavior

Sort order does not matter. Both above tests pass.

Link to repl or repo (highly encouraged)

https://github.com/chrisblossom/jest-issue-6138

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.3
    CPU: x64 Intel(R) Core(TM) i7-4960HQ CPU @ 2.60GHz
  Binaries:
    Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
    Yarn: 1.6.0 - ~/.yarn/bin/yarn
    npm: 5.6.0 - ~/.nvm/versions/node/v8.9.4/bin/npm
  npmPackages:
    jest: 22.4.3 => 22.4.3
@donysukardi
Copy link

donysukardi commented May 7, 2018

The test cases presented seem straightforward, i.e. no duplicate deep equal array or object.

However, there are some edge cases when dealing with the presence of such values in a data structure where order does not matter, i.e. in Set and Map.

For instance,

const expected = new Set([
  [1],
  [2],
  [2]
])

const actual = new Set([
  [2],
  [1],
  [2]
])

What would be the expected outcome for the above comparison? I checked them out using Lodash and Underscore’s isEqual, both gave me different results.

@chrisblossom
Copy link
Contributor Author

@donysukardi I am a bit confused by your response. These tests are all regarding Set and Map. For your example, I would expect them to be equal because the following is considered equal:

const expected = new Set([
  1,
  2,
  2
])

const actual = new Set([
  2,
  1,
  2
])

See #4286 and #4303 for more reasoning why this is the case.

@SimenB
Copy link
Member

SimenB commented May 22, 2018

#6150

@SimenB SimenB closed this as completed May 22, 2018
@github-actions
Copy link

This issue 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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants