deep-eql
deep-eql copied to clipboard
`entriesEqual` breaks on sets of objects with null prototype
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)