Skip to content

Commit 30e5dc1

Browse files
authoredApr 29, 2024··
fix: backport jest iterable equality within object (#5621)
1 parent 08c79d6 commit 30e5dc1

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
 

‎packages/expect/src/jest-utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ export function iterableEquality(a: any, b: any, customTesters: Array<Tester> =
411411
if (!bIterator.next().done)
412412
return false
413413

414+
const aEntries = Object.entries(a)
415+
const bEntries = Object.entries(b)
416+
if (!equals(aEntries, bEntries))
417+
return false
418+
414419
// Remove the first value from the stack of traversed values.
415420
aStack.pop()
416421
bStack.pop()

‎test/core/test/expect.test.ts

+45
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,48 @@ describe('Error equality', () => {
391391
}
392392
})
393393
})
394+
395+
describe('iterator', () => {
396+
test('returns true when given iterator within equal objects', () => {
397+
const a = {
398+
[Symbol.iterator]: () => ({ next: () => ({ done: true }) }),
399+
a: [],
400+
}
401+
const b = {
402+
[Symbol.iterator]: () => ({ next: () => ({ done: true }) }),
403+
a: [],
404+
}
405+
406+
expect(a).toStrictEqual(b)
407+
})
408+
409+
test('returns false when given iterator within inequal objects', () => {
410+
const a = {
411+
[Symbol.iterator]: () => ({ next: () => ({ done: true }) }),
412+
a: [1],
413+
}
414+
const b = {
415+
[Symbol.iterator]: () => ({ next: () => ({ done: true }) }),
416+
a: [],
417+
}
418+
419+
expect(a).not.toStrictEqual(b)
420+
})
421+
422+
test('returns false when given iterator within inequal nested objects', () => {
423+
const a = {
424+
[Symbol.iterator]: () => ({ next: () => ({ done: true }) }),
425+
a: {
426+
b: [1],
427+
},
428+
}
429+
const b = {
430+
[Symbol.iterator]: () => ({ next: () => ({ done: true }) }),
431+
a: {
432+
b: [],
433+
},
434+
}
435+
436+
expect(a).not.toStrictEqual(b)
437+
})
438+
})

0 commit comments

Comments
 (0)
Please sign in to comment.