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-265.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 @@ -37,6 +37,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'

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.

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