Skip to content

fix: set non-zero exit code when teardown throws during close#10794

Merged
sheremet-va merged 2 commits into
vitest-dev:mainfrom
zakcutner:fix/close-teardown-exit-code
Jul 21, 2026
Merged

fix: set non-zero exit code when teardown throws during close#10794
sheremet-va merged 2 commits into
vitest-dev:mainfrom
zakcutner:fix/close-teardown-exit-code

Conversation

@zakcutner

Copy link
Copy Markdown
Contributor

Description

Errors thrown while closing Vitest, most notably a failing globalSetup teardown, were logged but never affected the exit code. As a result vitest run exited 0 even though cleanup failed.

vitest.close() catches teardown errors since #9573, so that the rest of the shutdown still runs, and logs them via logger.error('error during close', ...). However, nothing set a non-zero exit code afterwards, so any cleanup failure in a globalSetup teardown was silently swallowed.

This change routes the collected errors, meaning teardown errors plus any rejected close promises, through the existing _checkUnhandledErrors. That is the same mechanism used for unhandled test errors. It sets process.exitCode to 1 and honours dangerouslyIgnoreUnhandledErrors.

Reproduction

A globalSetup whose teardown throws, with otherwise passing tests:

// globalSetup.ts
export default function () {
  return () => {
    throw new Error('teardown error')
  }
}

Before this change, vitest run prints the error but exits 0. After, it exits 1.

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

  • 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.
  • 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.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

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

  • 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:.

@netlify

netlify Bot commented Jul 17, 2026

Copy link
Copy Markdown

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0b596f2
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a5df1ad1ea0b100089b55f5
😎 Deploy Preview https://deploy-preview-10794--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

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

@zakcutner
zakcutner force-pushed the fix/close-teardown-exit-code branch from 0cfa5c2 to 0b596f2 Compare July 20, 2026 10:00
@zakcutner
zakcutner force-pushed the fix/close-teardown-exit-code branch from 0b596f2 to 4876a68 Compare July 20, 2026 10:10

@AriPerkkio AriPerkkio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reporting project._teardownGlobalSetup() errors as unhandled errors makes sense to me. I'm not fully sure if pool closing and project cleanups should be treated too. Though why not 🤔

@sheremet-va
sheremet-va merged commit 22116fe into vitest-dev:main Jul 21, 2026
26 of 27 checks passed
@zakcutner
zakcutner deleted the fix/close-teardown-exit-code branch July 21, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants