Skip to content

fix(cli): give root-level mocha hooks the suite file, as the non-CLI flow does (v8) - #309

Open
rounak610 wants to merge 4 commits into
v8from
fix/wdio-root-hook-file-v8
Open

rounak610 wants to merge 4 commits into
v8from
fix/wdio-root-hook-file-v8

Conversation

@rounak610

@rounak610 rounak610 commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

What is this about?

v8 backport of #308 (main): same change, same tests. Please review them together.

On the CLI flow, a Mocha before / beforeEach / afterEach declared outside any describe reaches the BrowserStack binary with no file path. Every test passes, yet the build ends as timed out on Test Reporting & Analytics.

Mocha attaches such hooks to its root suite, and the root suite has no file. The CLI mocha framework read only test.file, so these hooks went out without test_file_path / test_location. The binary could not build the hook's file details, so it dropped the hook's HookRunStarted / HookRunFinished events. The test's TestRunFinished still referenced the hook id, so TRA kept a hook open that never closed. The build timeout then marked the whole build timed out.

The non-CLI flow never had this. Its insights handler falls back to the current suite file (test.file || this._suiteFile, fed from beforeSuite). This PR gives the CLI flow the same behaviour:

  • WdioMochaTestFramework.setSuiteFile(). beforeSuite feeds it on the CLI flow, the same call the non-CLI flow makes on its insights handler.
  • trackHookEvents and getTestData use test.file || this._suiteFile. The getTestData fallback was already there, commented out.

Root-level before. MochaAdapter registers wdio's beforeSuite as a root beforeAll only after the spec files have loaded, so a root-level before runs ahead of it, while the suite file is still unset. The service's before() hook runs before Mocha starts, so it now seeds the suite file from the worker's spec. The runner hands specs over as file:// URLs; they are converted the same way MochaAdapter converts them for suite.file. beforeSuite still updates the file, but a suite with no file no longer clears it. A root-level before on the non-CLI flow has no file either; there the hook events don't go through the binary, so nothing breaks.

Hooks declared inside a describe keep their own file, and nothing else changes.

When it shows up: CLI-flow hooks only started carrying ids in 8.49.x. Before that they were not reported at all. So suites with root-level hooks have hit this since 8.49.x.

Paired binary hardening (the binary also stops throwing on a hook with no file, which covers already-released SDK versions): browserstack/browserstack-binary#1988.

Verification

End to end, on real BrowserStack sessions. The sample spec has a root-level beforeEach / afterEach, a before / after in one describe, and its own beforeEach in a second describe. It runs 2 tests, both passing in every run.

@wdio/browserstack-service binary errors The "to" argument must be of type string root-level hooks' file
8.53.1 (published) 8: 2 tests × root beforeEach/afterEach start + finish none
8.54.0 + this PR 0 test/specs/root-hooks.spec.js on all 8 hooks

A second sample adds a root-level before and after to the same spec (10 hooks in all):

@wdio/browserstack-service binary errors root-level before
8.54.0 + this PR without the root-before seed (earlier head) 2 (its start + finish) no file; HookRunStarted / HookRunFinished never sent, still referenced by the first test's TestRunFinished
8.54.0 + this PR 0 test/specs/root-before.spec.js; the binary sends every hook with that file

Unit tests:

  • tests/cli/frameworks/wdioMochaTestFramework.test.ts (+3):
    • a file-less hook gets the suite file;
    • a describe-level hook keeps its own file;
    • test data uses the same fallback.
  • tests/service.test.ts (+3):
    • beforeSuite hands the suite file to the CLI mocha framework;
    • before() seeds it from the worker's spec (a file:// URL, decoded to the path);
    • a later suite with no file keeps the seeded spec.
  • The two before() tests fail without the seed. Both files pass in full on Node 22: 122/122 and 12/12. eslint (main's config) and tsc --noEmit are clean.

Related Jira task/s

  • SDK-8004 (TRA SDK ingestion stability)

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 builds being reported as timed out on Test Reporting & Analytics when a Mocha before / beforeEach / afterEach hook is declared outside any describe block.

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

  • The CLI mocha framework now falls back to the current suite file for hooks and test data (test.file || this._suiteFile, fed from beforeSuite), matching the non-CLI insights handler.
  • service.before() seeds that file from the worker's spec, because a root-level before runs ahead of the first beforeSuite. A suite with no file no longer clears it.
  • Root-level hooks previously went out with no file. The binary dropped their HookRun events, and TestRunFinished's reference to them left a never-closed hook that timed the build out.

Checklist

  • Ready to review
  • Has it been tested locally?

PR Validations

Run Tests: Comment RUN_TESTS to trigger sanity tests.

🤖 Generated with Claude Code

…flow does (v8)

Mocha attaches a beforeEach/afterEach declared outside any describe to its
root suite, which has no file, so the CLI flow sent these hooks with no path.
The binary threw on the missing path and dropped their HookRunStarted/Finished
events while the test still referenced the hook id, leaving a never-closed
hook that timed the build out.

The non-CLI flow falls back to the current suite file (set in beforeSuite);
the CLI mocha framework now does the same for hooks and test data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 26, 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
  • 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: 063a9d77-a2f4-4d65-8dfc-e4cf50aea2d7

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

Comment @coderabbitai help to get the list of available commands.

@rounak610
rounak610 marked this pull request as ready for review October 26, 2026 14:56
@rounak610
rounak610 requested a review from a team as a code owner October 26, 2026 14:56
@rounak610
rounak610 requested review from 07souravkunda and AakashHotchandani and removed request for a team October 26, 2026 14:56
@github-actions

Copy link
Copy Markdown
Contributor

🔴 SDK PR Review gate is red. Pending:

  • The SDK PR Review Agent has not been run on the current head commit yet — run the SDK PR Review Agent (its verdict is advisory; this gate only requires that it ran on the latest commit).

It turns green once the SDK PR Review Agent has run on the current head commit (any verdict — the gate only requires that the review ran). A native reviewer approval is separately required by branch protection before merge.

@github-actions

Copy link
Copy Markdown
Contributor

🔴 SDK PR Review gate is red. Pending:

  • The SDK PR Review Agent has not been run on the current head commit yet — run the SDK PR Review Agent (its verdict is advisory; this gate only requires that it ran on the latest commit).

It turns green once the SDK PR Review Agent has run on the current head commit (any verdict — the gate only requires that the review ran). A native reviewer approval is separately required by branch protection before merge.

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

🔴 SDK PR Review gate is red. Pending:

  • The SDK PR Review Agent has not been run on the current head commit yet — run the SDK PR Review Agent (its verdict is advisory; this gate only requires that it ran on the latest commit).

It turns green once the SDK PR Review Agent has run on the current head commit (any verdict — the gate only requires that the review ran). A native reviewer approval is separately required by branch protection before merge.

@rounak610

rounak610 commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

✅ Good to go

File Confidence
.changeset/pr-309.md ✅ All Clear
packages/browserstack-service/src/cli/frameworks/wdioMochaTestFramework.ts ✅ All Clear
packages/browserstack-service/src/service.ts ✅ All Clear
packages/browserstack-service/tests/cli/frameworks/wdioMochaTestFramework.test.ts ✅ All Clear
packages/browserstack-service/tests/service.test.ts ✅ All Clear

Change map (generated deterministically from the diff)

graph LR
  subgraph nwdio_service["wdio-service"]
    npackages_browserstack_service_src_cli_frameworks_wdioMochaTestFramework_ts["wdioMochaTestFramework.ts<br/>~32 lines"]
    npackages_browserstack_service_tests_cli_frameworks_wdioMochaTestFramework_test_ts["wdioMochaTestFramework.test.ts<br/>~32 lines"]
    npackages_browserstack_service_src_service_ts["service.ts<br/>~30 lines"]
    npackages_browserstack_service_tests_service_test_ts["service.test.ts<br/>~30 lines"]
    n_changeset_pr_309_md["pr-309.md<br/>~5 lines"]
  end
Loading

↻ This verdict comment is the review anchor — it's updated in place on each run (the gate posts its status separately).

— SDK PR Review Agent

@github-actions

Copy link
Copy Markdown
Contributor

🟢 SDK PR Review gate is green — the SDK PR Review Agent has run on the current head commit (verdict: success).

This gate confirms a review ran on the latest commit. The verdict itself is advisory — read the findings and use your judgement; it does not block merge. A native GitHub reviewer approval is still separately required by branch protection before this PR can merge.

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

🟢 SDK PR Review gate is green — the SDK PR Review Agent has run on the current head commit (verdict: success).

This gate confirms a review ran on the latest commit. The verdict itself is advisory — read the findings and use your judgement; it does not block merge. A native GitHub reviewer approval is still separately required by branch protection before this PR can merge.

@rounak610

Copy link
Copy Markdown
Collaborator Author

RUN_TESTS

@rounak610
rounak610 requested review from kamal-kaur04 and vivianludrick and removed request for 07souravkunda and AakashHotchandani October 26, 2026 16:56
@minionhelperappqa

Copy link
Copy Markdown

[SDK Wdio Test] TRA build state: failed | Stability 96% — verdict: success. Passed: 51, Failed: 2, Aggregate: 53. TRA: https://observability.browserstack.com/builds/txq8omihjgciupc0kbf1doofa8o4rvhg6tdn8afo

wdio registers its beforeSuite as a root beforeAll only after the spec files
load, so a before() declared outside any describe runs ahead of it. The suite
file the CLI mocha framework falls back to was still unset at that point and
the hook went out with no path, which the binary throws on.

Seed the fallback from the worker's spec in before(), converted from the file
URL the runner hands over the same way MochaAdapter does for suite.file.
beforeSuite still updates it, but no longer clears it when a suite has no
file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

🔴 SDK PR Review gate is red. Pending:

  • The SDK PR Review Agent has not been run on the current head commit yet — run the SDK PR Review Agent (its verdict is advisory; this gate only requires that it ran on the latest commit).

It turns green once the SDK PR Review Agent has run on the current head commit (any verdict — the gate only requires that the review ran). A native reviewer approval is separately required by branch protection before merge.

@github-actions

Copy link
Copy Markdown
Contributor

🟢 SDK PR Review gate is green — the SDK PR Review Agent has run on the current head commit (verdict: pending).

This gate confirms a review ran on the latest commit. The verdict itself is advisory — read the findings and use your judgement; it does not block merge. A native GitHub reviewer approval is still separately required by branch protection before this PR can merge.

@github-actions

Copy link
Copy Markdown
Contributor

🟢 SDK PR Review gate is green — the SDK PR Review Agent has run on the current head commit (verdict: success).

This gate confirms a review ran on the latest commit. The verdict itself is advisory — read the findings and use your judgement; it does not block merge. A native GitHub reviewer approval is still separately required by branch protection before this PR can merge.

@github-actions

Copy link
Copy Markdown
Contributor

🟢 SDK PR Review gate is green — the SDK PR Review Agent has run on the current head commit (verdict: success).

This gate confirms a review ran on the latest commit. The verdict itself is advisory — read the findings and use your judgement; it does not block merge. A native GitHub reviewer approval is still separately required by branch protection before this PR can merge.

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

🟢 SDK PR Review gate is green — the SDK PR Review Agent has run on the current head commit (verdict: success).

This gate confirms a review ran on the latest commit. The verdict itself is advisory — read the findings and use your judgement; it does not block merge. A native GitHub reviewer approval is still separately required by branch protection before this PR can merge.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant