jest Symbol equality is broken

Created on 3 Oct 2017  路  10Comments  路  Source: facebook/jest

bug?

What is the current behavior?

jest equality for object with Symbol key is broken.

{ } == { [Symbol("jest")]: 2 }

Example

What is the expected behavior?

{ } != { [Symbol("jest")]: 2 }
Bug

Most helpful comment

This problem seems to persist with expect.objectContaining:

expect({ x: 42}).toEqual(expect.objectContaining({ [Symbol('asd')]: expect.anything() }))

passes, while it shouldn't

All 10 comments

Would you like fix this? :)

I can give it a shot next week.

What's the cause, and is it related to #4603 ?

@cowboyd it actually may be related

@itaied246 any idea what a possible solution might be?

I have search for the cause for a while, and there is a weird behavior for Symbol.
JSON.stringify ignores Symbol keys. Also Object.keys ignore Symbol.
So, any comparison for keys will skip, and result this error.

Check out this.

lodash isEqual function supports Symbol, so we might use this instead of the built in jest equality for Symbol.
What do you think?

@itaied246 Jest uses pretty-format to Stringify any value.

Looks like this isn't a bug in the Jest latest version

jest - v21.2.1, node - v8.8.1

screen shot 2017-10-27 at 11 46 46 pm

This problem seems to persist with expect.objectContaining:

expect({ x: 42}).toEqual(expect.objectContaining({ [Symbol('asd')]: expect.anything() }))

passes, while it shouldn't

The issue in the OP is fixed, and the other issue can be tracked at #6466

Was this page helpful?
0 / 5 - 0 ratings