Skip to content

test: keep running deferred cleanup and afterEach hooks when one throws or hangs - #54950

Merged
MarshallOfSound merged 2 commits into
mainfrom
fix/vitest-defer-cleanup-runs-all-hooks
Oct 15, 2026
Merged

MarshallOfSound merged 2 commits into
mainfrom
fix/vitest-defer-cleanup-runs-all-hooks

Conversation

@ckerr

@ckerr ckerr commented Oct 14, 2026

Copy link
Copy Markdown
Member

Description of Change

This fixes two ways that one bad defer()-ed cleanup could break more than its own test.

A throwing cleanup skipped every later afterEach hook. vitest runs a test's afterEach hooks in order and stops at the first one that throws. It skips the rest, including every enclosing suite's afterEach hooks. The spec runner registers defer()-ed cleanup as the first afterEach hook of every suite, so one failing cleanup also skipped hooks such as protocol.unhandle() and closeWindow().

In the no-cors protocol specs on CI, a cleanup that failed with ENOTEMPTY caused two problems:

  • The no-cors handler stayed registered, so the next try failed in beforeEach with Failed to register protocol: no-cors.
  • A window was left open, which a later closeWindow() reported as a leak.

spec/vitest/runner.ts now catches what each afterEach hook throws, defer()-ed cleanup included, and lets the remaining hooks run. It rethrows the errors from an onTestFinished callback once all the hooks have finished. The test still fails, and vitest reports each error separately with its original stack. Hooks and cleanups run in the same order as before.

A cleanup that never settled would hang the whole spec file. defer()-ed cleanup has had no timeout since the move to vitest (#54264). The mocha shim registered it with vitest's hook timeout set to 0, which vitest treats as no timeout, and the runner from #54439 adds it to the hook list directly, without vitest's afterEach(). So a cleanup such as a closeWindow() whose window never emits closed would hang the worker until the CI job timed out. Each defer()-ed cleanup now gets the hook timeout, as an afterEach hook would. The timeout error points at the defer() call. A timed-out cleanup is handled like a throwing one, so the remaining cleanups and hooks still run; as with a timed-out vitest hook, it keeps running in the background.

The new spec/vitest-runner.spec.ts covers both cases in nested suites: cleanups and hooks that throw, and a cleanup that outlasts a short hook timeout set through vi.setConfig(). It checks that every later cleanup and hook still ran and that the test failed with the expected errors. It is a new spec file, so it adds one short Electron launch to the CI shard that runs it.

🤖 Generated with Claude Code

Checklist

Release Notes

Notes: none

ckerr and others added 2 commits October 14, 2026 16:44
vitest runs a test's afterEach hooks one at a time and stops at the
first one that throws,
skipping the rest of that suite's hooks and every enclosing suite's.
The spec runner adds defer()-ed cleanup as the first afterEach hook of
every suite,
so a single failing cleanup also skipped hooks such as
protocol.unhandle() and closeWindow().
The handler and the window then leaked,
and on CI the next try failed in beforeEach
with "Failed to register protocol: no-cors".

The runner now catches what each afterEach hook throws,
defer()-ed cleanup included,
lets the remaining hooks run,
and rethrows the errors from an onTestFinished callback
once all of them have finished.
The test still fails, and vitest reports each error separately.
The order of hooks and cleanups is unchanged.
A new spec covers failing hooks and cleanups in nested suites.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
defer()-ed cleanup has had no timeout since the move to vitest (#54264).
The mocha shim registered it with vitest's hook timeout set to 0,
which vitest treats as no timeout,
and the runner from #54439 puts it into each suite's afterEach hooks
directly, not through afterEach().
So a cleanup that never settles,
such as a closeWindow() whose window never emits 'closed',
hangs the spec file until the CI job times out
instead of failing one test.

Each defer()-ed cleanup now gets the hook timeout,
as an afterEach hook would.
A cleanup that times out fails the test with an error whose stack
points at its defer() call,
and the cleanups registered before it and the remaining afterEach hooks
still run.
The timeout is read when the cleanup runs, so vi.setConfig() applies;
the new spec uses that to keep its slow-cleanup case fast.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ckerr ckerr added semver/none target/42-x-y PR should also be added to the "42-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. target/45-x-y PR should also be added to the "45-x-y" branch. labels Oct 14, 2026
@MarshallOfSound MarshallOfSound removed the target/42-x-y PR should also be added to the "42-x-y" branch. label Oct 15, 2026
@MarshallOfSound
MarshallOfSound merged commit 5158488 into main Oct 15, 2026
109 checks passed
@release-clerk

release-clerk Bot commented Oct 15, 2026

Copy link
Copy Markdown

No Release Notes

@trop

trop Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "44-x-y", please check out #54994

@trop

trop Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "43-x-y", please check out #54995

@trop trop Bot added in-flight/44-x-y in-flight/43-x-y and removed target/44-x-y PR should also be added to the "44-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. labels Oct 15, 2026
@trop

trop Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "45-x-y", please check out #54997

@trop trop Bot added in-flight/45-x-y merged/45-x-y PR was merged to the "45-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. and removed target/45-x-y PR should also be added to the "45-x-y" branch. in-flight/45-x-y in-flight/44-x-y in-flight/43-x-y labels Oct 15, 2026
@trop trop Bot added the merged/43-x-y PR was merged to the "43-x-y" branch. label Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. semver/none

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants