Skip to content

Reject a promise after a test will make test end earlier in browser mode. #7079

@Tackoil

Description

@Tackoil

Describe the bug

If a test case will make an unhandled rejection just after one test and before the next test, the cases in the other files will not be tested.

After debugging, there will post a "done" message included all test files. And It will break the next test process.

async function reportUnexpectedError(
  type,
  error,
) {
  const processedError = serializeError(error)
  await client.waitForConnection().then(() => {
    return client.rpc.onUnhandledError(processedError, type)
  }).catch(console.error)
  const state = __vitest_browser_runner__

  if (state.type === 'orchestrator') {
    return
  }

  // `state.runTests` and `__vitest_worker__.current`  will both be `null`
  if (!state.runTests || !__vitest_worker__.current) {
    channel.postMessage({
      type: 'done',
      filenames: state.files,
      id: state.iframeId,
    })
  }
}

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-ceqdspje

This issue can also be reproduced by following steps.

  1. mkdir to create a new folder, and npm init.
  2. npx vitest init browser to init the test with browser mode.
  3. modify test command in package.json to run vitest run.
  4. make two test files as following.
import { it } from "vitest";
// Import some source file. It's necessary, but I don't know why.
import { add } from "../src/add";

it("should add two numbers", () => {
  new Promise((resolve, reject) => {
    reject("test");
  });
});
  1. run npm test:browser.

The result will show that only one file is tested.

 RUN  v2.1.8 /Users/<username>/Developer/vitest-playground

 ✓ test/add.test.ts (1)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Unknown Error: test
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 Test Files  1 passed (1)
      Tests  1 passed (1)
     Errors  1 error
   Start at  18:22:47
   Duration  842ms (transform 0ms, setup 0ms, collect 4ms, tests 1ms, environment 0ms, prepare 191ms)

 ELIFECYCLE  Command failed with exit code 1.

System Info

System:
    OS: macOS 15.2
    CPU: (8) arm64 Apple M2
    Memory: 111.73 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.1 - ~/Library/pnpm/node
    npm: 10.7.0 - ~/Library/pnpm/npm
    pnpm: 9.15.0 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 131.0.6778.110
    Chrome Canary: 133.0.6890.0
    Safari: 18.2
  npmPackages:
    @vitest/browser: ^2.1.8 => 2.1.8
    vitest: ^2.1.8 => 2.1.8

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: browserIssues and PRs related to the browser runnerp4-importantViolate documented behavior or significantly improves performance (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions