Refactoring: parsers share markdown report generation

This commit is contained in:
Michal Dorner 2021-01-10 17:56:35 +01:00
parent 64b8f12bbc
commit 4e2ae7493f
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
4 changed files with 219 additions and 189 deletions

72
src/report/get-report.ts Normal file
View file

@ -0,0 +1,72 @@
import {TestExecutionResult, TestRunResult, TestSuiteResult} from './test-results'
import {Align, Icon, link, table} from '../utils/markdown-utils'
import {slug} from '../utils/slugger'
export default function getReport(tr: TestRunResult): string {
const time = `${(tr.time / 1000).toFixed(3)}s`
const headingLine = `**${tr.tests}** tests were completed in **${time}** with **${tr.passed}** passed, **${tr.skipped}** skipped and **${tr.failed}** failed.`
const suitesSummary = tr.suites.map((s, i) => {
const icon = getResultIcon(s.result)
const tsTime = `${s.time}ms`
const tsName = s.name
const tsAddr = makeSuiteSlug(i, tsName).link
const tsNameLink = link(tsName, tsAddr)
return [icon, tsNameLink, s.tests, tsTime, s.passed, s.failed, s.skipped]
})
const summary = table(
['Result', 'Suite', 'Tests', 'Time', `Passed ${Icon.success}`, `Failed ${Icon.fail}`, `Skipped ${Icon.skip}`],
[Align.Center, Align.Left, Align.Right, Align.Right, Align.Right, Align.Right, Align.Right],
...suitesSummary
)
const suites = tr.suites.map((ts, i) => getSuiteSummary(ts, i)).join('\n')
const suitesSection = `# Test Suites\n\n${suites}`
return `${headingLine}\n${summary}\n${suitesSection}`
}
function getSuiteSummary(ts: TestSuiteResult, index: number): string {
const icon = getResultIcon(ts.result)
const content = ts.groups
.map(grp => {
const header = grp.name ? `### ${grp.name}\n\n` : ''
const tests = table(
['Result', 'Test', 'Time'],
[Align.Center, Align.Left, Align.Right],
...grp.tests.map(tc => {
const name = tc.name
const time = `${tc.time}ms`
const result = getResultIcon(tc.result)
return [result, name, time]
})
)
return `${header}${tests}\n`
})
.join('\n')
const tsName = ts.name
const tsSlug = makeSuiteSlug(index, tsName)
const tsNameLink = `<a id="${tsSlug.id}" href="${tsSlug.link}">${tsName}</a>`
return `## ${tsNameLink} ${icon}\n\n${content}`
}
function makeSuiteSlug(index: number, name: string): {id: string; link: string} {
// use "ts-$index-" as prefix to avoid slug conflicts after escaping the paths
return slug(`ts-${index}-${name}`)
}
function getResultIcon(result: TestExecutionResult): string {
switch (result) {
case 'success':
return Icon.success
case 'skipped':
return Icon.skip
case 'failed':
return Icon.fail
default:
return ''
}
}

View file

@ -0,0 +1,77 @@
export class TestRunResult {
constructor(readonly suites: TestSuiteResult[], private totalTime?: number) {}
get tests(): number {
return this.suites.reduce((sum, g) => sum + g.tests, 0)
}
get passed(): number {
return this.suites.reduce((sum, g) => sum + g.passed, 0)
}
get failed(): number {
return this.suites.reduce((sum, g) => sum + g.failed, 0)
}
get skipped(): number {
return this.suites.reduce((sum, g) => sum + g.skipped, 0)
}
get time(): number {
return this.totalTime ?? this.suites.reduce((sum, g) => sum + g.time, 0)
}
get result(): TestExecutionResult {
return this.suites.some(t => t.result === 'failed') ? 'failed' : 'success'
}
}
export class TestSuiteResult {
constructor(readonly name: string, readonly groups: TestGroupResult[], private totalTime?: number) {}
get tests(): number {
return this.groups.reduce((sum, g) => sum + g.tests.length, 0)
}
get passed(): number {
return this.groups.reduce((sum, g) => sum + g.passed, 0)
}
get failed(): number {
return this.groups.reduce((sum, g) => sum + g.failed, 0)
}
get skipped(): number {
return this.groups.reduce((sum, g) => sum + g.skipped, 0)
}
get time(): number {
return this.totalTime ?? this.groups.reduce((sum, g) => sum + g.time, 0)
}
get result(): TestExecutionResult {
return this.groups.some(t => t.result === 'failed') ? 'failed' : 'success'
}
}
export class TestGroupResult {
constructor(readonly name: string | undefined, readonly tests: TestCaseResult[]) {}
get passed(): number {
return this.tests.reduce((sum, t) => (t.result === 'success' ? sum + 1 : sum), 0)
}
get failed(): number {
return this.tests.reduce((sum, t) => (t.result === 'failed' ? sum + 1 : sum), 0)
}
get skipped(): number {
return this.tests.reduce((sum, t) => (t.result === 'skipped' ? sum + 1 : sum), 0)
}
get time(): number {
return this.tests.reduce((sum, t) => sum + t.time, 0)
}
get result(): TestExecutionResult {
return this.tests.some(t => t.result === 'failed') ? 'failed' : 'success'
}
}
export class TestCaseResult {
constructor(readonly name: string, readonly result: TestExecutionResult, readonly time: number) {}
}
export type TestExecutionResult = 'success' | 'skipped' | 'failed' | undefined