Skip to content

soft doesn't work for expect.soft(rejection).resolves #9838

@hi-ogawa

Description

@hi-ogawa

Describe the bug

For the following example, the first test case aborts at first expect.soft(...).resolves.toBe('1st') failure. Had a use case for this when writing the test #9830 and I wish this worked.

import { test, expect } from 'vitest';

test('repro1 (bad)', async () => {
  // executed
  await expect
    .soft(
      Promise.resolve().then(() => {
        throw new Error('boom');
      })
    )
    .resolves.toBe('1st');

  // not executed
  await expect
    .soft(
      Promise.resolve().then(() => {
        throw new Error('boom');
      })
    )
    .resolves.toBe('2nd');
});

test('repro2 (good)', async () => {
  // executed
  await expect.soft(Promise.resolve('boom')).resolves.toBe('1st');
  // executed
  await expect.soft(Promise.resolve('boom')).resolves.toBe('2nd');
});
❯ vitest

 DEV  v4.1.0-beta.6 /home/projects/vitest-dev-vitest-kzm5co9u

 ❯ test/repro.test.ts (2 tests | 2 failed) 7ms
   × repro1 (bad) 3ms
   × repro2 (good) 2ms

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/repro.test.ts > repro1 (bad)
AssertionError: promise rejected "Error: boom" instead of resolving
 ❯ test/repro.test.ts:9:5
      7|         throw new Error('boom');
      8|       })
      9|     )
       |     ^
     10|     .resolves.toBe('1st');
     11|
 ❯ new Promise ../../../blitz.056788dd.js:31:28254

Caused by: Error: boom
 ❯ test/repro.test.ts:7:15
 ❯ ../../../blitz.056788dd.js:31:29909

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/3]⎯

 FAIL  test/repro.test.ts > repro2 (good)
AssertionError: expected 'boom' to be '1st' // Object.is equality

Expected: "1st"
Received: "boom"

 ❯ ../../../blitz.056788dd.js:31:29909

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/3]⎯

 FAIL  test/repro.test.ts > repro2 (good)
AssertionError: expected 'boom' to be '2nd' // Object.is equality

Expected: "2nd"
Received: "boom"

 ❯ ../../../blitz.056788dd.js:31:29909

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/3]⎯


 Test Files  1 failed (1)
      Tests  2 failed (2)
   Start at  18:45:33
   Duration  609ms (transform 40ms, setup 0ms, import 55ms, tests 7ms, environment 0ms)

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-kzm5co9u?file=test%2Frepro.test.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 20.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: latest => 7.3.1 
    vitest: beta => 4.1.0-beta.6

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions