Skip to content

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

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

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

Conversation

@AakashHotchandani

@AakashHotchandani AakashHotchandani commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

What is this about?

On the CLI flow, a mocha test that calls browser.reloadSession() mid-test never gets its TestRunFinished when the user's afterTest writes to the console. Test Hub later times the test out (status=timeout, finished_at NULL). TRA counts that under metadata_issue_non_timeout (n_missing_timestamps). Seen in production on 10.0.0. 10.0.1 has the same code.

Cause (cli/modules/testHubModule.ts):

  • onAllTestEvents takes the state from instance.getCurrentTestState() / getCurrentHookState(). It doesn't use the event it was dispatched for.
  • That state is shared. TestFramework.updateInstanceState overwrites it on every trackEvent, LOG events included.
  • notifyObserver runs the TEST/POST observers one at a time, and AutomateModule runs before TestHubModule.
  • After a mid-test reload, AutomateModule.onAfterTest awaits markSessionName (about 350 ms), because the new session was never named.
  • In that window the user's afterTest (which WDIO runs concurrently with the service's) logs. Each log becomes a LOG/POST trackEvent.
  • When TestHub runs, it reads LOG/POST, takes the log branch, and never stashes the finish. service.after() then has nothing to flush.

Probe log from a 10.0.0 repro (build ni5sgzkn5ejnsviosnjchqjvh0n5dls5euzmqpnl):

33.992 trackEvent: TEST/POST
33.993 Marking session name for Automate
34.138 trackEvent: LOG/POST        <- user afterTest console.log
34.341 Session name updated
34.341 [probe] dispatched=TEST/POST instanceState=LOG/POST
34.341 onAllTestEvents: TestFrameworkState.LOG - TestFrameworkState.LOG

In the binary's end-of-run report, the reload test is events=TestRunStarted complete=false, open=1. The other 3 tests read TEST/POST and were sent.

Change:

  • The observer registration passes the dispatched (state, hook) to onAllTestEvents, which uses them. It falls back to the instance state when they're absent (direct callers).
  • The LOG_REPORT recovery re-entry passes TEST explicitly.
  • The immediate sendTestFrameworkEvent passes the dispatched state as stateOverride, so a non-deferred event can't be labelled with a later event's state.

Checks (local, Node 26):

  • tsc -p tsconfig.prod.json --noEmit passes; eslint on the changed files is clean.
  • vitest run tests/cli: 505 passed, 4 failed. The same 4 fail on clean main (cliUtils requestToUpdateCLI ×3 and cliUtils.staleBinary, which need the network).
  • 2 new tests in testHubModule.deferredFinish.test.ts:
    • a TEST/POST dispatch whose instance already reads LOG/POST is stashed and sent as TEST/POST;
    • a LOG/POST dispatch is still handled as a log.
    • With testHubModule.ts reverted, the first one fails.
  • One existing assertion in testHubModule.test.ts was updated for the stateOverride argument.

End to end (Automate, Windows 11 + Chrome; same spec: 2 control tests, 1 plain test, 1 test with a mid-test reloadSession(); config afterTest logs, sleeps 150 ms, logs again):

Each build has 4 tests and 4 TestRunStarted. Columns: TestRunFinished received by the binary (sdk-cli.log), and received by TestHub (observability_error_data BQ_METRIC_TRACKER metrics).

Build SDK Binary TestHub
qhgvsfqwxkttjmrygcyonxf8qaiuv3rmrgnfany6 10.0.1, no afterTest 4 / 4 passed: 4
7sbk5fbbfzjtuj7flyhepug43haplsjzkoumybeg 10.0.1 3 / 4 (reload test missing) passed: 3
o3ggzublzuxfc1tt9r1sqetj6unwxubshcj3hbeo this branch 4 / 4 passed: 4
em3zh1jup3kenykleyulyynjqlt77izyozv6cp3e this branch 4 / 4 passed: 4
efxmkknq67s7hoqfxt1qvmn4ttkph2cefqnkfz8m this branch 4 / 4 passed: 4

On the unfixed runs, TestHub stored the reload test as status=timeout with finished_at NULL. That covers builds 180622794 (10.0.0), 180622797 (10.0.1) and 180626994 (the probe). The fixed builds' rows had not synced to BigQuery when this was written.

Not verified:

  • Cucumber, Jasmine and multiremote.
  • Fixed-build rows in the TestHub results table (BigQuery sync lag); the TestHub event metrics above are confirmed.
  • v9 has the same code. That port will come as a separate PR.

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)

  • TestHubModule's observer uses the dispatched TestFramework/Hook state instead of the instance's shared current state, which concurrent LOG events can overwrite while an earlier observer (the Automate session rename after a reload) is awaiting.

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>
@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: 79145926-e94f-4ced-9f65-b75dd9b86e97

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.

File Status Reason
.changeset/pr-412.md ✅ All Clear Auto-generated by changeset-from-pr.yml
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); covers the LOG-overwrote-state race
packages/browserstack-service/tests/cli/modules/testHubModule.test.ts ✅ All Clear Judged, no blocking finding

Verified

  • Fix is correct and matches the v9 port (fix(cli): TestHub acts on the dispatched test state, not the shared one (v9) #413). The added/removed lines in testHubModule.ts and both test files are identical to fix(cli): TestHub acts on the dispatched test state, not the shared one (v9) #413; only the changeset differs. Each callback acts on the (state, hook) it was registered under, so a LOG/POST that overwrites the shared current state mid-dispatch no longer misroutes the event.
  • main-only paths checked: the held/deferred finish (holdFinishUntilHooksEnd, mocha defer, in-flight tracking) is now reached from the dispatched TEST/POST even if the shared state reads LOG, and sendDeferredFinish already pins TEST/POST + uuid. The LOG_REPORT recovery re-enters with TEST and the dispatched hook state.
  • Regression test present: "queues and sends the finish when a LOG event overwrote the instance state first" (testHubModule.deferredFinish.test.ts) fails with the source fix reverted.
  • Tests (Node 26): testHubModule* 32/32 pass. tests/cli: 496 pass on base → 498 on this PR (+2 new); the 2 skipReporter*.test.ts suites fail to load identically on base (pre-existing). With the source fix reverted: 2 fail (the race test + the updated call-shape assertion). tsc (prod config): 0 errors; eslint clean on the changed files.

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

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-412.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

@AakashHotchandani

Copy link
Copy Markdown
Collaborator Author

RUN_TESTS

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

@minionhelperappqa

Copy link
Copy Markdown

[SDK Wdio Test] TRA build state: failed | Stability 79% — verdict: failure. Passed: 38, Failed: 10, Aggregate: 48. TRA: https://observability.browserstack.com/builds/hy1hwt7abenyam8poibs1xsfgrwdd3mvddvcpzk8

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