Skip to content

Commit dff3b46

Browse files
committed
Unit test case for service package (reporter and utils)
1 parent cbb7652 commit dff3b46

2 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest'
2+
import { TestReporter } from '../src/reporter.js'
3+
import type { TestStats, SuiteStats } from '@wdio/reporter'
4+
5+
describe('TestReporter - Rerun & Stable UID', () => {
6+
let reporter: TestReporter
7+
let sendUpstream: ReturnType<typeof vi.fn>
8+
9+
const createTestStats = (overrides: Partial<TestStats> = {}): TestStats =>
10+
({
11+
uid: 'test-123',
12+
title: 'should login',
13+
fullTitle: 'Login Suite should login',
14+
file: '/test/login.spec.ts',
15+
parent: 'suite-1',
16+
cid: '0-0',
17+
...overrides
18+
}) as TestStats
19+
20+
const createSuiteStats = (overrides: Partial<SuiteStats> = {}): SuiteStats =>
21+
({
22+
uid: 'suite-123',
23+
title: 'Login Suite',
24+
fullTitle: 'Login Suite',
25+
file: '/test/login.spec.ts',
26+
cid: '0-0',
27+
...overrides
28+
}) as SuiteStats
29+
30+
beforeEach(() => {
31+
sendUpstream = vi.fn()
32+
reporter = new TestReporter(
33+
{ logFile: '/tmp/test.log' },
34+
sendUpstream as any
35+
)
36+
})
37+
38+
describe('stable UID generation for reruns', () => {
39+
it('should generate consistent UIDs for test reruns', () => {
40+
const testStats1 = createTestStats()
41+
const testStats2 = createTestStats({ uid: 'test-456' })
42+
43+
reporter.onTestStart(testStats1)
44+
const uid1 = (testStats1 as any).uid
45+
46+
reporter = new TestReporter(
47+
{ logFile: '/tmp/test.log' },
48+
sendUpstream as any
49+
)
50+
reporter.onTestStart(testStats2)
51+
const uid2 = (testStats2 as any).uid
52+
53+
expect(uid1).toBe(uid2)
54+
expect(uid1).toContain('stable-')
55+
})
56+
57+
it('should generate unique UIDs for different tests', () => {
58+
const tests = [
59+
createTestStats({
60+
uid: 'test-1',
61+
title: 'test A',
62+
fullTitle: 'Suite test A'
63+
}),
64+
createTestStats({
65+
uid: 'test-2',
66+
title: 'test B',
67+
fullTitle: 'Suite test B'
68+
})
69+
]
70+
71+
tests.forEach((test) => reporter.onTestStart(test))
72+
73+
expect((tests[0] as any).uid).not.toBe((tests[1] as any).uid)
74+
})
75+
76+
it('should handle suite stable UIDs for reruns', () => {
77+
const suite1 = createSuiteStats()
78+
const suite2 = createSuiteStats({ uid: 'suite-456' })
79+
80+
reporter.onSuiteStart(suite1)
81+
const uid1 = (suite1 as any).uid
82+
83+
reporter = new TestReporter(
84+
{ logFile: '/tmp/test.log' },
85+
sendUpstream as any
86+
)
87+
reporter.onSuiteStart(suite2)
88+
const uid2 = (suite2 as any).uid
89+
90+
expect(uid1).toBe(uid2)
91+
})
92+
})
93+
94+
describe('Cucumber feature file tracking for reruns', () => {
95+
it('should capture feature file and line for Cucumber tests', () => {
96+
const testStats = createTestStats({
97+
uid: 'test-1',
98+
title: 'Login scenario',
99+
fullTitle: 'Login scenario',
100+
file: '/test/login.feature',
101+
argument: { uri: '/test/features/login.feature', line: 15 } as any
102+
})
103+
104+
reporter.onTestStart(testStats)
105+
106+
expect((testStats as any).featureFile).toBe(
107+
'/test/features/login.feature'
108+
)
109+
expect((testStats as any).featureLine).toBe(15)
110+
})
111+
112+
it('should call reporter methods without errors', () => {
113+
const testStats = createTestStats({
114+
uid: 'test-1',
115+
title: 'test',
116+
fullTitle: 'test',
117+
file: '/test.spec.ts',
118+
parent: 'suite'
119+
})
120+
121+
expect(() => {
122+
reporter.onTestStart(testStats)
123+
reporter.onTestEnd(testStats)
124+
}).not.toThrow()
125+
})
126+
})
127+
})
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest'
2+
import { getBrowserObject, setCurrentSpecFile } from '../src/utils.js'
3+
4+
describe('service utils', () => {
5+
beforeEach(() => {
6+
vi.clearAllMocks()
7+
setCurrentSpecFile(undefined)
8+
})
9+
10+
describe('getBrowserObject', () => {
11+
it('should return browser directly or traverse hierarchy', () => {
12+
const mockBrowser = {
13+
sessionId: 'session-123',
14+
capabilities: {}
15+
} as WebdriverIO.Browser
16+
17+
// Direct browser object
18+
expect(getBrowserObject(mockBrowser)).toBe(mockBrowser)
19+
20+
// Single level
21+
const element1 = {
22+
elementId: 'element-1',
23+
parent: mockBrowser
24+
} as unknown as WebdriverIO.Element
25+
expect(getBrowserObject(element1)).toBe(mockBrowser)
26+
27+
// Multiple levels
28+
const element2 = {
29+
elementId: 'element-2',
30+
parent: element1
31+
} as unknown as WebdriverIO.Element
32+
expect(getBrowserObject(element2)).toBe(mockBrowser)
33+
})
34+
})
35+
36+
describe('setCurrentSpecFile', () => {
37+
it('should set and clear spec file without errors', () => {
38+
expect(() => setCurrentSpecFile('/path/to/test.spec.ts')).not.toThrow()
39+
expect(() => setCurrentSpecFile(undefined)).not.toThrow()
40+
})
41+
})
42+
})

0 commit comments

Comments
 (0)