Skip to content

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

Merged
MarshallOfSound merged 2 commits into
45-x-yfrom
trop/45-x-y-bp-test-keep-running-deferred-cleanup-and-aftereach-hooks-when-one-throws-or-hangs-1792045068393
Oct 15, 2026
Merged

MarshallOfSound merged 2 commits into
45-x-yfrom
trop/45-x-y-bp-test-keep-running-deferred-cleanup-and-aftereach-hooks-when-one-throws-or-hangs-1792045068393

Conversation

@trop

@trop trop Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Backport of #54950

See that PR for details.

Notes: none

trop Bot and others added 2 commits October 15, 2026 06:18
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]>

Co-authored-by: Charles Kerr <[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]>

Co-authored-by: Charles Kerr <[email protected]>
@trop
trop Bot requested a review from ckerr October 15, 2026 06:18
@trop trop Bot added 45-x-y backport This is a backport PR semver/none labels Oct 15, 2026
@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) October 15, 2026 07:05
@MarshallOfSound
MarshallOfSound merged commit 0a73a4d into 45-x-y Oct 15, 2026
90 checks passed
@MarshallOfSound
MarshallOfSound deleted the trop/45-x-y-bp-test-keep-running-deferred-cleanup-and-aftereach-hooks-when-one-throws-or-hangs-1792045068393 branch October 15, 2026 08:01
@release-clerk

release-clerk Bot commented Oct 15, 2026

Copy link
Copy Markdown

No Release Notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant