Skip to content

entriesEqual breaks on sets of objects with null prototype #51

Open
@demurgos

Description

@demurgos

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions