Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pr-264.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wdio/browserstack-service": patch
---

- 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.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export default class WdioMochaTestFramework extends TestFramework {
logger.info(`trackEvent: testFrameworkState=${testFrameworkState} hookState=${hookState}`)
await super.trackEvent(testFrameworkState, hookState, args)

// 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 and
// resolveInstance cannot create one for LOG. The classic path drops such a log silently; do the same instead of
// printing an ERROR on every worker (SDK-7843).
if (testFrameworkState === TestFrameworkState.LOG && !TestFramework.getTrackedInstance()) {
logger.debug(`trackEvent: no test or hook started yet, dropping log for testFrameworkState=${testFrameworkState} hookState=${hookState}`)
return
}

const instance = this.resolveInstance(testFrameworkState, hookState, args)
if (instance === null) {
logger.error(`trackEvent: instance not found for testFrameworkState=${testFrameworkState} hookState=${hookState}`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
import * as bstackLogger from '../../src/bstackLogger.js'

import WdioMochaTestFramework from '../../src/cli/frameworks/wdioMochaTestFramework.js'
import TestFramework from '../../src/cli/frameworks/testFramework.js'
import { TestFrameworkState } from '../../src/cli/states/testFrameworkState.js'
import { HookState } from '../../src/cli/states/hookState.js'
import { BStackLogger as cliLogger } from '../../src/cli/cliLogger.js'

vi.spyOn(bstackLogger.BStackLogger, 'logToFile').mockImplementation(() => {})

describe('SDK-7843 — a log written before the first mocha hook is dropped quietly', () => {
let framework: WdioMochaTestFramework
let errorSpy: ReturnType<typeof vi.spyOn>

beforeEach(() => {
framework = new WdioMochaTestFramework(['WebdriverIO', 'mocha'], {}, 'bin-session-id')
errorSpy = vi.spyOn(cliLogger, 'error').mockImplementation(() => {})
vi.spyOn(cliLogger, 'info').mockImplementation(() => {})
vi.spyOn(cliLogger, 'debug').mockImplementation(() => {})
})

afterEach(() => {
vi.restoreAllMocks()
})

it('does not log an ERROR for console output from wdio\'s `before` hook', async () => {
// wdio's `before` runs before mocha's `before all`, so no instance is tracked yet.
vi.spyOn(TestFramework, 'getTrackedInstance').mockReturnValue(null as any)
const resolveSpy = vi.spyOn(framework, 'resolveInstance')

await framework.trackEvent(TestFrameworkState.LOG, HookState.POST, {
logEntry: { kind: 'TEST_LOG', message: '[SelfHealer] Installed', level: 'info', timestamp: new Date().toISOString() }
})

expect(errorSpy).not.toHaveBeenCalled()
expect(resolveSpy).not.toHaveBeenCalled()
})

it('still resolves the instance for a log once a test or hook is tracked', async () => {
vi.spyOn(TestFramework, 'getTrackedInstance').mockReturnValue({} as any)
const resolveSpy = vi.spyOn(framework, 'resolveInstance').mockReturnValue(null)

await framework.trackEvent(TestFrameworkState.LOG, HookState.POST, { logEntry: {} })

expect(resolveSpy).toHaveBeenCalledOnce()
})

it('still reports a missing instance for non-log events', async () => {
vi.spyOn(TestFramework, 'getTrackedInstance').mockReturnValue(null as any)

await framework.trackEvent(TestFrameworkState.TEST, HookState.POST, {})

expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('resolveInstance: unable to resolve/create instance'))
})
})
Loading