Skip to content

Conversation

@akshah123
Copy link

@akshah123 akshah123 commented Nov 1, 2025

When before() or beforeEach() hooks fail, Mocha currently only reports the hook failure and silently skips affected tests. This makes it difficult to track test coverage and understand the full impact of hook failures.

This change introduces a new CLI option --fail-hook-affected-tests that, when enabled, reports all tests affected by hook failures as failed instead of silently skipping them.

Changes:

  • Added --fail-hook-affected-tests boolean CLI option
  • Implemented failAffectedTests() method in Runner to fail all affected tests
  • Updated hook() method to call failAffectedTests when hooks fail
  • Added failHookAffectedTests option to Mocha class
  • Added integration tests for both before() and beforeEach() hook failures
  • All affected tests now receive descriptive error messages indicating which hook caused them to be skipped

Fixes #4392

PR Checklist

Overview

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 1, 2025

CLA Not Signed

@mark-wiemer mark-wiemer changed the title feat: add --fail-hook-affected-tests option to report skipped tests a… feat: add --fail-hook-affected-tests option to report skipped tests as failed Nov 1, 2025
@mark-wiemer
Copy link
Member

Hmm, I think we can ignore the CLA issue as the non-signer is AI :D

@mark-wiemer
Copy link
Member

I'll review this in more detail sometime in the next ~7 days :)

@akshah123 akshah123 force-pushed the claude/fix-issue-4392-011CUfwRYBHqzwYtxQzmsCdN branch from 5dee260 to 37acdb2 Compare November 3, 2025 14:22
@codecov
Copy link

codecov bot commented Nov 8, 2025

Codecov Report

❌ Patch coverage is 94.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.69%. Comparing base (2a0bce0) to head (9fe8493).

Files with missing lines Patch % Lines
lib/mocha.js 33.33% 2 Missing ⚠️
lib/runner.js 97.87% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5519   +/-   ##
=======================================
  Coverage   93.69%   93.69%           
=======================================
  Files          57       57           
  Lines        4391     4440   +49     
  Branches      850      861   +11     
=======================================
+ Hits         4114     4160   +46     
- Misses        277      280    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@mark-wiemer mark-wiemer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks OK, just requesting some renames for clarity. I'll check the coverage and make sure I can repro this one locally as it's a big change. Fortunately we should be able to include this in 12.0.0-beta-1 :)

claude and others added 2 commits November 9, 2025 11:24
…s failed

When `before()` or `beforeEach()` hooks fail, Mocha currently only reports
the hook failure and silently skips affected tests. This makes it difficult
to track test coverage and understand the full impact of hook failures.

This change introduces a new CLI option `--fail-hook-affected-tests` that,
when enabled, reports all tests affected by hook failures as failed instead
of silently skipping them.

Changes:
- Added --fail-hook-affected-tests boolean CLI option
- Implemented failAffectedTests() method in Runner to fail all affected tests
- Updated hook() method to call failAffectedTests when hooks fail
- Added failHookAffectedTests option to Mocha class
- Added integration tests for both before() and beforeEach() hook failures
- All affected tests now receive descriptive error messages indicating
  which hook caused them to be skipped

Fixes mochajs#4392
- Extract error message creation into createHookSkipError() helper function to eliminate duplication across three locations
- Simplify recursive forEach call in failAffectedTests()
- Update test fixture and suite descriptions to use backticks for clarity (e.g., 'error in `before` hook')
@akshah123 akshah123 force-pushed the claude/fix-issue-4392-011CUfwRYBHqzwYtxQzmsCdN branch from ade4687 to 9fe8493 Compare November 9, 2025 16:24
@akshah123 akshah123 requested a review from mark-wiemer November 9, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🚀 Feature: Failures in before() and beforeAll() should cause all impacted tests to be reported as failed

3 participants