From c451bd5994d6bceb21164b1205b2bb069c668e4f Mon Sep 17 00:00:00 2001 From: ioancole Date: Mon, 14 Sep 2020 08:51:16 +0800 Subject: [PATCH 1/2] Make objectContaining recursive into sub-objects --- CHANGELOG.md | 1 + packages/expect/src/__tests__/asymmetricMatchers.test.ts | 3 +++ packages/expect/src/asymmetricMatchers.ts | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f563a15a2e70..dd1e7608e15c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes - `[jest-mock]` Allow to mock methods in getters (TypeScript 3.9 export) +- `[expect]` Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508)) ### Chore & Maintenance diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index 2474382c9ef8..acdcab7b2189 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -163,6 +163,9 @@ 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 a12436160f96..dd0026bc72de 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -178,6 +178,15 @@ 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 db2502726feec4019a1d8424285ec56b73779815 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 20 Oct 2020 09:34:43 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd1e7608e15c..f7877f54c223 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,8 @@ ### Fixes -- `[jest-mock]` Allow to mock methods in getters (TypeScript 3.9 export) - `[expect]` Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508)) +- `[jest-mock]` Allow to mock methods in getters (TypeScript 3.9 export) ([#10156](https://github.com/facebook/jest/pull/10156)) ### Chore & Maintenance