Skip to content

fix: report test error when afterEach fails before retry - #6200

Open
deepakganesh78 wants to merge 1 commit into
mochajs:mainfrom
deepakganesh78:fix/issue5007-aftereach-retry-error
Open

deepakganesh78 wants to merge 1 commit into
mochajs:mainfrom
deepakganesh78:fix/issue5007-aftereach-retry-error

Conversation

@deepakganesh78

Copy link
Copy Markdown

When a retryable test failed and its afterEach hook also failed, Mocha only reported the hook failure and hid the original test error.

The retry path ran afterEach before marking the test failed; this now reports the test error if afterEach aborts the retry.

Fixes #5007

Validation: npm run lint; npm run test-node:unit (1222 passing, 6 pending); npm run test-smoke (1 passing).

Fixes mochajs#5007

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

👋 Hi @deepakganesh78, thanks for the pull request! A scan flagged a concern with it. Could you please take a look?

[pr-task-completion] This PR's body is missing [x] checks on the following tasks from the PR template.

Repositories often provide a set of tasks that pull request authors are expected to complete. Those tasks should be marked as completed with a [x] in the pull request description. Please complete those tasks and mark the checks as [x] completed.

🗺️ This message was posted automatically by OctoGuide: a bot for GitHub repository best practices.

@praann07 praann07 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed at the code level only (did not run the suite locally).

When afterEach throws before a retry, this now calls self.fail(test, err) and manually emits EVENT_TEST_END. Two things worth checking before merge:

  1. Does Runner.prototype.fail already emit EVENT_TEST_END downstream? If so, the explicit self.emit(constants.EVENT_TEST_END, test) could double-emit and surface the test twice in reporters (and the event stream). Worth confirming against lib/runner.cjs fail().
  2. self.fail(test, err) fails the test with the test's own error (err), not the hook's error (hookErr). That reads as intentional (the test should be reported as failed), but a one-line comment clarifying "fail the test, not the hook" would prevent future confusion.

The added regression test is a good guard.

praann07

This comment was marked as spam.

praann07

This comment was marked as spam.

praann07

This comment was marked as spam.

@mark-wiemer mark-wiemer added this to Mocha Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

🐛 Bug: Error from afterEach hides the original error from the test case when retries > 0

3 participants