-
Notifications
You must be signed in to change notification settings - Fork 7
fix: report skipped and hook-aborted tests in the CLI flow #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kamal-kaur04
wants to merge
3
commits into
main
Choose a base branch
from
fix/cli-skipped-tests-reporting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6d8c086
fix(browserstack-service): report skipped and hook-aborted tests in t…
kamal-kaur04 756c675
fix: clearer batch-upload failures; guard sync-skip hook shape
kamal-kaur04 4836f87
Merge pull request #55 from kamal-kaur04/fix/cli-skipped-tests-reporting
kamal-kaur04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import path from 'node:path' | ||
|
|
||
| import type { Frameworks } from '@wdio/types' | ||
|
|
||
| import type TestFramework from './frameworks/testFramework.js' | ||
| import { TestFrameworkState } from './states/testFrameworkState.js' | ||
| import { HookState } from './states/hookState.js' | ||
| import { BStackLogger } from '../bstackLogger.js' | ||
|
|
||
| /** | ||
| * Reports tests that never reach the beforeTest/afterTest lifecycle (static `it.skip`, | ||
| * `this.skip()` inside before hooks, suites aborted by a failed before hook) through the | ||
| * CLI gRPC tracker, so they land on the dashboard and attribute their Automate session. | ||
| * Without this, such tests emit no events at all in the CLI flow (the legacy | ||
| * Listener -> api/v1/batch path is dead here) and their sessions surface as | ||
| * `session_linking_issue_build` in TRA stability. | ||
| */ | ||
|
|
||
| interface MochaRuntimeTest { | ||
| title: string | ||
| state?: string | ||
| body?: string | ||
| file?: string | ||
| parent?: { title?: string, parent?: unknown, tests?: unknown[], suites?: unknown[] } | ||
| } | ||
|
|
||
| // tests that entered the CLI beforeTest lifecycle — those report their own finish | ||
| // (including runtime `this.skip()` inside a test body) and must not be re-reported | ||
| const startedTests = new Set<string>() | ||
| const reportedSkips = new Set<string>() | ||
| // wdio does not await reporter hooks, so back-to-back skips would interleave on the | ||
| // tracker's single mutable per-worker instance — serialize every report through one chain | ||
| let reportChain: Promise<void> = Promise.resolve() | ||
|
|
||
| export function markTestStarted(identifier: string) { | ||
| startedTests.add(identifier) | ||
| } | ||
|
|
||
| export function reportSkippedTest(framework: TestFramework, identifier: string, test: Frameworks.Test, suiteTitle?: string): Promise<void> { | ||
| if (startedTests.has(identifier) || reportedSkips.has(identifier)) { | ||
| return reportChain | ||
| } | ||
| reportedSkips.add(identifier) | ||
| const result = { passed: false, skipped: true } as Frameworks.TestResult | ||
| reportChain = reportChain.then(async () => { | ||
| // LOG_REPORT/POST is what loads the result into the instance (loadTestResult is | ||
| // gated on it, not on TEST/POST) — same sequence afterTest uses | ||
| await framework.trackEvent(TestFrameworkState.INIT_TEST, HookState.PRE, { test }) | ||
| await framework.trackEvent(TestFrameworkState.TEST, HookState.PRE, { test, suiteTitle }) | ||
| await framework.trackEvent(TestFrameworkState.LOG_REPORT, HookState.POST, { test, result }) | ||
| await framework.trackEvent(TestFrameworkState.TEST, HookState.POST, { test, result, suiteTitle }) | ||
| }).catch((err: unknown) => { | ||
| BStackLogger.debug(`Failed reporting skipped test '${identifier}': ${err}`) | ||
| }) | ||
| return reportChain | ||
| } | ||
|
|
||
| /** | ||
| * Port of the legacy insights-handler skip propagation: when a BEFORE_ALL/BEFORE_EACH/ | ||
| * AFTER_EACH hook fails (or skips), mocha silently drops the remaining tests in the | ||
| * suite — report each state-undefined test as skipped, recursing into nested describes. | ||
| */ | ||
| export async function reportSuiteSkipped(framework: TestFramework, suite: { tests?: unknown[], suites?: unknown[] }): Promise<void> { | ||
| for (const t of (suite.tests || []) as MochaRuntimeTest[]) { | ||
| if (t.state !== undefined) { | ||
| continue | ||
| } | ||
| const parentTitle = t.parent?.title ?? '' | ||
| const identifier = `${parentTitle} - ${t.title}` | ||
| // keep `parent` a string (the Automate session name interpolates it) and pass the | ||
| // real suite chain via ctx for scope/hierarchy extraction; `file` must resolve or | ||
| // the binary drops the event on path.relative(cwd, undefined) | ||
| const synthetic = { | ||
| title: t.title, | ||
| parent: parentTitle, | ||
| body: t.body || '', | ||
| file: t.file, | ||
| ctx: { test: { parent: t.parent } } | ||
| } as unknown as Frameworks.Test | ||
| await reportSkippedTest(framework, identifier, synthetic, parentTitle) | ||
| } | ||
| for (const sub of (suite.suites || []) as { tests?: unknown[], suites?: unknown[] }[]) { | ||
| await reportSuiteSkipped(framework, sub) | ||
| } | ||
| } | ||
|
|
||
| /** Resolve a spec file path usable by the binary (it rejects events with no location). */ | ||
| export function resolveSpecFile(candidate: string | undefined, runnerSpec: string | undefined): string | undefined { | ||
| if (candidate) { | ||
| return candidate | ||
| } | ||
| if (runnerSpec) { | ||
| return runnerSpec.startsWith('file://') ? runnerSpec.replace(/^file:\/\//, '') : path.resolve(runnerSpec) | ||
| } | ||
| return undefined | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/browserstack-service/tests/cli/skipReporter.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import path from 'node:path' | ||
|
|
||
| import { describe, expect, it, vi, beforeEach } from 'vitest' | ||
| import type { Frameworks } from '@wdio/types' | ||
|
|
||
| vi.mock('@wdio/logger', () => import(path.join(process.cwd(), '__mocks__', '@wdio/logger'))) | ||
|
|
||
| import { markTestStarted, reportSkippedTest, reportSuiteSkipped, resolveSpecFile } from '../../src/cli/skipReporter.js' | ||
| import { TestFrameworkState } from '../../src/cli/states/testFrameworkState.js' | ||
| import { HookState } from '../../src/cli/states/hookState.js' | ||
| import type TestFramework from '../../src/cli/frameworks/testFramework.js' | ||
|
|
||
| const makeFramework = () => ({ trackEvent: vi.fn().mockResolvedValue(undefined) }) as unknown as TestFramework | ||
|
|
||
| const makeTest = (title: string, parent = 'suite') => ({ title, parent }) as unknown as Frameworks.Test | ||
|
|
||
| describe('skipReporter', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('reports a skipped test through the INIT_TEST/TEST/LOG_REPORT sequence', async () => { | ||
| const framework = makeFramework() | ||
| await reportSkippedTest(framework, 'suite - reports once', makeTest('reports once'), 'suite') | ||
|
|
||
| const calls = vi.mocked(framework.trackEvent).mock.calls | ||
| expect(calls.map(([state, hook]) => [state, hook])).toEqual([ | ||
| [TestFrameworkState.INIT_TEST, HookState.PRE], | ||
| [TestFrameworkState.TEST, HookState.PRE], | ||
| [TestFrameworkState.LOG_REPORT, HookState.POST], | ||
| [TestFrameworkState.TEST, HookState.POST], | ||
| ]) | ||
| expect(calls[2][2]).toMatchObject({ result: { passed: false, skipped: true } }) | ||
| }) | ||
|
|
||
| it('does not re-report the same identifier', async () => { | ||
| const framework = makeFramework() | ||
| await reportSkippedTest(framework, 'suite - dedup', makeTest('dedup'), 'suite') | ||
| await reportSkippedTest(framework, 'suite - dedup', makeTest('dedup'), 'suite') | ||
| expect(framework.trackEvent).toHaveBeenCalledTimes(4) | ||
| }) | ||
|
|
||
| it('does not report tests that entered the beforeTest lifecycle', async () => { | ||
| const framework = makeFramework() | ||
| markTestStarted('suite - runtime skip') | ||
| await reportSkippedTest(framework, 'suite - runtime skip', makeTest('runtime skip'), 'suite') | ||
| expect(framework.trackEvent).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('serializes concurrent reports through one chain', async () => { | ||
| const order: string[] = [] | ||
| const framework = { | ||
| trackEvent: vi.fn().mockImplementation(async (_s: unknown, _h: unknown, args: { test: { title: string } }) => { | ||
| order.push(args.test.title) | ||
| await new Promise(resolve => setTimeout(resolve, 1)) | ||
| }) | ||
| } as unknown as TestFramework | ||
|
|
||
| await Promise.all([ | ||
| reportSkippedTest(framework, 'suite - first', makeTest('first'), 'suite'), | ||
| reportSkippedTest(framework, 'suite - second', makeTest('second'), 'suite'), | ||
| ]) | ||
| expect(order).toEqual(['first', 'first', 'first', 'first', 'second', 'second', 'second', 'second']) | ||
| }) | ||
|
|
||
| it('walks nested suites and skips already-determined tests', async () => { | ||
| const framework = makeFramework() | ||
| const parent = { title: 'outer' } | ||
| const suite = { | ||
| tests: [ | ||
| { title: 'ran already', state: 'passed', parent }, | ||
| { title: 'undetermined', parent, file: '/spec/a.spec.js' }, | ||
| ], | ||
| suites: [{ | ||
| tests: [{ title: 'nested undetermined', parent: { title: 'inner' } }], | ||
| suites: [], | ||
| }], | ||
| } | ||
| await reportSuiteSkipped(framework, suite) | ||
| // 2 undetermined tests x 4 tracker events | ||
| expect(framework.trackEvent).toHaveBeenCalledTimes(8) | ||
| const reported = vi.mocked(framework.trackEvent).mock.calls | ||
| .filter(([state]) => state === TestFrameworkState.INIT_TEST) | ||
| .map(([, , args]) => (args as { test: { title: string } }).test.title) | ||
| expect(reported).toEqual(['undetermined', 'nested undetermined']) | ||
| }) | ||
|
|
||
| it('resolves spec file from the runner spec when the test has none', () => { | ||
| expect(resolveSpecFile('/abs/spec.js', 'file:///runner/spec.js')).toBe('/abs/spec.js') | ||
| expect(resolveSpecFile(undefined, 'file:///runner/spec.js')).toBe('/runner/spec.js') | ||
| expect(resolveSpecFile(undefined, undefined)).toBeUndefined() | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't checked this myself -> Is there a better way to check this than asserting error message string? (Asking because string literals might change in the future without us realising the impact)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, skipped check should suffice, if at all string changes, worst case it will marked as failure. Shouldn't be an issues since testResult itself was not a skipped status.