Skip to content

Commit 9a07781

Browse files
authored
fix(expect): objectContaining should recurse into sub-objects (jestjs#10508)
1 parent 30e8020 commit 9a07781

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
### Fixes
88

9-
- `[jest-mock]` Allow to mock methods in getters (TypeScript 3.9 export)
9+
- `[expect]` Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508))
10+
- `[jest-mock]` Allow to mock methods in getters (TypeScript 3.9 export) ([#10156](https://github.com/facebook/jest/pull/10156))
1011

1112
### Chore & Maintenance
1213

packages/expect/src/__tests__/asymmetricMatchers.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ test('ObjectContaining matches', () => {
163163
objectContaining({}).asymmetricMatch('jest'),
164164
objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}),
165165
objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}),
166+
objectContaining({foo: {bar: [1]}}).asymmetricMatch({
167+
foo: {bar: [1], qux: []},
168+
}),
166169
objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({
167170
first: {second: {}},
168171
}),

packages/expect/src/asymmetricMatchers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
178178
return true;
179179
} else {
180180
for (const property in this.sample) {
181+
if (
182+
typeof this.sample[property] === 'object' &&
183+
!(this.sample[property] instanceof AsymmetricMatcher)
184+
) {
185+
this.sample[property] = objectContaining(
186+
this.sample[property] as Record<string, unknown>,
187+
);
188+
}
189+
181190
if (
182191
!hasProperty(other, property) ||
183192
!equals(this.sample[property], other[property])

0 commit comments

Comments
 (0)