Skip to content

Commit 6ee9fdf

Browse files
committed
refactor: add file stats
1 parent 252dec7 commit 6ee9fdf

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

packages/vitest/src/node/reporters/github-actions.ts

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,11 @@ function escapeProperty(s: string): string {
247247
.replace(/,/g, '%2C')
248248
}
249249

250+
type SummaryTestsStats = Record<'failed' | 'passed' | 'expectedFail' | 'skipped' | 'todo', number>
251+
250252
interface SummaryData {
251-
stats: Record<'failed' | 'passed' | 'expectedFail' | 'skipped' | 'todo', number>
253+
fileStats: Pick<SummaryTestsStats, 'failed' | 'passed'>
254+
testsStats: SummaryTestsStats
252255
flakyTests: Array<{
253256
path: {
254257
relative: string
@@ -268,7 +271,11 @@ interface SummaryData {
268271

269272
function collectSummaryData(testModules: ReadonlyArray<TestModule>): SummaryData {
270273
const summaryData: SummaryData = {
271-
stats: {
274+
fileStats: {
275+
failed: 0,
276+
passed: 0,
277+
},
278+
testsStats: {
272279
failed: 0,
273280
passed: 0,
274281
expectedFail: 0,
@@ -284,28 +291,39 @@ function collectSummaryData(testModules: ReadonlyArray<TestModule>): SummaryData
284291
tests: [],
285292
}
286293

294+
switch (module.task.result?.state) {
295+
case 'fail': {
296+
summaryData.fileStats.failed += 1
297+
break
298+
}
299+
case 'pass': {
300+
summaryData.fileStats.passed += 1
301+
break
302+
}
303+
}
304+
287305
for (const test of module.children.allTests()) {
288306
switch (test.task.mode) {
289307
case 'skip': {
290-
summaryData.stats.skipped += 1
308+
summaryData.testsStats.skipped += 1
291309
break
292310
}
293311
case 'todo': {
294-
summaryData.stats.todo += 1
312+
summaryData.testsStats.todo += 1
295313
break
296314
}
297315
default: {
298316
switch (test.task.result?.state) {
299317
case 'fail': {
300-
summaryData.stats.failed += 1
318+
summaryData.testsStats.failed += 1
301319
break
302320
}
303321
case 'pass': {
304322
if (test.task.fails) {
305-
summaryData.stats.expectedFail += 1
323+
summaryData.testsStats.expectedFail += 1
306324
}
307325
else {
308-
summaryData.stats.passed += 1
326+
summaryData.testsStats.passed += 1
309327
}
310328

311329
break
@@ -366,41 +384,55 @@ function mdLink(text: string, url: string | null): string {
366384
return url === null ? text : `[${text}](${url})`
367385
}
368386

369-
function renderStats(stats: SummaryData['stats']): string {
370-
const primaryInfoTotal = stats.failed + stats.passed + stats.expectedFail
371-
const secondaryInfoTotal = stats.skipped + stats.todo
387+
function renderStats({ fileStats, testsStats }: SummaryData): string {
388+
const SEPARATOR_SYMBOL = ' · '
389+
390+
const fileInfoTotal = fileStats.failed + fileStats.passed
391+
const primaryInfoTotal = testsStats.failed + testsStats.passed + testsStats.expectedFail
392+
const secondaryInfoTotal = testsStats.skipped + testsStats.todo
372393

394+
const fileInfo: string[] = []
373395
const primaryInfo: string[] = []
374396
const secondaryInfo: string[] = []
375397

376-
if (stats.failed > 0) {
377-
primaryInfo.push(`❌ **${stats.failed} ${noun(stats.failed, 'failure', 'failures')}**`)
398+
if (fileStats.failed > 0) {
399+
fileInfo.push(`❌ **${fileStats.failed} ${noun(fileStats.failed, 'failure', 'failures')}**`)
400+
}
401+
402+
if (fileStats.passed > 0) {
403+
fileInfo.push(`✅ **${fileStats.passed} ${noun(fileStats.passed, 'pass', 'passes')}**`)
404+
}
405+
406+
fileInfo.push(`${fileInfoTotal} total`)
407+
408+
if (testsStats.failed > 0) {
409+
primaryInfo.push(`❌ **${testsStats.failed} ${noun(testsStats.failed, 'failure', 'failures')}**`)
378410
}
379411

380-
if (stats.passed > 0) {
381-
primaryInfo.push(`✅ **${stats.passed} ${noun(stats.passed, 'pass', 'passes')}**`)
412+
if (testsStats.passed > 0) {
413+
primaryInfo.push(`✅ **${testsStats.passed} ${noun(testsStats.passed, 'pass', 'passes')}**`)
382414
}
383415

384-
if (stats.expectedFail > 0) {
385-
primaryInfo.push(`🔵 **${stats.expectedFail} expected ${noun(stats.expectedFail, 'failure', 'failures')}**`)
416+
if (testsStats.expectedFail > 0) {
417+
primaryInfo.push(`🔵 **${testsStats.expectedFail} expected ${noun(testsStats.expectedFail, 'failure', 'failures')}**`)
386418
}
387419

388420
primaryInfo.push(`${primaryInfoTotal} total`)
389421

390-
if (stats.skipped > 0) {
391-
secondaryInfo.push(`${stats.skipped} ${noun(stats.skipped, 'skip', 'skips')}`)
422+
if (testsStats.skipped > 0) {
423+
secondaryInfo.push(`${testsStats.skipped} ${noun(testsStats.skipped, 'skip', 'skips')}`)
392424
}
393425

394-
if (stats.todo > 0) {
395-
secondaryInfo.push(`${stats.todo} ${noun(stats.todo, 'todo', 'todos')}`)
426+
if (testsStats.todo > 0) {
427+
secondaryInfo.push(`${testsStats.todo} ${noun(testsStats.todo, 'todo', 'todos')}`)
396428
}
397429

398-
let output = `\n### Summary\n\n- **Test Results**: ${primaryInfo.join(' · ')}\n`
430+
let output = `\n### Summary\n\n- **Test Files**: ${fileInfo.join(SEPARATOR_SYMBOL)}\n- **Test Results**: ${primaryInfo.join(SEPARATOR_SYMBOL)}\n`
399431

400432
if (secondaryInfo.length > 0) {
401433
secondaryInfo.push(`${secondaryInfoTotal} total`)
402434

403-
output += `- **Other**: ${secondaryInfo.join(' · ')}\n`
435+
output += `- **Other**: ${secondaryInfo.join(SEPARATOR_SYMBOL)}\n`
404436
}
405437

406438
return output
@@ -411,7 +443,7 @@ const SUMMARY_HEADER = '## Vitest Test Report\n'
411443
function renderSummary(summaryData: SummaryData, fileLinks?: JobSummaryOptions['fileLinks']): string {
412444
const fileLinkCreator = createGitHubFileLinkCreator(fileLinks)
413445

414-
let summary = `${SUMMARY_HEADER}${renderStats(summaryData.stats)}`
446+
let summary = `${SUMMARY_HEADER}${renderStats(summaryData)}`
415447

416448
if (summaryData.flakyTests.length > 0) {
417449
summary += '\n### Flaky Tests\n\nThese tests passed only after one or more retries, indicating potential instability.\n'

0 commit comments

Comments
 (0)