Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect): objectContaining should recurse into sub-objects #10508

Merged
merged 2 commits into from Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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