From 16b56067b4e5483abcf5164d1307b3da1abc83b0 Mon Sep 17 00:00:00 2001 From: Aakash Hotchandani Date: Fri, 4 Dec 2026 13:18:16 +0530 Subject: [PATCH 1/3] fix(cli): TestHub acts on the dispatched test state, not the shared one 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 --- .changeset/testhub-dispatched-state.md | 5 ++ .../src/cli/modules/testHubModule.ts | 21 ++++-- .../testHubModule.deferredFinish.test.ts | 75 +++++++++++++++++++ .../tests/cli/modules/testHubModule.test.ts | 2 +- 4 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 .changeset/testhub-dispatched-state.md diff --git a/.changeset/testhub-dispatched-state.md b/.changeset/testhub-dispatched-state.md new file mode 100644 index 00000000..bdcc55fd --- /dev/null +++ b/.changeset/testhub-dispatched-state.md @@ -0,0 +1,5 @@ +--- +"@wdio/browserstack-service": patch +--- + +- 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. diff --git a/packages/browserstack-service/src/cli/modules/testHubModule.ts b/packages/browserstack-service/src/cli/modules/testHubModule.ts index 4bcee90c..2a8bf38c 100644 --- a/packages/browserstack-service/src/cli/modules/testHubModule.ts +++ b/packages/browserstack-service/src/cli/modules/testHubModule.ts @@ -72,7 +72,10 @@ export default class TestHubModule extends BaseModule { Object.values(TestFrameworkState).forEach(state => { Object.values(HookState).forEach(hook => { - TestFramework.registerObserver(state, hook, this.onAllTestEvents.bind(this)) + // Pass the event's own state: observers run one after another, and an earlier + // one that awaits (e.g. the Automate session rename after a mid-test reload) lets + // concurrent LOG events overwrite the instance's current state before this runs. + TestFramework.registerObserver(state, hook, (args: Record) => this.onAllTestEvents(args, state, hook)) }) }) } @@ -93,11 +96,14 @@ export default class TestHubModule extends BaseModule { this.sendTestSessionEvent(args) } - onAllTestEvents(args: Record) { + onAllTestEvents(args: Record, dispatchedTestState?: State, dispatchedHookState?: State) { this.logger.debug('onAllTestEvents: Called after all test events from cli configured module!!!') const instance = args.instance as TestFrameworkInstance - const testState = instance.getCurrentTestState() - const hookState = instance.getCurrentHookState() + // The instance's current state is shared and moves with every tracked event, so prefer + // the state this observer was dispatched for. Otherwise a TEST/POST that reaches this + // observer after a LOG/POST landed is handled as a log and its finish is never sent. + const testState = dispatchedTestState ?? instance.getCurrentTestState() + const hookState = dispatchedHookState ?? instance.getCurrentHookState() const keyTestDeferred = TestFramework.getState(instance, TestFrameworkConstants.KEY_TEST_DEFERRED) // A NEW test is starting (INIT_TEST minted a fresh instance) — the previous test's @@ -130,7 +136,7 @@ export default class TestHubModule extends BaseModule { ) { // Create a modified args object with updated test framework state instance.setCurrentTestState(TestFrameworkState.TEST) - this.onAllTestEvents(args) + this.onAllTestEvents(args, TestFrameworkState.TEST, hookState) } if (testState === TestFrameworkState.TEST || CLIUtils.matchHookRegex(testState.toString().split('.')[1])) { @@ -158,7 +164,10 @@ export default class TestHubModule extends BaseModule { this.pendingTestFinishes.set(deferUuid, { args, uuid: deferUuid }) this.logger.debug(`onAllTestEvents: deferred TEST/POST send past the after-each hook window (uuid=${deferUuid}, pending=${this.pendingTestFinishes.size})`) } else { - this.sendTestFrameworkEvent(args) + this.sendTestFrameworkEvent(args, { + testFrameworkState: testState.toString().split('.')[1], + testHookState: hookState.toString().split('.')[1] + }) } } } diff --git a/packages/browserstack-service/tests/cli/modules/testHubModule.deferredFinish.test.ts b/packages/browserstack-service/tests/cli/modules/testHubModule.deferredFinish.test.ts index 48071a34..53f86df9 100644 --- a/packages/browserstack-service/tests/cli/modules/testHubModule.deferredFinish.test.ts +++ b/packages/browserstack-service/tests/cli/modules/testHubModule.deferredFinish.test.ts @@ -354,3 +354,78 @@ describe('TestHubModule — finish held until the TEST/POST observers end (cucum expect(testHubModule.flushPendingTestFinishEvent()).toBeUndefined() }) }) + +describe('TestHubModule — finish uses the dispatched state, not the shared instance state', () => { + let testHubModule: TestHubModule + let mockGrpcClient: { testFrameworkEvent: ReturnType, logCreatedEvent: ReturnType } + + // A mocha test whose instance was moved to LOG/POST by console output from the user's + // afterTest while an earlier TEST/POST observer (the Automate session rename after a + // mid-test reload) was still awaiting. + function makeInstanceOverwrittenByLog(uuid: string) { + return { + ...makeMochaTestInstance(uuid), + getCurrentTestState: () => TestFrameworkState.LOG, + getCurrentHookState: () => HookState.POST + } + } + + function registeredObserver(state: unknown, hook: unknown) { + const call = vi.mocked(TestFramework.registerObserver).mock.calls.find(([s, h]) => s === state && h === hook) + return call?.[2] as (args: Record) => void + } + + beforeEach(() => { + vi.clearAllMocks() + process.env.WDIO_WORKER_ID = '0-1' + mockGrpcClient = { + testFrameworkEvent: vi.fn().mockResolvedValue({ success: true }), + logCreatedEvent: vi.fn().mockResolvedValue({ success: true }) + } + vi.mocked(GrpcClient.getInstance).mockReturnValue(mockGrpcClient as never) + vi.mocked(TestFramework.hasState).mockReturnValue(true) + vi.mocked(TestFramework.getState).mockImplementation((instance: any, key: unknown) => { + if (key === TestFrameworkConstants.KEY_TEST_FRAMEWORK_NAME) { + return 'WebdriverIO-mocha' + } + if (key === TestFrameworkConstants.KEY_TEST_DEFERRED) { + return false + } + if (key === TestFrameworkConstants.KEY_TEST_UUID) { + return instance?.__uuid + } + return '' + }) + testHubModule = new TestHubModule({ enabled: true, hubUrl: 'https://hub.browserstack.com' }) + Object.defineProperty(testHubModule, 'config', { + value: { hubUrl: 'https://hub.browserstack.com' }, + writable: true + }) + }) + + afterEach(() => { + vi.resetAllMocks() + delete process.env.WDIO_WORKER_ID + }) + + it('queues and sends the finish when a LOG event overwrote the instance state first', async () => { + const observer = registeredObserver(TestFrameworkState.TEST, HookState.POST) + expect(observer).toBeTypeOf('function') + + observer({ instance: makeInstanceOverwrittenByLog('reload'), test: { title: 'reload' } as Frameworks.Test }) + await testHubModule.flushPendingTestFinishEvent() + + expect(mockGrpcClient.testFrameworkEvent).toHaveBeenCalledTimes(1) + expect(mockGrpcClient.testFrameworkEvent).toHaveBeenCalledWith( + expect.objectContaining({ testFrameworkState: 'TEST', testHookState: 'POST', uuid: 'reload' }) + ) + }) + + it('still handles a LOG/POST dispatch as a log', () => { + const observer = registeredObserver(TestFrameworkState.LOG, HookState.POST) + observer({ instance: makeInstanceOverwrittenByLog('log'), test: { title: 'log' } as Frameworks.Test }) + + expect(testHubModule.flushPendingTestFinishEvent()).toBeUndefined() + expect(mockGrpcClient.testFrameworkEvent).not.toHaveBeenCalled() + }) +}) diff --git a/packages/browserstack-service/tests/cli/modules/testHubModule.test.ts b/packages/browserstack-service/tests/cli/modules/testHubModule.test.ts index 47316f20..e1d19696 100644 --- a/packages/browserstack-service/tests/cli/modules/testHubModule.test.ts +++ b/packages/browserstack-service/tests/cli/modules/testHubModule.test.ts @@ -223,7 +223,7 @@ describe('TestHubModule', () => { await testHubModule.onAllTestEvents(mockArgs) - expect(sendTestFrameworkEventSpy).toHaveBeenCalledWith(mockArgs) + expect(sendTestFrameworkEventSpy).toHaveBeenCalledWith(mockArgs, { testFrameworkState: 'TEST', testHookState: 'PRE' }) }) }) From 24f60ee19821a717ce9bea623486281a88a362a6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 4 Dec 2026 07:48:57 +0000 Subject: [PATCH 2/3] chore(changeset): auto-generate from PR template (patch) --- .changeset/pr-412.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pr-412.md diff --git a/.changeset/pr-412.md b/.changeset/pr-412.md new file mode 100644 index 00000000..bdcc55fd --- /dev/null +++ b/.changeset/pr-412.md @@ -0,0 +1,5 @@ +--- +"@wdio/browserstack-service": patch +--- + +- 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. From 196656cd436a4d826a96c4983fbe269b419265f3 Mon Sep 17 00:00:00 2001 From: Aakash Hotchandani Date: Fri, 4 Dec 2026 15:05:30 +0530 Subject: [PATCH 3/3] chore: drop manual changeset; the PR-template changeset covers it Co-Authored-By: Claude Opus 5.5 --- .changeset/testhub-dispatched-state.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .changeset/testhub-dispatched-state.md diff --git a/.changeset/testhub-dispatched-state.md b/.changeset/testhub-dispatched-state.md deleted file mode 100644 index bdcc55fd..00000000 --- a/.changeset/testhub-dispatched-state.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@wdio/browserstack-service": patch ---- - -- 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.