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 all 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
21 changes: 11 additions & 10 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,18 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const pass = jestEquals(actual, expected, [...customTesters, iterableEquality, subsetEquality])
const isNot = utils.flag(this, 'negate') as boolean
const { subset: actualSubset, stripped } = getObjectSubset(actual, expected)
const msg = utils.getMessage(
this,
[
pass,
'expected #{this} to match object #{exp}',
'expected #{this} to not match object #{exp}',
expected,
actualSubset,
],
)
if ((pass && isNot) || (!pass && !isNot)) {
const msg = utils.getMessage(
Copy link
Contributor Author

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 shows loupe is doing something).

For now, I hid it inside failure case since msg is not used fro success case anyways.

this,
[
pass,
'expected #{this} to match object #{exp}',
'expected #{this} to not match object #{exp}',
expected,
actualSubset,
false,
],
)
const message = stripped === 0 ? msg : `${msg}\n(${stripped} matching ${stripped === 1 ? 'property' : 'properties'} omitted from actual)`
throw new AssertionError(message, { showDiff: true, expected, actual: actualSubset })
}
Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -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') {
Copy link
Contributor Author

@hi-ogawa hi-ogawa Apr 16, 2024

Choose a reason for hiding this comment

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

I'm not entirely convinced with the fix for subsetEquality as well, but the "array" specific infinite recursion is due to seenReference is missing array.

(Actually I'm fine with the fix, but I feel the implementation of subsetEquality feels a little off in general since it doesn't take the approach of aStack/bStack like eq and iterableEquality do)

if (seenReferences.has(subset[key]))
return equals(object[key], subset[key], filteredCustomTesters)

Expand Down
60 changes: 60 additions & 0 deletions test/core/test/expect-circular.test.ts
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())
})
})