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

improved failure message readability of failing ObjectContaining tests #15038

Merged
merged 3 commits into from May 12, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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