diff --git a/CHANGELOG.md b/CHANGELOG.md index f563a15a2e70..f7877f54c223 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +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 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])