Skip to content

Commit 0f0082c

Browse files
committed
implement summary counts
1 parent 05f464c commit 0f0082c

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

javascript/src/SummaryPrinter.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Envelope, TestRunFinished, TestRunStarted, TimeConversion } from '@cucumber/messages'
1+
import { Envelope, TestRunFinished, TestRunStarted, TestStepResultStatus } from '@cucumber/messages'
22
import { Query } from '@cucumber/query'
33

4+
import { formatCounts, formatDuration } from './helpers.js'
45
import type { Options } from './types.js'
5-
import { formatDuration } from './helpers.js'
66

77
export class SummaryPrinter {
88
private readonly println: (content?: string) => void
@@ -25,6 +25,48 @@ export class SummaryPrinter {
2525
}
2626

2727
private printSummary() {
28+
this.println()
29+
this.printScenarioCounts()
30+
this.printStepCounts()
31+
this.printDuration()
32+
}
33+
34+
private printScenarioCounts() {
35+
const scenarioCountsByStatus = this.query
36+
.findAllTestCaseFinished()
37+
.map((testCaseFinished) => this.query.findMostSevereTestStepResultBy(testCaseFinished))
38+
.map((testStepResult) => testStepResult?.status)
39+
.filter((status) => !!status)
40+
.reduce(
41+
(prev, status) => {
42+
return {
43+
...prev,
44+
[status]: (prev[status] ?? 0) + 1,
45+
}
46+
},
47+
{} as Partial<Record<TestStepResultStatus, number>>
48+
)
49+
this.println(formatCounts('scenarios', scenarioCountsByStatus, this.options.theme, this.stream))
50+
}
51+
52+
private printStepCounts() {
53+
const stepCountsByStatus = this.query
54+
.findAllTestCaseFinished()
55+
.flatMap((testCaseFinished) => this.query.findTestStepsFinishedBy(testCaseFinished))
56+
.map((testStepFinished) => testStepFinished.testStepResult.status)
57+
.reduce(
58+
(prev, status) => {
59+
return {
60+
...prev,
61+
[status]: (prev[status] ?? 0) + 1,
62+
}
63+
},
64+
{} as Partial<Record<TestStepResultStatus, number>>
65+
)
66+
this.println(formatCounts('steps', stepCountsByStatus, this.options.theme, this.stream))
67+
}
68+
69+
private printDuration() {
2870
const testRunStarted = this.query.findTestRunStarted() as TestRunStarted
2971
const testRunFinished = this.query.findTestRunFinished() as TestRunFinished
3072

javascript/src/helpers.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ import {
2020
TimeConversion,
2121
Timestamp,
2222
} from '@cucumber/messages'
23+
import { Interval } from 'luxon'
2324

2425
import { TextBuilder } from './TextBuilder.js'
2526
import { Theme } from './types.js'
26-
import { Interval } from 'luxon'
2727

2828
export const GHERKIN_INDENT_LENGTH = 2
2929
export const STEP_ARGUMENT_INDENT_LENGTH = 2
3030
export const ATTACHMENT_INDENT_LENGTH = 4
3131
export const ERROR_INDENT_LENGTH = 4
3232
const DURATION_FORMAT = "m'm' s.S's'"
33+
const STATUS_ORDER: TestStepResultStatus[] = [
34+
TestStepResultStatus.UNKNOWN,
35+
TestStepResultStatus.PASSED,
36+
TestStepResultStatus.SKIPPED,
37+
TestStepResultStatus.PENDING,
38+
TestStepResultStatus.UNDEFINED,
39+
TestStepResultStatus.AMBIGUOUS,
40+
TestStepResultStatus.FAILED,
41+
]
3342
const STATUS_CHARACTERS: Record<TestStepResultStatus, string> = {
3443
[TestStepResultStatus.AMBIGUOUS]: 'A',
3544
[TestStepResultStatus.FAILED]: 'F',
@@ -340,3 +349,30 @@ export function formatDuration(start: Timestamp, finish: Timestamp) {
340349
])
341350
return duration.toFormat(DURATION_FORMAT)
342351
}
352+
353+
export function formatCounts(
354+
suffix: string,
355+
counts: Partial<Record<TestStepResultStatus, number>>,
356+
theme: Theme,
357+
stream: NodeJS.WritableStream
358+
) {
359+
const builder = new TextBuilder(stream)
360+
const total = Object.values(counts).reduce((prev, curr) => prev + curr, 0)
361+
builder.append(`${total} ${suffix}`)
362+
if (total > 0) {
363+
let first = true
364+
builder.append(' (')
365+
for (const status of STATUS_ORDER) {
366+
const count = counts[status]
367+
if (count) {
368+
builder.append(`${count} ${status.toLowerCase()}`, theme.status?.all?.[status])
369+
if (!first) {
370+
builder.append(', ')
371+
}
372+
first = false
373+
}
374+
}
375+
builder.append(')')
376+
}
377+
return builder.build()
378+
}

0 commit comments

Comments
 (0)