diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e2f49e47c16..4ae06c8a4e65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ - `[expect]` Move typings of `.not`, `.rejects` and `.resolves` modifiers outside of `Matchers` interface ([#12346](https://github.com/facebook/jest/pull/12346)) - `[expect]` Throw useful error if `expect.extend` is called with invalid matchers ([#12488](https://github.com/facebook/jest/pull/12488)) +- `[expect]` Fix `iterableEquality` ignores other properties ([#8359](https://github.com/facebook/jest/pull/8359)) - `[jest-config]` Correctly detect CI environment and update snapshots accordingly ([#12378](https://github.com/facebook/jest/pull/12378)) - `[jest-config]` Pass `moduleTypes` to `ts-node` to enforce CJS when transpiling ([#12397](https://github.com/facebook/jest/pull/12397)) - `[jest-config, jest-haste-map]` Allow searching for tests in `node_modules` by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084)) diff --git a/packages/expect-utils/src/__tests__/utils.test.ts b/packages/expect-utils/src/__tests__/utils.test.ts index 08d6a2f03ed1..09b6574e9164 100644 --- a/packages/expect-utils/src/__tests__/utils.test.ts +++ b/packages/expect-utils/src/__tests__/utils.test.ts @@ -422,6 +422,49 @@ describe('iterableEquality', () => { ).toBe(false); }); + test('returns true when given iterator within equal objects', () => { + const a = { + [Symbol.iterator]: () => ({next: () => ({done: true})}), + a: [], + }; + const b = { + [Symbol.iterator]: () => ({next: () => ({done: true})}), + a: [], + }; + + expect(iterableEquality(a, b)).toBe(true); + }); + + test('returns false when given iterator within inequal objects', () => { + const a = { + [Symbol.iterator]: () => ({next: () => ({done: true})}), + a: [1], + }; + const b = { + [Symbol.iterator]: () => ({next: () => ({done: true})}), + a: [], + }; + + expect(iterableEquality(a, b)).toBe(false); + }); + + test('returns false when given iterator within inequal nested objects', () => { + const a = { + [Symbol.iterator]: () => ({next: () => ({done: true})}), + a: { + b: [1], + }, + }; + const b = { + [Symbol.iterator]: () => ({next: () => ({done: true})}), + a: { + b: [], + }, + }; + + expect(iterableEquality(a, b)).toBe(false); + }); + test('returns true when given circular Set shape', () => { const a1 = new Set(); const a2 = new Set(); diff --git a/packages/expect-utils/src/utils.ts b/packages/expect-utils/src/utils.ts index c8b0e48d9c77..7dbd4d124cbf 100644 --- a/packages/expect-utils/src/utils.ts +++ b/packages/expect-utils/src/utils.ts @@ -254,6 +254,12 @@ export const iterableEquality = ( return false; } + const aEntries = Object.entries(a); + const bEntries = Object.entries(b); + if (!equals(aEntries, bEntries)) { + return false; + } + // Remove the first value from the stack of traversed values. aStack.pop(); bStack.pop();