From 034c5f9fd58dc99fe3b83084e12165d73c5e046e Mon Sep 17 00:00:00 2001 From: ninevra Date: Mon, 26 Oct 2020 01:31:30 -0700 Subject: [PATCH 1/3] Test that ObjectContaining does not mutate the sample --- packages/expect/src/__tests__/asymmetricMatchers.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index acdcab7b2189..c17ca610d599 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -211,6 +211,14 @@ 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'), From 40fd62658839011c2a284e9a00e8f6c56728545f Mon Sep 17 00:00:00 2001 From: ninevra Date: Mon, 26 Oct 2020 01:31:46 -0700 Subject: [PATCH 2/3] Stop mutating the sample in ObjectContaining --- .../expect/src/__tests__/asymmetricMatchers.test.ts | 4 ++-- packages/expect/src/asymmetricMatchers.ts | 11 ++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index c17ca610d599..dac1a7c1b804 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -212,9 +212,9 @@ test('ObjectContaining throws for non-objects', () => { }); test('ObjectContaining does not mutate the sample', () => { - const sample = { foo: { bar: {} } }; + const sample = {foo: {bar: {}}}; const sample_json = JSON.stringify(sample); - expect({ foo: { bar: {} } }).toEqual(expect.objectContaining(sample)); + expect({foo: {bar: {}}}).toEqual(expect.objectContaining(sample)); expect(JSON.stringify(sample)).toEqual(sample_json); }); diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts index dd0026bc72de..280ebc9d5fe2 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -178,18 +178,15 @@ class ObjectContaining extends AsymmetricMatcher> { return true; } else { for (const property in this.sample) { - if ( + const expected = typeof this.sample[property] === 'object' && !(this.sample[property] instanceof AsymmetricMatcher) - ) { - this.sample[property] = objectContaining( - this.sample[property] as Record, - ); - } + ? objectContaining(this.sample[property] as Record) + : this.sample[property]; if ( !hasProperty(other, property) || - !equals(this.sample[property], other[property]) + !equals(expected, other[property]) ) { return false; } From 60e9f3dd8355b7bf4b9554c833170789a23eee3d Mon Sep 17 00:00:00 2001 From: ninevra Date: Mon, 26 Oct 2020 01:57:33 -0700 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa8314e1b95e..bc4c3c1f8bde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Fixes +- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711)) + ### Chore & Maintenance - `[jest-cli]` chore: standardize files and folder names ([#10698](https://github.com/facebook/jest/pull/1098))