diff --git a/CHANGELOG.md b/CHANGELOG.md index c652a32acd75..e8f0141ed5dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,6 @@ - `[babel-plugin-jest-hoist]` Preserve order of hoisted mock nodes within containing block ([#10536](https://github.com/facebook/jest/pull/10536)) - `[babel-plugin-jest-hoist]` Hoist pure constants to support experimental JSX transform in hoisted mocks ([#10723](https://github.com/facebook/jest/pull/10723)) - `[babel-preset-jest]` Update `babel-preset-current-node-syntax` to support top level await ([#10747](https://github.com/facebook/jest/pull/10747)) -- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711)) - `[jest-circus, jest-jasmine2]` fix: don't assume `stack` is always a string ([#10697](https://github.com/facebook/jest/pull/10697)) - `[jest-config]` Fix bug introduced in watch mode by PR [#10678](https://github.com/facebook/jest/pull/10678/files#r511037803) ([#10692](https://github.com/facebook/jest/pull/10692)) - `[jest-config]` Throw correct error for missing preset modules ([#10737](https://github.com/facebook/jest/pull/10737)) diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index 9da8f2cda5c6..60979b799e82 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -210,14 +210,6 @@ test('ObjectContaining throws for non-objects', () => { jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow(); }); -test('ObjectContaining does not mutate the sample', () => { - const sample = {foo: {bar: {}}}; - const sample_json = JSON.stringify(sample); - expect({foo: {bar: {}}}).toEqual(expect.objectContaining(sample)); - - expect(JSON.stringify(sample)).toEqual(sample_json); -}); - test('ObjectNotContaining matches', () => { [ objectNotContaining({}).asymmetricMatch('jest'), diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts index de604b205f13..ef1e38d328d5 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -177,15 +177,18 @@ class ObjectContaining extends AsymmetricMatcher> { return true; } else { for (const property in this.sample) { - const expected = + if ( typeof this.sample[property] === 'object' && !(this.sample[property] instanceof AsymmetricMatcher) - ? objectContaining(this.sample[property] as Record) - : this.sample[property]; + ) { + this.sample[property] = objectContaining( + this.sample[property] as Record, + ); + } if ( !hasProperty(other, property) || - !equals(expected, other[property]) + !equals(this.sample[property], other[property]) ) { return false; }