|
| 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 | +}) |
0 commit comments