Repository navigation
test: keep running deferred cleanup and afterEach hooks when one throws or hangs - #54950
Merged
Merged
Conversation
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]>
MarshallOfSound
approved these changes
Oct 15, 2026
|
No Release Notes |
This was referenced Oct 15, 2026
Contributor
|
I have automatically backported this PR to "44-x-y", please check out #54994 |
Contributor
|
I have automatically backported this PR to "43-x-y", please check out #54995 |
Contributor
|
I have automatically backported this PR to "45-x-y", please check out #54997 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 asprotocol.unhandle()andcloseWindow().In the
no-corsprotocol specs on CI, a cleanup that failed withENOTEMPTYcaused two problems:no-corshandler stayed registered, so the next try failed inbeforeEachwithFailed to register protocol: no-cors.closeWindow()reported as a leak.spec/vitest/runner.tsnow catches what each afterEach hook throws,defer()-ed cleanup included, and lets the remaining hooks run. It rethrows the errors from anonTestFinishedcallback 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'safterEach(). So a cleanup such as acloseWindow()whose window never emitsclosedwould hang the worker until the CI job timed out. Eachdefer()-ed cleanup now gets the hook timeout, as an afterEach hook would. The timeout error points at thedefer()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.tscovers both cases in nested suites: cleanups and hooks that throw, and a cleanup that outlasts a short hook timeout set throughvi.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