Skip to content

expect.soft + .resolves/.rejects failures lose their location #11791

Closed

Description

@hi-ogawa

I found a related but independent pre-existing bugs while reviewing #11325, which partly fixed this issue but not fully.

When an assertion fails under expect.soft(…) combined with .resolves or .rejects, the error has no stack frames. The report prints no ❯ file:line:col and no code frame. This happens with any matcher, not just snapshots. expect.soft alone and .rejects alone both keep the location.

Repro

Any matcher reproduces it:

await expect(Promise.resolve(1)).resolves.toEqual(2)      // ❯ file:line:col and code frame
await expect.soft(Promise.resolve(1)).resolves.toEqual(2) // no location

The gist is a standalone project pinned to the beta. It also covers snapshot matchers and toThrow, with a .snap file that has outdated values and update: 'none'. The rows without .soft, or without .resolves/.rejects, are controls that keep their location.

git clone https://gist.github.com/hi-ogawa/bfdfc0f8cc87bba3b8358c9a51f70caa.git repro && cd repro
npm install
npx vitest run

Each test failed on purpose. This is where its reported location pointed (full output):

Test Assertion Location
resolves toEqual expect(p).resolves.toEqual(2) repro.test.ts:36:35
soft resolves toEqual expect.soft(p).resolves.toEqual(2) none
rejects snapshot expect(p).rejects.toMatchSnapshot() repro.test.ts:4:49
rejects toThrow expect(p).rejects.toThrow('old') repro.test.ts:24:49
soft sync toBe expect.soft('new').toBe('old') repro.test.ts:28:22
soft rejects snapshot expect.soft(p).rejects.toMatchSnapshot() none
soft resolves snapshot expect.soft(p).resolves.toMatchSnapshot() none
soft rejects toThrow expect.soft(p).rejects.toThrow('old') none
soft rejects toThrowErrorMatchingSnapshot expect.soft(p).rejects.toThrowErrorMatchingSnapshot() none
soft rejects, promise resolves expect.soft(Promise.resolve('new')).rejects.toThrow('old') repro.test.ts:32:44

Same assertion, with and without .soft:

  • 🟢 resolves only
 FAIL  repro.test.ts > resolves toEqual
AssertionError: expected 1 to deeply equal 2

- Expected
+ Received

- 2
+ 1

 ❯ repro.test.ts:36:35
     34|
     35| test('resolves toEqual', async () => {
     36|   await expect(Promise.resolve(1)).resolves.toEqual(2)
       |                                   ^
  • 🔴 soft + resolves
 FAIL  repro.test.ts > soft resolves toEqual
AssertionError: expected 1 to deeply equal 2

- Expected
+ Received

- 2
+ 1

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[10/10]⎯

An e2e probe in the vitest repo (browser-probe.test.ts.txt) shows the same thing in browser mode (Chromium), on a workspace build whose snapshot matcher code matches main.

Cause analysis 🤖

Code links are to main at 54a2bdb.

The matcher runs inside a promise callback, so the error it throws has only internal frames in its stack. Vitest filters those out, which leaves stacks empty. .resolves and .rejects have a fix for this: the getter creates an Error when it is accessed, at the user's line (jest-expect.ts:972, :1035). A .catch on the promise chain then rewrites the failing error's stack to that one (:1013, :1080).

expect.soft bypasses that fix. Under .soft, the matcher called at :1000 and :1075 is wrapAssertion's soft wrapper (utils.ts:95). The wrapper catches the matcher's error itself and calls handleTestError (utils.ts:136), which runs processError and records the error on the test right away. So:

  1. The error is recorded with its original stack, which has no user frames.
  2. The wrapper returns normally, so the promise chain resolves and the .catch that would have rewritten the stack never runs.
expect.soft(p).rejects.toThrow('old')
  rejects getter: error = new Error('rejects')       ← user's line, kept for later
  promise.then(err => matcher())                     ← runs in a microtask
    soft wrapper: try { toThrow() } catch (e) {
      handleTestError(test, e)                       ← recorded here, original stack
    }                                                ← no rethrow
  .catch(err => err.stack = error.stack)             ← skipped, nothing rejected

The "promise resolves" row confirms this. There the promise resolves, so .rejects throws its own "promise resolved … instead of rejecting" error before any matcher runs. That error does reach the .catch and gets the user's stack. The soft handler in recordAsyncExpect records it afterwards (utils.ts:48), and the location is kept. Only errors from the matcher itself lose it.

A fix could rewrite the stack before recording: in the soft wrapper, when the error flag is set, copy its stack onto the caught error ahead of handleTestError. Another option is to skip the soft wrapper inside .resolves/.rejects, so the error flows through the existing .catch and then recordAsyncExpect's soft handler. Either way, the fix covers every matcher, not just snapshots.

Relation to PR 11325

PR 11325 copies the .resolves/.rejects stack (the chai error flag) onto the thrown error inside the snapshot matchers' assertMatchResult. Non-soft async snapshot failures already get the right stack from the .catch above, so that copy only changes anything under expect.soft. This is why its review screen shows "Source:" for expect.soft(…).rejects.toThrowErrorMatchingSnapshot(…). It doesn't help other matchers, such as soft rejects toThrow above. The fix belongs in the soft wrapper or in the .resolves/.rejects getters, not in the snapshot matchers.

Activity

  1. added
    p3-minor-bug
    on Oct 30, 2026
  2. locked and limited conversation to collaborators on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions