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

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Apr 14, 2024

Description

This seems to be a regression from custom equality tester #4880 (introduced in v1.2.0). I haven't properly analyzed code, but this filteredCustomTesters looks fishy.

Btw, it turned out it reproduces on Jest too since code looks mostly same https://github.com/hi-ogawa/reproductions/tree/main/jest-circular-iterable

Searching Jest issues, I found a similar issue and this reproduces on Vitest jestjs/jest#14734. They seemed to fixed this one already, so I'll try port back to Vitest later.
(EDIT: this one is also partially fixed)

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Apr 14, 2024

Deploy Preview for fastidious-cascaron-4ded94 canceled.

Name Link
🔨 Latest commit c2274b6
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/661e4f86f42ff60008738b27

@hi-ogawa hi-ogawa marked this pull request as ready for review April 14, 2024 03:44
@@ -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...)

* wip

* fix: array reference in subset
@hi-ogawa hi-ogawa changed the title fix(expect): fix iterable equality with circular references fix(expect): fix toEqual and toMatchObject with circular references Apr 16, 2024
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.

@@ -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)

@sheremet-va sheremet-va merged commit 9e6417c into vitest-dev:main Apr 17, 2024
18 of 19 checks passed
@hi-ogawa hi-ogawa deleted the fix-toEqual-two-circular-iterable branch April 17, 2024 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants