Skip to content

Commit

Permalink
fix(expect): objectContaining should recurse into sub-objects (#10508)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioancole committed Oct 20, 2020
1 parent 30e8020 commit 9a07781
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Expand Up @@ -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: {}},
}),
Expand Down
9 changes: 9 additions & 0 deletions packages/expect/src/asymmetricMatchers.ts
Expand Up @@ -178,6 +178,15 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
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<string, unknown>,
);
}

if (
!hasProperty(other, property) ||
!equals(this.sample[property], other[property])
Expand Down

0 comments on commit 9a07781

Please sign in to comment.