Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

entriesEqual breaks on sets of objects with null prototype #51

Open
demurgos opened this issue Jan 30, 2018 · 0 comments
Open

entriesEqual breaks on sets of objects with null prototype #51

demurgos opened this issue Jan 30, 2018 · 0 comments

Comments

@demurgos
Copy link

demurgos commented Jan 30, 2018

The last line of entriesEqual throws the following error in my test cases:

TypeError: Cannot convert object to primitive value
    at Array.toString (native)
    at Array.sort (native)
[...]

Here is how I can reproduce it with chai:

function getAnimals() {
  const duck = Object.create(null);
  duck.name = "duck";
  const cat = Object.create(null);
  cat.name = "cat";
  return new Set([duck, cat]);
}

const actual = getAnimals();
const expected = new Set([{name: "duck"}, {name: "cat"}]);

assert.deepEqual(actual, expected);

The error is caused by the fact that the objects with a null prototype cannot be converted to strings implicitly, and the array sort function performs a lexicographical sort on strings so it first tries to convert them to strings.

(Tested on Node 9.4)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant