fix(mocha): fail fast when test hook import chain rejects#5624
Merged
DavertMik merged 1 commit intoJun 13, 2026
Merged
Conversation
In 4.x the per-test setup()/teardown() hooks load ./test.js via a dynamic import() with no .catch(). If that import rejects, or the .then body throws (e.g. enhanceMochaTest on an undefined test, a listener throwing), done() is never called and the mocha hook hangs forever — the silent-hang failure mode the 4.0 release is most concerned with. This mirrors the existing suiteSetup()/suiteTeardown() shape exactly: append a .catch(err => doneFn(err)) to both import chains. No recorder.errHandler is added (the per-test handler owns the single errFn slot). makeDoneCallableOnce already guards against a double done() call. Adds 3 regression tests: setup() and teardown() with a throwing then-body call done with the error within 1s instead of hanging, plus a happy-path check. Reverting the fix makes the two error-path tests hang (verified). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
0705154
into
advisor/001-promise-core-characterization
11 checks passed
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.
What
In 4.x the per-test mocha hooks
setup()andteardown()inlib/mocha/asyncWrapper.jsload./test.jsvia a dynamicimport()whose promise has no.catch(). If that import rejects — or the.thenbody throws (enhanceMochaTeston an undefined test, a listener throwing) —done()is never called and the mocha hook hangs forever: the silent-hang failure mode the 4.0 release is most concerned with. The siblingsuiteSetup()/suiteTeardown()already handle this correctly.This mirrors the
suiteSetupshape exactly — appends.catch(err => doneFn(err))to both import chains. It converts a potential silent hang into a reported error; it does not by itself fix the known acceptance failures.Why it's safe
recorder.errHandleris added in setup/teardown — the single module-levelerrFnslot is owned by the per-testtest()/injected()handlers; adding one here would clobber it.makeDoneCallableOncealready guards against a doubledone()call, so the catch cannot double-fire../test.jsfromasyncWrapper.jsis an ESM cycle —lib/mocha/test.jsimportstestback from here).grep -c catch lib/mocha/asyncWrapper.jsgoes 12 → 14 (exactly +2).Tests
3 new regression tests in
test/unit/mocha/asyncWrapper_test.js:setup()/teardown()with a throwingthen-body (actx.currentTestgetter that throws) calldonewith the error within 1s instead of hanging.setup()happy path callsdonewith no error.Reverting the Step-1 lib edit makes both error-path tests hang/fail (verified locally). With the fix they pass.
Verification
npx mocha test/unit/mocha/asyncWrapper_test.js --exit→ 12 passingnpm run test:unit→ 750 passed, 0 failed, 11 skippednpm run test:runner→ 273 passed, 0 failed, 2 skippednpm run lint→ cleanDependency
advisor/001-promise-core-characterizationbranch; GitHub will retarget it to4.xonce #5622 merges. The first commit here is #5622's; review only thefix(mocha): …commit.Note: #5622's commit in this branch also carries a one-line robustness tweak to a characterization assertion (
onFailed.calledOnce→onFailed.called) that prevents a full-suite ordering flake — that tweak belongs to #5622 and was folded into it.🤖 Generated with Claude Code