From 46b0838d580f4e0854f1f638ad31ad3ec5facd89 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 2 Nov 2020 11:25:28 +0100 Subject: [PATCH 1/7] Revert "fix: don't mutate the sample in expect.objectContaining() (#10711)" This reverts commit f84a807236183c850eea42ce98e860a3a7ac5ee7. --- CHANGELOG.md | 1 - .../expect/src/__tests__/asymmetricMatchers.test.ts | 8 -------- packages/expect/src/asymmetricMatchers.ts | 11 +++++++---- 3 files changed, 7 insertions(+), 13 deletions(-) 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; } From e0e840661fc7698e1d68216a279392846f46869c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 2 Nov 2020 11:26:20 +0100 Subject: [PATCH 2/7] Revert "fix(expect): `objectContaining` should recurse into sub-objects (#10508)" This reverts commit 9a077810337b7536cbff95377386dfa2e463857f. --- CHANGELOG.md | 1 + packages/expect/src/__tests__/asymmetricMatchers.test.ts | 3 --- packages/expect/src/asymmetricMatchers.ts | 9 --------- 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8f0141ed5dc..ce1a965197d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `[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]` Revert "Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508))" - `[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 60979b799e82..2f5a7ef9dbc7 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -162,9 +162,6 @@ test('ObjectContaining matches', () => { objectContaining({}).asymmetricMatch('jest'), objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}), objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}), - objectContaining({foo: {bar: [1]}}).asymmetricMatch({ - foo: {bar: [1], qux: []}, - }), objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({ first: {second: {}}, }), diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts index ef1e38d328d5..b20f3052ca97 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -177,15 +177,6 @@ class ObjectContaining extends AsymmetricMatcher> { return true; } else { for (const property in this.sample) { - if ( - typeof this.sample[property] === 'object' && - !(this.sample[property] instanceof AsymmetricMatcher) - ) { - this.sample[property] = objectContaining( - this.sample[property] as Record, - ); - } - if ( !hasProperty(other, property) || !equals(this.sample[property], other[property]) From 6fbbd56f26839ac2452931b3adeff913925b7728 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 2 Nov 2020 11:28:38 +0100 Subject: [PATCH 3/7] keep some tests --- .../expect/src/__tests__/asymmetricMatchers.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index 2f5a7ef9dbc7..81bed3166a1b 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -165,6 +165,10 @@ test('ObjectContaining matches', () => { objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({ first: {second: {}}, }), + objectContaining({foo: Buffer.from('foo')}).asymmetricMatch({ + foo: Buffer.from('foo'), + jest: 'jest', + }), ].forEach(test => { jestExpect(test).toEqual(true); }); @@ -207,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 60201d59537736c37ed30ee319e0a3f93af1964b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 2 Nov 2020 11:30:51 +0100 Subject: [PATCH 4/7] link to PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce1a965197d9..e50bf7aa46b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - `[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]` Revert "Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508))" +- `[expect]` Revert "Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508))" ([#10760](https://github.com/facebook/jest/pull/10760)) - `[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)) From 58d9b436b54952f1206760262286c3f3700013a6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 2 Nov 2020 11:37:12 +0100 Subject: [PATCH 5/7] link correctly... --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e50bf7aa46b7..1e0e2ed1dc93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - `[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]` Revert "Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508))" ([#10760](https://github.com/facebook/jest/pull/10760)) +- `[expect]` Revert "Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508))" ([#10766](https://github.com/facebook/jest/pull/10766)) - `[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)) From 34c710efbb8e27702e928739422fc13d2715b6bb Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 2 Nov 2020 11:59:46 +0100 Subject: [PATCH 6/7] update assertions --- packages/babel-jest/src/__tests__/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/babel-jest/src/__tests__/index.ts b/packages/babel-jest/src/__tests__/index.ts index 99fd909be5b4..4ee2960bd65e 100644 --- a/packages/babel-jest/src/__tests__/index.ts +++ b/packages/babel-jest/src/__tests__/index.ts @@ -93,7 +93,14 @@ describe('caller option correctly merges from defaults and options', () => { expect(loadPartialConfig).toHaveBeenCalledTimes(1); expect(loadPartialConfig).toHaveBeenCalledWith( - expect.objectContaining({caller: {name: 'babel-jest', ...output}}), + expect.objectContaining({ + caller: { + name: 'babel-jest', + ...output, + supportsExportNamespaceFrom: false, + supportsTopLevelAwait: false, + }, + }), ); }); }); @@ -110,7 +117,9 @@ test('can pass null to createTransformer', () => { caller: { name: 'babel-jest', supportsDynamicImport: false, + supportsExportNamespaceFrom: false, supportsStaticESM: false, + supportsTopLevelAwait: false, }, }), ); From cda33c9c71cccf6790922f2ac366d466bede4840 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 2 Nov 2020 12:20:31 +0100 Subject: [PATCH 7/7] chore: add one more assertion for non-merging behavior --- packages/expect/src/__tests__/asymmetricMatchers.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index 81bed3166a1b..6e1886b65409 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -179,6 +179,10 @@ test('ObjectContaining does not match', () => { objectContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}), objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}), objectContaining({foo: undefined}).asymmetricMatch({}), + objectContaining({ + answer: 42, + foo: {bar: 'baz', foobar: 'qux'}, + }).asymmetricMatch({foo: {bar: 'baz'}}), ].forEach(test => { jestExpect(test).toEqual(false); });