Skip to content

fix(cli): report tests behind a failed before hook as failed when Mocha fails them - #387

Closed
kamal-kaur04 wants to merge 2 commits into
mainfrom
fix/mocha-fail-hook-affected-tests
Closed

kamal-kaur04 wants to merge 2 commits into
mainfrom
fix/mocha-fail-hook-affected-tests

Conversation

@kamal-kaur04

Copy link
Copy Markdown
Collaborator

What is this about?

@wdio/mocha-framework 10 turns on Mocha's failHookAffectedTests when the user leaves it unset. With it on, Mocha 12 marks the tests behind a failed before/beforeEach hook as failed, so CI reports them failed.

The service still reported those tests as skipped. Its failed-hook cascade, in afterHook on the CLI flow and in the insights handler off it, runs inside the hook wrapper, before Mocha marks the tests. Mocha's later fail for each test was then dropped, because the test had never started. So CI said failed and Test Reporting said skipped.

Change

  • service.ts:
    • New flag _mochaFailHookAffectedTests: on for Mocha unless mochaOpts.failHookAffectedTests is false.
    • New helper _hookAffectedTestsFailure() that returns the hook's result as a failure only when:
      • the flag is on;
      • the hook is BEFORE_ALL or BEFORE_EACH;
      • the hook really failed. A this.skip() hook still reports the tests as skipped.
  • cli/skipReporter.ts: reportSuiteSkipped / reportSkippedTest take an optional failure and send it through the same event sequence instead of the skipped result. The existing result mapping turns it into failed with the error message, stack and type. The bail cascade calls this without a failure, so it is unchanged.
  • insights-handler.ts (non-CLI path): with that failure, the affected tests are sent as TestRunFinished with the failure instead of TestRunSkipped.
  • Unchanged:
    • AFTER_EACH: Mocha still drops those tests, so they stay skipped;
    • failHookAffectedTests: false;
    • Cucumber and Jasmine.

Verification

  • Unit tests, all in the existing per-source files:
    • service.test.ts: BEFORE_EACH / BEFORE_ALL fail with the hook error; option false, AFTER_EACH and a this.skip() hook stay skipped; the non-CLI handoff.
    • cli/skipReporter.test.ts: the failure propagates to nested suites.
    • insights-handler.test.ts: finished-with-failure vs skipped.
  • The 5 tests that check failed reporting fail on main. Full package suite: 58 files / 1,504 tests pass; build and lint clean.

End to end on BrowserStack: WebdriverIO 10.2.0 (@wdio/cli, @wdio/local-runner, @wdio/mocha-framework 10.2.0, Mocha 12.0.3), CLI flow, Chrome on Windows 11, Test Reporting on. One spec with a beforeEach that throws and 2 tests. The service was installed from npm pack of main and of this branch.

Run Test Reporting build Test 1 Test 2
main qffjianrcxbd4nf3i2xdrulrbb1ww47qeg8ehh59 skipped skipped
this branch cnxniqlqr4giogckdbaaygqzvymlezqazywlrqc8 failed (hook error + stack) failed
this branch, failHookAffectedTests: false 0dwf0uec0f2i22op6vjeau14evhdddylx4r0ymat skipped skipped

There are no duplicate test rows, and both tests stay linked to the session.

Not covered end to end:

  • an outer-suite beforeEach failing while running a test in a child suite (the cascade starts from the test's parent, as the skip cascade does);
  • maxParallelContexts;
  • multiremote.

Mocha retries behaves as before.

Related Jira task/s

  • N/A (tracked internally)

Release (mandatory for every PR — required for the ready-for-review label)

Version bump: (required — tick exactly one)

  • minor (backwards-compatible feature)
  • patch (bug fix or other small change)

Release notes type: (optional)

  • New Feature
  • Bug Fix
  • Other Improvement

Release notes (customer-facing): (optional but encouraged)

  • Fixed Mocha tests that fail because of a failed before/beforeEach hook showing as skipped in Test Reporting. They are now reported as failed with the hook's error, matching WebdriverIO 10's default failHookAffectedTests.

Release notes (internal): (required — engineer-facing; what actually changed / why)

  • The failed-hook cascade (CLI reportSuiteSkipped and the non-CLI insights handler) now reports BEFORE_ALL/BEFORE_EACH-affected tests as failed with the hook's result when mochaOpts.failHookAffectedTests is not false, to match Mocha 12 under @wdio/mocha-framework 10. AFTER_EACH and the opt-out keep the skipped report.

Checklist

  • Ready to review
  • Has it been tested locally?

PR Validations

Run Tests: Comment RUN_TESTS to trigger sanity tests.

🤖 Generated with Claude Code

…ha fails them

@wdio/mocha-framework 10 turns Mocha's failHookAffectedTests on by default,
so tests behind a failed before/beforeEach hook are failed, not dropped.
The hook cascade still reported them as skipped, and the reporter's later
fail event for them is dropped, so Test Reporting showed skipped while CI
showed failed.

Report them as failed with the hook's error unless the user sets
mochaOpts.failHookAffectedTests to false. afterEach failures and
this.skip() hooks keep the skipped report. Applies to both the CLI
cascade and the legacy insights cascade.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kamal-kaur04
kamal-kaur04 requested a review from a team as a code owner November 25, 2026 15:07
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto (fallback from Default branch)
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Central YAML (base), Organization UI (inherited), Workspace UI (inherited); Enforced overrides: Organization
  • Review profile: ASSERTIVE
  • Plan: Enterprise
  • Run ID: 54316e88-eaf6-46e4-8ef3-05ddfd54803d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@kamal-kaur04

Copy link
Copy Markdown
Collaborator Author

Combined into #388 with the other 10.x CLI follow-ups, so they review and release together. The commits are unchanged (cherry-picked onto current main).

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.

1 participant