Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ecraig12345 opened this issue Feb 11, 2025 · 0 comments · May be fixed by #15508
Open

Comments

@ecraig12345
Copy link

ecraig12345 commented Feb 11, 2025

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant