Skip to content

Conversation

@akshah123
Copy link
Contributor

@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 Signed

The committers listed above are authorized under a signed CLA.

@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 92.59259% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.40%. Comparing base (1972dd7) to head (72381b7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/mocha.js 33.33% 2 Missing ⚠️
lib/runner.js 96.07% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5519   +/-   ##
=======================================
  Coverage   88.40%   88.40%           
=======================================
  Files          66       66           
  Lines        4734     4787   +53     
  Branches      964      977   +13     
=======================================
+ Hits         4185     4232   +47     
- Misses        549      555    +6     

☔ 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
@mark-wiemer
Copy link
Member

Took me a minute to remember I needed to actively add the --fail-hook-affected-tests flag to see the new behavior :D was able to repro locally and the code does look good. Going to wait for @JoshuaKGoldberg to review, hopefully early next week.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Generally looking good - the approach is solid and respects existing patterns. Great! 🙌

Requesting changes on the [Bug] around throwing non-Error values. Otherwise we're looking pretty good to merge soon.

@mark-wiemer shall we back-port this to v11?

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author waiting on response from OP or other posters - more information needed label Dec 18, 2025
- Fix JSDoc placement for failHookAffectedTests and failZero
- Handle non-Error thrown values in createHookSkipError
- Add tests for null, undefined, and other non-Error throws

Addresses review comments from @JoshuaKGoldberg:
- Separated JSDoc for failHookAffectedTests from failZero docs
- Added proper handling for falsy and non-Error exceptions using
  createInvalidExceptionError and thrown2Error patterns
- Added comprehensive test coverage for edge cases

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@akshah123
Copy link
Contributor Author

@JoshuaKGoldberg Thanks for the thorough review! I've addressed both of your comments in the latest commit (c171cf1):

Re: [Docs] JSDoc placement - Fixed! The JSDoc now correctly documents failHookAffectedTests and has been separated from failZero's documentation. Each function now has its own properly positioned JSDoc comment.

Re: [Bug] Non-Error handling - Great catch! The createHookSkipError function now properly handles:

  • Falsy values (null/undefined) using createInvalidExceptionError
  • Non-Error objects (strings, numbers, etc.) using thrown2Error

This follows the existing patterns from runner.js:1112 and runner.js:537. I've also added comprehensive test coverage for these edge cases with new fixture files testing null, undefined, string, and number throws in both before and beforeEach hooks.

All tests are passing, including the 2 new integration tests specifically for non-Error handling. ✅

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

LGTM. One little touchup but we can always apply this before merging. Thanks! ⭐

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
@mark-wiemer
Copy link
Member

@akshah123 looks like your latest commit broke some stuff, please review the failed checks.

@JoshuaKGoldberg happy to port this back to v11, created #5580

Fixed syntax error where hookError was being interpolated into the
error message when it should just describe it as falsy/undefined.
@akshah123
Copy link
Contributor Author

Sorry about that @mark-wiemer . I didn't verify syntax issues with suggested changes. It should be fixed now.

@mark-wiemer mark-wiemer removed the status: waiting for author waiting on response from OP or other posters - more information needed label Dec 20, 2025
@mark-wiemer mark-wiemer added the status: needs review a maintainer should (re-)review this pull request label Jan 3, 2026
@mark-wiemer mark-wiemer removed the status: needs review a maintainer should (re-)review this pull request label Jan 3, 2026
@mark-wiemer
Copy link
Member

Still LGTM to me, great work :) running tests now to make sure nothing weird changed, then I plan to apply my suggestions and run tests once more. @JoshuaKGoldberg , OK to merge this into 12.0 or should we wait until 12.1? Obviously the flag is still disabled by default so I don't foresee issues and would love to merge this into 12.0 :)

@JoshuaKGoldberg
Copy link
Member

If we merge it into 12, I think we should probably also back-port it to 11. 👍 from me!

@mark-wiemer
Copy link
Member

Yep, we already have #5580 :)

@mark-wiemer mark-wiemer merged commit 0ed524a into mochajs:main Jan 6, 2026
79 checks passed
@JoshuaKGoldberg
Copy link
Member

heck yes

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

Labels

None yet

Projects

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

4 participants