-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
91cf623
d034e24
470f561
a13e9ab
c2274b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,7 +334,7 @@ export function iterableEquality(a: any, b: any, customTesters: Array<Tester> = | |
return iterableEquality( | ||
a, | ||
b, | ||
[...filteredCustomTesters], | ||
[...customTesters], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Previously |
||
[...aStack], | ||
[...bStack], | ||
) | ||
|
@@ -452,7 +452,7 @@ export function subsetEquality(object: unknown, subset: unknown, customTesters: | |
return undefined | ||
|
||
return Object.keys(subset).every((key) => { | ||
if (isObjectWithKeys(subset[key])) { | ||
if (typeof subset[key] === 'object') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not entirely convinced with the fix for (Actually I'm fine with the fix, but I feel the implementation of |
||
if (seenReferences.has(subset[key])) | ||
return equals(object[key], subset[key], filteredCustomTesters) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { describe, expect, test } from 'vitest' | ||
|
||
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()) | ||
expect(gen()).toMatchObject(gen()) | ||
}) | ||
|
||
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()) | ||
expect(gen()).toMatchObject(gen()) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't follow up this properly, but in some cases,
utils.getMessage
also gets in loop (stacktrace showsloupe
is doing something).For now, I hid it inside failure case since
msg
is not used fro success case anyways.