jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.16k stars 6.45k forks source link

[Bug]: Wrong visual diff for non-matching Symbols and Functions and matching asymmetric matcher in toHaveBeenCalledWith #12077

Open wolfgangwalther opened 2 years ago

wolfgangwalther commented 2 years ago

Version

27.3.1

Steps to reproduce

  test('expected/received diff mock fn', () => {
    const mock = jest.fn()
    mock({
      sym: Symbol('hello'),
      fn: () => 'world',
      obj: {}
    })

    expect(mock).toHaveBeenCalledWith({
      sym: Symbol('hello'),
      fn: () => 'world',
      obj: expect.any(Object)
    })
  })

Expected behavior

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

      Object {
    -   "fn": [Function fn],
    +   "fn": [Function fn],
        "obj": Any<Object>,
    -   "sym": Symbol(hello),
    +   "sym": Symbol(hello),
      },

Actual behavior

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

      Object {
        "fn": [Function fn],
    -   "obj": Any<Object>,
    +   "obj": Object {},
        "sym": Symbol(hello),
      },

Additional context

There's two things happening here:

Doing the same with just expect({ ... }).toMatchObject({ ... }) instead of toHaveBeenCalledWith is showing a slightly more helpful less misleading diff:

    Expected: {"fn": [Function fn], "obj": Any<Object>, "sym": Symbol(hello)}
    Received: serializes to the same string

It would be even more helpful to have the multi-line output as seen above, though - of course, only when the diff is actually correct... ;)

Environment

System:
    OS: Linux 5.14 Alpine Linux
    CPU: (32) x64 AMD Ryzen 9 3950X 16-Core Processor
  Binaries:
    Node: 16.11.1 - /usr/local/bin/node
    Yarn: 1.22.15 - /usr/local/bin/yarn
    npm: 8.0.0 - /usr/local/bin/npm
  npmPackages:
    jest: 27.3.1 => 27.3.1
github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

wolfgangwalther commented 1 year ago

Still an issue with jest 29.3.1

github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

wolfgangwalther commented 6 months ago

Still an issue with jest 29.7.0