vitest-dev / vitest-dev/vitest

More Strict Error Equality fails with mswjs HttpResponse.error()

Open
#8,317 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

mswjs uses another way to add the cause of an error to the error itself, so the diff, based on keys
eq({...a}, {....b) ...
fails, but should not
currently we will remove the cause from the expected result manually to compare without it.

Reproduction
const NewError = new Error('hi');
const NewErrorWithCause = new Error('hi', { cause: 'x' });
const NewErrorWithAssignedCause = Object.assign(new Error('hi'), {
    cause: 'x',
});

// examples from https://vitest.dev/guide/migration.html#more-strict-error-equality
// same as
// expect(new Error('hi', { cause: 'x' })).toEqual(new Error('hi')); // ✅
expect(NewErrorWithCause).toEqual(NewError); // ✅
// same as 
// expect(new Error('hi')).toEqual(new Error('hi', { cause: 'x' })); // ❌
expect(NewError).toEqual(NewErrorWithCause); // ❌

// examples based on
// https://github.com/mswjs/interceptors/blob/7410d45a919bacadc2995d6173f3f7e7340f6e50/src/interceptors/fetch/utils/createNetworkError.ts
// https://github.com/mswjs/interceptors/blob/main/src/interceptors/fetch/utils/createNetworkError.ts
expect(NewErrorWithAssignedCause).toEqual(NewError); // should be ✅ -> but is ❌
expect(NewErrorWithAssignedCause).toEqual(NewErrorWithCause); // should be ✅ -> but is ❌

### System Info

```shell
System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-1260P
    Memory: 12.65 GB / 31.71 GB
Binaries:
    Node: 22.16.0
    npm: 10.9.2
Browsers:
    Edge: Chromium (132.0.2957.140)
    Internet Explorer: 11.0.22621.3527
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the examples in the issue around more-strict error equality, including errors created with Error.cause and errors whose cause is assigned afterward. Read the linked Vitest migration guidance and the referenced mswjs createNetworkError implementation to compare the error shapes. Done means the assigned-cause cases match the expected equality behavior without manual cause removal.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.