Skip to content

toEqual assertion does not work with objects where the values are anonymous functions #8166

@rmoskal

Description

@rmoskal

🐛 Bug Report

toEqual assertion does not work with objects where the values are anonymous functions, like this:

  const foo = {a:_in=>_in}
  const bar = {a:_in=>_in}

It really surprised me.

To Reproduce

Write a test like this:

describe('toEqual', () => {
  it('should handle an anonymous function', () => {
    const foo = {a:_in=>_in}
    const bar = {a:_in=>_in}
    expect(foo).toEqual(bar)
  })
})

Expected behavior

I expected the following two objects to be equal:

const foo = {a:_in=>_in}
const bar = {a:_in=>_in}

Link to repl or repo (highly encouraged)

https://repl.it/repls/DefinitiveMintyHypercard

Paste the results here:

Jest v22.1.2 node v7.4.0 linux/amd64
 FAIL  ./toEqual-test.js
  toEqual
    ✓ handles a simple object (5ms)
    ✓ handles a function as object value
    ✕ shuld handle an anonymous function (9ms)

  ● toEqual › shuld handle an anonymous function

    expect(received).toEqual(expected)

    Expected value to equal:
      {"a": [Function a]}
    Received:
      {"a": [Function a]}

    Difference:

    Compared values have no visual difference.

      16 |   const foo = {a:_in=>identity(_in)}
      17 |   const bar = {a:_in=>identity(_in)}
    > 18 |   expect(foo).toEqual(bar)
      19 | })
      20 | })
      21 |

      at Object.it (toEqual-test.js:18:15)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 2 passed, 3 total
Snapshots:   0 total
Time:        0.978s, estimated 1s
Ran all test suites.
exit status 1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions