Skip to content

toEqual does not treat non-enumerable properties equally #6392

@mweststrate

Description

@mweststrate

🐛 Bug Report

I discovered some inconsistent behavior in the toEqual matcher in Jest; when comparing to objects, it ignores non-enumerable properties, but only if they are not symbols. This leads to quite inconsistent behavior:

This test passes:

const actual1 = {
        x: 3
}
Object.defineProperty(actual1, "test", {
        enumerable: false,
        value: 5
})
expect(actual1).toEqual({ x: 3 })

But this one doesn't:

const actual2 = {
        x: 3
}
const mySymbol = Symbol("test")
Object.defineProperty(actual2, mySymbol, {
        enumerable: false,
        value: 5
})
expect(actual2).toEqual({ x: 3 })

Where one would expect both to behave the same.

The cause of the issue can be found here. To find the string properties the for..in loop is used which ignores non-enumerable properties, but for the symbolic properties getOwnPropertySymbols is used, which will include non-enumerable properties.

To Reproduce

See link below

Expected behavior

Non-enumerable symbolic properties should be treated the same as non-enumerable non-symbolic properties when running toEqual. Either both should be included, or excluded. Given that symbolic properties are lot more exotic, I think the behavior of string properties should be followed and non-enumerable symbolic properties should be excluded from deep equality.

I am willing to file a PR myself

Link to repl or repo (highly encouraged)

https://repl.it/@mweststrate/IckyMeanProgrammers

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: Linux 4.15 Ubuntu 18.04 LTS (Bionic Beaver)
    CPU: x64 Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
  Binaries:
    Node: 9.3.0 - ~/.nvm/versions/node/v9.3.0/bin/node
    Yarn: 1.6.0 - /usr/bin/yarn
    npm: 5.5.1 - ~/.nvm/versions/node/v9.3.0/bin/npm
  npmPackages:
    @types/jest: ^21.1.9 => 21.1.10 
    jest: ^22.0.4 => 22.4.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