From 30e5dc1b832ab6e2dd204056ce98e66abde23b9f Mon Sep 17 00:00:00 2001 From: Milan Suk Date: Mon, 29 Apr 2024 13:24:55 +0200 Subject: [PATCH] fix: backport jest iterable equality within object (#5621) --- packages/expect/src/jest-utils.ts | 5 ++++ test/core/test/expect.test.ts | 45 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/packages/expect/src/jest-utils.ts b/packages/expect/src/jest-utils.ts index 3358ca39bed1..20e57ad31556 100644 --- a/packages/expect/src/jest-utils.ts +++ b/packages/expect/src/jest-utils.ts @@ -411,6 +411,11 @@ export function iterableEquality(a: any, b: any, customTesters: Array = if (!bIterator.next().done) 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() diff --git a/test/core/test/expect.test.ts b/test/core/test/expect.test.ts index 5a131baae512..116328dd7824 100644 --- a/test/core/test/expect.test.ts +++ b/test/core/test/expect.test.ts @@ -391,3 +391,48 @@ describe('Error equality', () => { } }) }) + +describe('iterator', () => { + 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(a).toStrictEqual(b) + }) + + 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(a).not.toStrictEqual(b) + }) + + 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(a).not.toStrictEqual(b) + }) +})