Skip to content

Commit

Permalink
fix: backport jest iterable equality within object (#5621)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej committed Apr 29, 2024
1 parent 08c79d6 commit 30e5dc1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/expect/src/jest-utils.ts
Expand Up @@ -411,6 +411,11 @@ export function iterableEquality(a: any, b: any, customTesters: Array<Tester> =
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()
Expand Down
45 changes: 45 additions & 0 deletions test/core/test/expect.test.ts
Expand Up @@ -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)
})
})

0 comments on commit 30e5dc1

Please sign in to comment.