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

Only report a subset of the actual and expected for toMatchObject #5353

Closed
4 tasks done
geersch opened this issue Mar 7, 2024 · 0 comments · Fixed by #5364
Closed
4 tasks done

Only report a subset of the actual and expected for toMatchObject #5353

geersch opened this issue Mar 7, 2024 · 0 comments · Fixed by #5364
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@geersch
Copy link
Contributor

geersch commented Mar 7, 2024

Clear and concise description of the problem

If you run this test.

describe('diff test', () => {
  it('should compare 2 objects', () => {
    const a = { a: 1, b: 2, c: { d: 4 } };
    const b = { b: 3 };

    expect(a).toMatchObject(b);
  });
});

You get the following output.

Screenshot 2024-03-07 at 12 43 23

It would be clearer if it would strip properties that are not relevant. For example, see getObjectSubset in @jest/expect-utils.

getObjectSubset

After patching the processError function (@vitest/utils) locally.

export function processError(err: any, diffOptions?: DiffOptions) {
...
  if (err.showDiff || (err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined)) {
    ...
    const actualSubset = getObjectSubset(replacedActual, replacedExpected);
    err.actual = actualSubset;
    err.diff = diff(replacedExpected, actualSubset, { ...diffOptions, ...err.diffOptions });
    ...
  }
...
}

I get this output instead.

Screenshot 2024-03-07 at 12 56 42

Which makes it a lot easier to spot the difference.

The output with the VSCode plugin looks like this now.

Screenshot 2024-03-07 at 12 22 44

Perhaps it would be useful to also add this into Vitest?

Suggested solution

Extract a subset of properties from the actual and report that as output.

Alternative

No response

Additional context

No response

Validations

@sheremet-va sheremet-va added p2-nice-to-have Not breaking anything but nice to have (priority) and removed enhancement: pending triage labels Mar 7, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants