Bug Report Checklist
Expected behavior
--fail-hook-affected-tests (added in v12) is documented to report tests as failed when they are affected by a hook failure, instead of silently skipping them. A failing beforeEach affects every test the hook would have run before, including tests in nested describe blocks, so all of them should be reported as failed. This is what before/beforeAll already does.
Actual behavior
With --fail-hook-affected-tests on, a failing beforeEach only fails the remaining tests of the immediate suite. Tests inside nested sub-suites vanish from the run entirely (not failed, not pending), so the failure and total counts are under-reported in every reporter (spec, json, tap, xunit).
Minimal, Complete, and Verifiable Example
// repro.mjs
describe('outer', function () {
beforeEach(function () { throw new Error('boom'); });
it('direct test', function () {});
describe('nested', function () {
it('nested test 1', function () {});
it('nested test 2', function () {});
});
});
Run: mocha --fail-hook-affected-tests repro.mjs
beforeEach result: 2 failing (the hook and direct test); the two nested tests are dropped.
Swap beforeEach for before: 4 failing (the hook, direct test, nested test 1, nested test 2) are all reported. That is the behavior the beforeEach path should match.
Versions
- mocha 12.0.0-rc.1 (also reproduces on current
main)
- Node.js 24
- Platform-independent (reproduced on macOS)
Additional Information
before/beforeAll already recurses into sub-suites via its failAffectedTests path; the beforeEach branch fails only the immediate suite's not-yet-run tests and never recurses. The two branches should be consistent.
Bug Report Checklist
npm audit, or GitHub Advisory issue.faqlabel, but none matched.Expected behavior
--fail-hook-affected-tests(added in v12) is documented to report tests as failed when they are affected by a hook failure, instead of silently skipping them. A failingbeforeEachaffects every test the hook would have run before, including tests in nesteddescribeblocks, so all of them should be reported as failed. This is whatbefore/beforeAllalready does.Actual behavior
With
--fail-hook-affected-testson, a failingbeforeEachonly fails the remaining tests of the immediate suite. Tests inside nested sub-suites vanish from the run entirely (not failed, not pending), so the failure and total counts are under-reported in every reporter (spec, json, tap, xunit).Minimal, Complete, and Verifiable Example
Run:
mocha --fail-hook-affected-tests repro.mjsbeforeEachresult: 2 failing (the hook anddirect test); the two nested tests are dropped.Swap
beforeEachforbefore: 4 failing (the hook,direct test,nested test 1,nested test 2) are all reported. That is the behavior thebeforeEachpath should match.Versions
main)Additional Information
before/beforeAllalready recurses into sub-suites via itsfailAffectedTestspath; thebeforeEachbranch fails only the immediate suite's not-yet-run tests and never recurses. The two branches should be consistent.