Skip to content

🐛 Bug: --fail-hook-affected-tests drops nested-suite tests when a beforeEach hook fails #6132

Description

@spokodev

Bug Report Checklist

  • This is NOT a security, npm audit, or GitHub Advisory issue.
  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines.
  • I have searched for related issues and issues with the faq label, but none matched.
  • I have 'smoke tested' the code to be tested by running it outside the real test suite.
  • I want to provide a PR to resolve this.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions