Skip to content

Commit 169ba83

Browse files
fix: improve failure message of failing ObjectContaining tests (#15038)
1 parent 7f2f96b commit 169ba83

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
4242
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
43+
- `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038))
4344
- `[jest-changed-files]` Print underlying errors when VCS commands fail ([#15052](https://github.com/jestjs/jest/pull/15052))
4445
- `[jest-changed-files]` Abort `sl root` call if output resembles a steam locomotive ([#15053](https://github.com/jestjs/jest/pull/15053))
4546
- `[jest-circus]` [**BREAKING**] Prevent false test failures caused by promise rejections handled asynchronously ([#14315](https://github.com/jestjs/jest/pull/14315))

packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,13 +2176,13 @@ exports[`.toEqual() {pass: false} expect({"a": 1, "b": 2}).toEqual(ObjectContain
21762176
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>
21772177

21782178
<g>- Expected - 2</>
2179-
<r>+ Received + 3</>
2179+
<r>+ Received + 2</>
21802180

21812181
<g>- ObjectContaining {</>
21822182
<g>- "a": 2,</>
21832183
<r>+ Object {</>
21842184
<r>+ "a": 1,</>
2185-
<r>+ "b": 2,</>
2185+
<d> "b": 2,</>
21862186
<d> }</>
21872187
`;
21882188

packages/expect/src/asymmetricMatchers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,19 @@ class ObjectContaining extends AsymmetricMatcher<
243243
const matcherContext = this.getMatcherContext();
244244
const objectKeys = getObjectKeys(this.sample);
245245

246+
const otherKeys = other ? getObjectKeys(other) : [];
247+
246248
for (const key of objectKeys) {
247249
if (
248250
!hasProperty(other, key) ||
249251
!equals(this.sample[key], other[key], matcherContext.customTesters)
250252
) {
253+
// Result has already been determined, mutation only affects diff output
254+
for (const key of otherKeys) {
255+
if (!hasProperty(this.sample, key)) {
256+
this.sample[key] = other[key];
257+
}
258+
}
251259
result = false;
252260
break;
253261
}

0 commit comments

Comments
 (0)