Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect): fix toEqual and toMatchObject with circular references #5535

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export function iterableEquality(a: any, b: any, customTesters: Array<Tester> =
return iterableEquality(
a,
b,
[...filteredCustomTesters],
[...customTesters],
Copy link
Contributor Author

@hi-ogawa hi-ogawa Apr 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this fix is entirely fool-proof for more complicated case, but at least this recursion with filteredCustomTesters seems redundant.

Previously iterableEqualityWithStack was calling iterableEquality(..., [... iterableEqualityWithStack], ...), but iterableEquality is already setting up filteredCustomTesters.
(I don't know how to explain this better...)

[...aStack],
[...bStack],
)
Expand Down
61 changes: 61 additions & 0 deletions test/core/test/expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,64 @@ describe('Error equality', () => {
}
})
})

describe('circular equality', () => {
test('object, set, map', () => {
// https://github.com/vitest-dev/vitest/issues/5533
function gen() {
const obj = {
a: new Set<any>(),
b: new Map<any, any>(),
}
obj.a.add(obj)
obj.b.set('k', obj)
return obj
}
expect(gen()).toEqual(gen())
expect(gen()).toMatchObject(gen())
})

test('object, set', () => {
function gen() {
const obj = {
a: new Set<any>(),
b: new Set<any>(),
}
obj.a.add(obj)
obj.b.add(obj)
return obj
}
expect(gen()).toEqual(gen())
expect(gen()).toMatchObject(gen())
})

test('array, set', () => {
function gen() {
const obj = [new Set<any>(), new Set<any>()]
obj[0].add(obj)
obj[1].add(obj)
return obj
}
expect(gen()).toEqual(gen())
// TODO
expect(() => expect(gen()).toMatchObject(gen())).toThrow()
})

test('object, array', () => {
// https://github.com/jestjs/jest/issues/14734
function gen() {
const a: any = {
v: 1,
}
const c1: any = {
ref: [],
}
c1.ref.push(c1)
a.ref = c1
return a
}
expect(gen()).toEqual(gen())
// TODO
expect(() => expect(gen()).toMatchObject(gen())).toThrow()
})
})