Skip to content

expect.objectContaining(...) mutates properties of the argument #10689

Closed
@floyd-may

Description

@floyd-may

🐛 Bug Report

When upgrading from react-scripts version 3.4.3 to version 4.0.0, jest was also upgraded. Tests that were behaving reasonably began failing. Upon closer inspection, I discovered that expect.objectContaining(...) was mutating properties of the argument! For example, this code would fail in the indicated location:

test('adds 1 + 2 to equal 3', () => {
  const thing = { foo: [{ val: 1}], bar: 2 };
  const originalThing = { ...thing };

  const json = JSON.stringify(originalThing);
  expect(originalThing).toEqual(expect.objectContaining(thing));

  expect(json).toEqual(JSON.stringify(thing)); // kaboom!
});

Expected behavior

The argument to objectContaining(...) is not mutated.

Link to repl or repo (highly encouraged)

Note: this link contains the very code described above 😄

https://repl.it/repls/TomatoVengefulAngles#example.test.js

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