Skip to content

[Bug]: Incorrect comparison with calledWith(objectContaining) and multiple calls (reopening) #15501

Open
@ecraig12345

Description

@ecraig12345

This was originally opened by @glenjamin in #15342, but it was closed due to inactivity.

Version

30.0.0-alpha.7

Steps to reproduce

describe('something', () => {
  it('should work', () => {
    const f = jest.fn();
    f({ a: 1, b: 2 });
    f({ a: 3, b: 7 });

    expect(f).toHaveBeenCalledWith(expect.objectContaining({ b: 7 }));
  });
});

Expected behavior

Test passes

Actual behavior

 FAIL  ./demo.test.js
  something
    ✕ should work (38 ms)

  ● something › should work

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

    Expected: ObjectContaining {"a": 1, "b": 7}
    Received
           1: {"a": 1, "b": 2}
           2: {"a": 3, "b": 7}

    Number of calls: 2

       5 |     f({ a: 3, b: 7 });
       6 |
    >  7 |     expect(f).toHaveBeenCalledWith(expect.objectContaining({ b: 7 }));
         |               ^
       8 |   });
       9 | });
      10 |

      at Object.toHaveBeenCalledWith (demo.test.js:7:15)

Additional context

@glenjamin's comment on the original issue #15342:

I've traced this back to 169ba83#diff-f8846ea7aae48b4baadb9c50ba129cdadc6783e6051e605b5e2827153fc04479R253

The assumption that this.sample is safe to modify is incorrect.

I expect that this will fail in any scenario where objectContaining is compared against multiple values

cc @colinacassidy who supplied the previous fix

Environment

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 18.20.1
    Yarn: 4.4.0
    npm: 9.9.3
    pnpm: 9.1.0
  npmPackages:
    jest: ^30.0.0-alpha.5 => 30.0.0-alpha.7

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