Skip to content

Commit

Permalink
fix: improve failure message of failing ObjectContaining tests (#15038
Browse files Browse the repository at this point in the history
)
  • Loading branch information
colinacassidy committed May 12, 2024
1 parent 7f2f96b commit 169ba83
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
- `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038))
- `[jest-changed-files]` Print underlying errors when VCS commands fail ([#15052](https://github.com/jestjs/jest/pull/15052))
- `[jest-changed-files]` Abort `sl root` call if output resembles a steam locomotive ([#15053](https://github.com/jestjs/jest/pull/15053))
- `[jest-circus]` [**BREAKING**] Prevent false test failures caused by promise rejections handled asynchronously ([#14315](https://github.com/jestjs/jest/pull/14315))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2176,13 +2176,13 @@ exports[`.toEqual() {pass: false} expect({"a": 1, "b": 2}).toEqual(ObjectContain
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>

<g>- Expected - 2</>
<r>+ Received + 3</>
<r>+ Received + 2</>

<g>- ObjectContaining {</>
<g>- "a": 2,</>
<r>+ Object {</>
<r>+ "a": 1,</>
<r>+ "b": 2,</>
<d> "b": 2,</>
<d> }</>
`;

Expand Down
8 changes: 8 additions & 0 deletions packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,19 @@ class ObjectContaining extends AsymmetricMatcher<
const matcherContext = this.getMatcherContext();
const objectKeys = getObjectKeys(this.sample);

const otherKeys = other ? getObjectKeys(other) : [];

for (const key of objectKeys) {
if (
!hasProperty(other, key) ||
!equals(this.sample[key], other[key], matcherContext.customTesters)
) {
// Result has already been determined, mutation only affects diff output
for (const key of otherKeys) {
if (!hasProperty(this.sample, key)) {
this.sample[key] = other[key];
}
}
result = false;
break;
}
Expand Down

0 comments on commit 169ba83

Please sign in to comment.