Jest: expect.objectContaining(...) mutates properties of the argument

Created on 23 Oct 2020  ยท  6Comments  ยท  Source: facebook/jest

๐Ÿ› 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

Bug Report Needs Repro Needs Triage

Most helpful comment

Hoping to have a fix for #10690 this weekend, but if not I'll cut a release without it

All 6 comments

This is the PR that seems to have broken it.
https://github.com/facebook/jest/pull/10508

Looks like we're mutating the object?

this.sample[property] = objectContaining(
  this.sample[property] as Record<string, unknown>,
);

[email protected]

On Fri, Oct 23, 2020, 5:24 PM Floyd May, notifications@github.com wrote:

๐Ÿ› 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

โ€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/facebook/jest/issues/10689, or unsubscribe
https://github.com/notifications/unsubscribe-auth/AOH4EJHBJZUZ3MHDMHATHGLSMHYCVANCNFSM4S5BVV5Q
.

Thank you all for addressing this so quickly! Do you know when I can expect a release that contains this fix?

today or tomorrow, need to fix #10690 first

fyi: for those waiting for a fix, for now, you can pin the expect package in the package.json :

  "resolutions": {
    "expect": "26.6.0"
  }

Hoping to have a fix for #10690 this weekend, but if not I'll cut a release without it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephenlautier picture stephenlautier  ยท  3Comments

ticky picture ticky  ยท  3Comments

kentor picture kentor  ยท  3Comments

gustavjf picture gustavjf  ยท  3Comments

jardakotesovec picture jardakotesovec  ยท  3Comments