diff --git a/CHANGELOG.md b/CHANGELOG.md index 1de4313f1baf..afcaa6cb3e51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 31c26a85f106..c0e737dc4685 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -2176,13 +2176,13 @@ exports[`.toEqual() {pass: false} expect({"a": 1, "b": 2}).toEqual(ObjectContain expect(received).toEqual(expected) // deep equality - Expected - 2 -+ Received + 3 ++ Received + 2 - ObjectContaining { - "a": 2, + Object { + "a": 1, -+ "b": 2, + "b": 2, } `; diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts index dfb605cb17a3..7a52e4f8b2e2 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -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; }