vitest icon indicating copy to clipboard operation
vitest copied to clipboard

fix(expect,pretty-format,utils)!: check more properties for error equality

Open hi-ogawa opened this issue 1 year ago • 4 comments

Description

  • closes https://github.com/vitest-dev/vitest/issues/5244
  • closes https://github.com/vitest-dev/vitest/issues/5697

NodeJs PR for a reference

  • https://github.com/nodejs/node/pull/51805

todo

  • [x] error equality
  • [x] error formatting
  • [x] asymmetrically show cause diff
  • [x] handle cyclic object
    • [x] ~it can fail due to chai's error message generation~ https://github.com/chaijs/chai/issues/1645 Fixed!
  • [x] should we update toThrowError(new Error(...)) usage too? (I think so)
  • [ ] should we break snapshot too? (for now, new error formatter is applied only when diffing and this looks fine)
  • [x] docs

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

  • [x] 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.
  • [x] 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

  • [x] 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:.

hi-ogawa avatar Jun 12 '24 02:06 hi-ogawa

@hi-ogawa is there anything specific that keeps this PR in draft? Can I help?

sheremet-va avatar Jul 01 '24 16:07 sheremet-va

I had a concern about whether we should print diff when custom properties are different. Currently, Vitest cannot show any diff in this case since pretty-format strips it off. Fortunately, chai/loupe can print them, but it's possible that the message will be truncated and not visible to users. This is an example from test cases:

exports[`Error equality > basic 4`] = `
{
  "actual": "[Error: hello]",
  "diff": "Compared values have no visual difference.",
  "expected": "[Error: hello]",
  "message": "expected Error: hello { cause: 'x' } to deeply equal Error: hello { cause: 'y' }",
}
`;

Do you think it's worth changing pretty-format side (I haven't attempted anything yet whether it's possible or not) or just keep it as is and rely on chai error message?

hi-ogawa avatar Jul 01 '24 23:07 hi-ogawa

Do you think it's worth changing pretty-format side (I haven't attempted anything yet whether it's possible or not) or just keep it as is and rely on chai error message?

I think we can maybe introduce a flag in pretty-print (or maybe there is already one?) to print Error as an object.

sheremet-va avatar Jul 02 '24 07:07 sheremet-va

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
Latest commit baf414bdc0c023ec67e4696d334fcf2e27457c54
Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/67578f91cb654800080d3bcf
Deploy Preview https://deploy-preview-5876--vitest-dev.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Oct 07 '24 04:10 netlify[bot]

This fails now since we changed how diff is displayed 😄

sheremet-va avatar Dec 09 '24 09:12 sheremet-va