Skip to content

fix(cli): drop a log written before the first mocha hook instead of logging an ERROR (SDK-7843) [v8] - #265

Open
AakashHotchandani wants to merge 3 commits into
v8from
fix/SDK-7843-quiet-pre-test-log-v8
Open

AakashHotchandani wants to merge 3 commits into
v8from
fix/SDK-7843-quiet-pre-test-log-v8

Conversation

@AakashHotchandani

@AakashHotchandani AakashHotchandani commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

What is this about?

Any console output from wdio's before/beforeSession hooks makes every worker print two ERROR lines when the BrowserStack CLI is running:

ERROR @wdio/browserstack-service/cli: resolveInstance: unable to resolve/create instance for testFrameworkState=TestFrameworkState.LOG hookState=HookState.POST
ERROR @wdio/browserstack-service/cli: trackEvent: instance not found for testFrameworkState=TestFrameworkState.LOG hookState=HookState.POST

A customer hit this (SDK-7843). Their before hook awaits driver.setGeoLocation() and then logs [SelfHealer] Installed ....

Why it happens:

  • wdio's before runs before mocha's first hook. The service has already patched console by then, so the line goes to appendTestItemLog → trackEvent(LOG, POST).
  • No TestFramework instance exists yet. resolveInstance only creates one for NONE, INIT_TEST and hook PRE, so it returns null.
  • That null check and its ERROR log came in with 9.30.0 (Version Packages #47 / 7d6f822). Before 9.30.0 the case was silent.

Customer SDK log, worker 0-0:

time event
16:26:14.662 trackEvent: testFrameworkState=TestFrameworkState.LOG hookState=HookState.POST
16:26:14.663 ERROR resolveInstance: unable to resolve/create instance ...
16:26:14.670 trackWdioMochaInstance: created instance ... state=TestFrameworkState.BEFORE_ALL (the first instance)

Impact is cosmetic. Only that one pre-test line can't be attributed. The classic path drops it silently as well, because the line has no hook or test uuid. Customers still read the ERROR as a broken run.

The fix: in WdioMochaTestFramework.trackEvent, if a LOG event arrives while no instance is tracked, log at debug and return. This matches the classic path. Everything else is unchanged:

  • Logs inside a hook or test still resolve and upload as before.
  • A non-LOG event with no instance still reports the ERROR, since that remains a real tracking gap.

Verification

Reproduced on a real Automate session (chrome/Win11, WDIO 8 + mocha, Test Reporting on). The repro is a before hook that awaits one driver command and then calls console.log, the same shape as the customer's config.

Timing matters: a console.log placed before any await runs before the service's own before has patched console. That log is never captured, so it never errors.

build [SelfHealer] line ERROR pair in-test log reaches TRA (LogCreated in sdk-cli.log)
smosjpekx4gxcihtk0v7vwmtj6fcxpqloozfam0f stock 8.53.1 printed yes yes
djju5rlyfb0sd7n7yjagpyucu7rljbszifpx5hbm this PR printed no yes

Tests: new tests/cli/wdioMochaTestFramework.preTestLog.test.ts has 3 cases:

  • a pre-test LOG is dropped without an ERROR or a resolveInstance call;
  • a LOG with a tracked instance still resolves;
  • a non-LOG event with no instance still reports the ERROR.

The first case fails on the unfixed source.

Checks: the new file and the existing tests/cli/frameworks/wdioMochaTestFramework.test.ts pass (12/12). tsc -p tsconfig.prod.json is clean. The v8 branch has no eslint config.

v8 port of #264 (same change, same test).

Related Jira task/s

SDK-7843

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)

  • Removed misleading resolveInstance: unable to resolve/create instance ... TestFrameworkState.LOG / trackEvent: instance not found error messages. They were printed when a wdio before hook writes to the console. Test execution and reporting were never affected.

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

  • WdioMochaTestFramework.trackEvent now returns early, at debug level, for TestFrameworkState.LOG when TestFramework.getTrackedInstance() is empty. Console output from wdio's before/beforeSession hooks arrives before mocha's first hook, so resolveInstance cannot create an instance for it. Since 9.30.0 that printed two ERROR lines per worker. The classic path already drops such a log (no hook/test uuid), so nothing reported to TRA changes.
  • Non-LOG events with no instance still log the ERROR.

Checklist

  • Ready to review
  • Has it been tested locally?

PR Validations

Run Tests: Comment RUN_TESTS to trigger sanity tests.

🤖 Generated with Claude Code

…ogging an ERROR (SDK-7843)

Console output from wdio's `before`/`beforeSession` hooks reaches
trackEvent(LOG, POST) before mocha's first hook, so no TestFramework
instance exists and resolveInstance cannot create one for LOG. Since
9.30.0 that printed "resolveInstance: unable to resolve/create instance"
and "trackEvent: instance not found" at ERROR level on every worker.
Drop it at debug level instead, matching the classic path, which already
ignores a log that has no hook or test uuid.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AakashHotchandani
AakashHotchandani requested a review from a team as a code owner October 5, 2026 12:19
@AakashHotchandani
AakashHotchandani requested review from 07souravkunda and shivam5643 and removed request for a team October 5, 2026 12:19
@coderabbitai

coderabbitai Bot commented Oct 5, 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
  • Configuration used: Central YAML (base), Organization UI (inherited), Workspace UI (inherited)
  • Review profile: ASSERTIVE
  • Plan: Enterprise
  • Run ID: 09cf5d7a-f5cb-4145-8e7c-0493703063dc

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
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@AakashHotchandani AakashHotchandani left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Automated SDK PR Review

Summary

Intent: Stop WdioMochaTestFramework.trackEvent from logging the resolveInstance: unable to resolve/create instance / trackEvent: instance not found ERROR pair when console output from a wdio before hook (after the service patched console) arrives as TestFrameworkState.LOG before any mocha hook has created a TestFramework instance (SDK-7843; regression from 7d6f822, first shipped in 9.30.0). When no instance is tracked for the worker, a LOG event is now dropped at debug level, matching the classic path and the CLI cucumber framework. Non-LOG events with no instance still log ERROR. Same fix as #264 on main.
Risk: Low
0 critical · 0 warnings · 2 suggestions | Files reviewed: 3

The guard is exactly as wide as the failure it silences. For LOG, resolveInstance returns null only when TestFramework.getTrackedInstance() (key ${pid}:${threadId}) is null. TestFramework.instances is never cleared, so the drop can only fire before the first mocha hook on each worker. after/afterSession, between-test and late-async logs still resolve as before, and nothing sent to TRA changes. The fix matches the existing no-instance-LOG handling in WdioCucumberTestFramework and the classic path. Same fix as #264 on main: the src/test patches and changeset are identical, and trackEvent/resolveInstance/TestFramework are identical on both lines. No proto/binary change.

See inline comments below for full detail on each finding.

Verdict

✅ Good to go — all 3 files are 🟢. The suggestions are optional polish (code-comment accuracy; test placement on v8). Same fix as #264 on main.

Rovo enrichment unavailable — review based on local docs only.

Generated by Automated SDK PR review.

logger.info(`trackEvent: testFrameworkState=${testFrameworkState} hookState=${hookState}`)
await super.trackEvent(testFrameworkState, hookState, args)

// Console output from wdio's `before`/`beforeSession` hooks arrives before mocha's first

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

💡 Suggestion — [DOCS] Code comment names beforeSession, which can never reach this path

Problem

The new comment says console output from wdio's before/beforeSession hooks lands here. That is only true for before. Console is patched by patchConsoleLogs(), which the service calls inside its own before() (src/service.ts:262). A user's beforeSession always runs earlier, so its console.* output goes to the original methods and never reaches appendTestItemLog. The PR body says the same thing in its "Timing matters" note, and the changeset correctly mentions only before. A reader debugging a later attribution issue could be sent looking at beforeSession for no reason.

Suggested Fix

Name only the hook that can actually hit the guard:

// Console output from wdio's `before` hook (after the service has patched console) arrives
// before mocha's first hook, so there is no test or hook to attach it to yet ...

Confidence: 🟢 Objectively verifiable: patchConsoleLogs() is only called from the service's before(), which wdio runs after beforeSession.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Valid, fixed in 18f5f61. Same wording change as #264 (85a794f): the comment names only before, because beforeSession runs before the service patches console.

@@ -0,0 +1,56 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

💡 Suggestion — [TESTING] New test sits outside v8's tests/cli/frameworks/ home for mocha framework tests

Problem

On v8, the existing WdioMochaTestFramework tests live in tests/cli/frameworks/wdioMochaTestFramework.test.ts, which uses ../../../src imports. This file is placed at tests/cli/ (the main layout, where wdioMochaTestFramework.*.test.ts siblings live at that level). It works either way. Moving it to tests/cli/frameworks/ on v8, or folding the 3 cases into the existing file, keeps the v8 tree consistent. Keeping it identical to #264 for cherry-pick symmetry is also a reasonable choice.

Suggested Fix

Move the file to packages/browserstack-service/tests/cli/frameworks/wdioMochaTestFramework.preTestLog.test.ts and change the imports from ../../src/... to ../../../src/.... Alternatively, append the describe block to the existing tests/cli/frameworks/wdioMochaTestFramework.test.ts.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not changing this one. I'm keeping the file identical to #264 (same path, same ../../src imports) so the test stays a straight cherry-pick between main and v8. Splitting the layout would make the next port of this file diverge. It runs fine where it is: 12/12 together with the existing tests/cli/frameworks/wdioMochaTestFramework.test.ts. As the suggestion itself notes, symmetry with #264 is a reasonable choice here.

… comment (SDK-7843)

`beforeSession` runs before the service's own before() patches console, so its
output never reaches appendTestItemLog.

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

github-actions Bot commented Oct 5, 2026

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.

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