Skip to content

fix(cli): TestHub acts on the dispatched test state, not the shared one (v9) - #413

Open
AakashHotchandani wants to merge 3 commits into
v9from
fix/cli-testhub-dispatched-state-v9
Open

AakashHotchandani wants to merge 3 commits into
v9from
fix/cli-testhub-dispatched-state-v9

Conversation

@AakashHotchandani

@AakashHotchandani AakashHotchandani commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

What is this about?

v9 port of #412. The v9 CLI flow has the same race. A mocha test that calls browser.reloadSession() mid-test, with a config afterTest that writes to the console, never gets its TestRunFinished. Test Hub then times the test out (status=timeout, finished_at NULL), which TRA counts under metadata_issue_non_timeout (n_missing_timestamps).

Cause (same as #412, cli/modules/testHubModule.ts):

  • onAllTestEvents reads the instance's shared current state, not the state it was dispatched for.
  • After a mid-test reload, AutomateModule.onAfterTest awaits the session rename before TestHub's observer runs.
  • Console output from the user's afterTest lands as LOG/POST during that wait and overwrites the state.
  • TestHub then handles the TEST/POST as a log and never stashes the finish.

Change: cherry-pick of #412 (git cherry-pick -x). It applied cleanly.

  • The observer registration passes the dispatched (state, hook) to onAllTestEvents, which uses them.
  • The LOG_REPORT recovery re-entry passes TEST.
  • The immediate sendTestFrameworkEvent passes the dispatched state as stateOverride.

Checks (local, Node 26):

  • vitest run tests/cli: 506 passed, 4 failed. The same 4 cliUtils network tests also fail on main.
  • The new race test fails with testHubModule.ts reverted (1 failed, 14 passed).
  • eslint on the changed files: clean.
  • I ran these against the clone's main (v10) node_modules. With those deps, tsc -p tsconfig.prod.json reports 78 errors on this branch both with and without the change, none in testHubModule.ts. A typecheck with v9 deps is left to CI.
  • The esbuild bundle (scripts/build.mjs) builds.

End to end (Automate, Windows 11 + Chrome):

  • Project: @wdio/cli / mocha-framework / webdriverio 9.33.1, with @wdio/browserstack-service 9.44.4 installed. The npm guard here would not install 9.44.5 yet.
  • For the last 3 rows, the package's build/index.js + cleanup.js were replaced with bundles built from this branch (origin/v9 9.44.5, with and without the fix).
  • Same spec as fix(cli): TestHub acts on the dispatched test state, not the shared one #412: 2 control tests, 1 plain test, and 1 test with a mid-test reloadSession(). The config afterTest logs, sleeps 150 ms, then logs again.
Build SDK TestRunFinished received by the binary Reload test finished Test Hub received (BQ_METRIC_TRACKER)
tyxh38jehsfczdqpmuh54vgmhua3eolmhujka0gl 9.44.4 (published) 3 / 4 no passed: 3
fiwymnknixzacxld5yf8immv5hkvfz97qtydfttc origin/v9 without the fix 3 / 4 no passed: 3
4ubygf0eecelen8fmvpwiyqmii1hhmirdxz2b3b6 this branch 4 / 4 yes passed: 4
j9ya5khqyks17fyhbtyxd86itusdklzobr3b65pa this branch 4 / 4 yes not synced yet

Not verified:

  • Test Hub's stored test rows for these builds (BigQuery sync lag). Every build received 4 TestRunStarted.
  • Cucumber, Jasmine and multiremote.

Related Jira task/s

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 a test being reported as timed out in Test Reporting & Analytics when it reloads the session mid-test and the afterTest hook writes console output.

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

Checklist

  • Ready to review
  • Has it been tested locally?

PR Validations

Run Tests: Comment RUN_TESTS to trigger sanity tests.

🤖 Generated with Claude Code

TestHub read the instance's current state when its observer ran. Observers run
one after another, so when an earlier TEST/POST observer awaits (the Automate
session rename after a mid-test reloadSession), console output from the user's
afterTest lands as LOG/POST in the meantime and overwrites that state. TestHub
then handled the TEST/POST as a log, never queued the finish, and the test was
left open until Test Hub timed it out (finished_at NULL).

Pass the dispatched state to onAllTestEvents and use it, including for the
immediate send.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 16b5606)
@coderabbitai

coderabbitai Bot commented Dec 4, 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: 6e96d32b-f0ad-4004-a5a8-067c62987ef4

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

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

Copy link
Copy Markdown
Collaborator Author

✅ Good to go — no blocking findings.

Reviewed at 478c449; the only change since (2bb8776) removes .changeset/testhub-dispatched-state.md, which resolves the one finding from that review.

File Status Reason
.changeset/pr-413.md ✅ All Clear Auto-generated by changeset-from-pr.yml
.changeset/testhub-dispatched-state.md ✅ Resolved Was byte-identical to pr-413.md (duplicate CHANGELOG bullet); removed in 2bb8776
packages/browserstack-service/src/cli/modules/testHubModule.ts ✅ All Clear Judged (two passes), no blocking finding
packages/browserstack-service/tests/cli/modules/testHubModule.deferredFinish.test.ts ✅ All Clear Judged (two passes), no blocking finding
packages/browserstack-service/tests/cli/modules/testHubModule.test.ts ✅ All Clear Judged, no blocking finding

Verified

  • Fix is correct. eventDispatcher.notifyObserver runs callbacks sequentially per ${state}:${hook}, but runHooks for different events interleave while an observer awaits — so the shared current state can be overwritten (e.g. by LOG/POST) mid-dispatch. Each callback now acts on the (state, hook) it was registered under; the log-entry lookup uses the dispatched state, the LOG_REPORT re-entry passes TEST, and the immediate send passes the override. Deferred/held flush paths already pin TEST/POST. The new parameters are optional, so direct callers keep the old behaviour.
  • Tests (against v9 deps): testHubModule* 31/31 pass. tests/cli: 498 pass on base v9 → 500 on this PR (+2 new); the 2 skipReporter*.test.ts suites fail to load identically on base (pre-existing vi.mock hoisting error). With the source fix reverted, the new race test fails (plus one assertion that tracks the call shape). tsc (prod config): 0 errors; eslint clean on the changed files.

💡 Follow-ups (non-blocking, outside this diff)

  • sendLogCreatedEvent (testHubModule.ts ~L402) and sendTestSessionEvent (~L299) still read the shared current state — a log entry can carry a stale state label (label only; the test finish isn't lost). customTagsModule.ts ~L176 and uploadAttachmentModule.ts ~L204 use the same pattern.
  • The new "LOG/POST still treated as a log" test also passes without the fix — fine as a regression guard, but it doesn't prove the fix (the race test does).
  • Cucumber, Jasmine and multiremote paths aren't covered (as the PR description notes).
  • main doesn't have this fix yet (testHubModule.ts L133 still calls this.onAllTestEvents(args)); fix(cli): TestHub acts on the dispatched test state, not the shared one #412 needs to land too so v10 isn't left behind.

Change map

graph LR
  subgraph nwdio_service["wdio-service"]
    n1["⚠ testHubModule.deferredFinish.test.ts<br/>~75 lines"]
    n2["testHubModule.ts<br/>~11 lines"]
    n3["testHubModule.test.ts<br/>~11 lines"]
    n4["pr-413.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

github-actions Bot commented Dec 4, 2026

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.

@AakashHotchandani

Copy link
Copy Markdown
Collaborator Author

RUN_TESTS

@minionhelperappqa

Copy link
Copy Markdown

[SDK Wdio Test] TRA build state: passed | Stability 100% — verdict: success. Passed: 58, Failed: 0, Aggregate: 58. TRA: https://observability.browserstack.com/builds/gqah1hflt9snyy1dl4mg9w3i8dq1xjlmwgm8uqyv

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