mirror of
https://github.com/dorny/test-reporter.git
synced 2025-12-16 06:17:10 +01:00
Implements jest-junit report parsing
This commit is contained in:
parent
7bda3b9f6f
commit
bc706859ad
11 changed files with 381 additions and 34 deletions
121
src/parsers/jest-junit/jest-junit-parser.ts
Normal file
121
src/parsers/jest-junit/jest-junit-parser.ts
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
import {TestResult} from '../test-parser'
|
||||
import {parseStringPromise} from 'xml2js'
|
||||
import GithubSlugger from 'github-slugger'
|
||||
|
||||
import {JunitReport, TestCase, TestSuite, TestSuites} from './jest-junit-types'
|
||||
import {Align, Icon, link, table, exceptionCell} from '../../utils/markdown-utils'
|
||||
import {parseAttribute} from '../../utils/xml-utils'
|
||||
|
||||
export async function parseJestJunit(content: string): Promise<TestResult> {
|
||||
const junit = (await parseStringPromise(content, {
|
||||
attrValueProcessors: [parseAttribute]
|
||||
})) as JunitReport
|
||||
const testsuites = junit.testsuites
|
||||
|
||||
const slugger = new GithubSlugger()
|
||||
const success = !(testsuites.$?.failures > 0 || testsuites.$?.errors > 0)
|
||||
|
||||
return {
|
||||
success,
|
||||
output: {
|
||||
title: junit.testsuites.$.name,
|
||||
summary: getSummary(success, junit, slugger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getSummary(success: boolean, junit: JunitReport, slugger: GithubSlugger): string {
|
||||
const stats = junit.testsuites.$
|
||||
|
||||
const icon = success ? Icon.success : Icon.fail
|
||||
const time = `${stats.time.toFixed(3)}s`
|
||||
|
||||
const skipped = getSkippedCount(junit.testsuites)
|
||||
const failed = stats.errors + stats.failures
|
||||
const passed = stats.tests - failed - skipped
|
||||
|
||||
const heading = `# ${stats.name} ${icon}`
|
||||
const headingLine = `**${stats.tests}** tests were completed in **${time}** with **${passed}** passed, **${skipped}** skipped and **${failed}** failed.`
|
||||
|
||||
const suitesSummary = junit.testsuites.testsuite.map(ts => {
|
||||
const skip = ts.$.skipped
|
||||
const fail = ts.$.errors + ts.$.failures
|
||||
const pass = ts.$.tests - fail - skip
|
||||
const tm = `${ts.$.time.toFixed(3)}s`
|
||||
const result = success ? Icon.success : Icon.fail
|
||||
const slug = slugger.slug(`${ts.$.name} ${result}`).replace(/_/g, '')
|
||||
const tsAddr = `#${slug}`
|
||||
const name = link(ts.$.name, tsAddr)
|
||||
return [result, name, ts.$.tests, tm, pass, fail, skip]
|
||||
})
|
||||
|
||||
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 = junit.testsuites?.testsuite?.map(ts => getSuiteSummary(ts)).join('\n')
|
||||
const suitesSection = `## Test Suites\n\n${suites}`
|
||||
|
||||
return `${heading}\n${headingLine}\n${summary}\n${suitesSection}`
|
||||
}
|
||||
|
||||
function getSkippedCount(suites: TestSuites): number {
|
||||
return suites.testsuite.reduce((sum, suite) => sum + suite.$.skipped, 0)
|
||||
}
|
||||
|
||||
function getSuiteSummary(suite: TestSuite): string {
|
||||
const success = !(suite.$?.failures > 0 || suite.$?.errors > 0)
|
||||
const icon = success ? Icon.success : Icon.fail
|
||||
|
||||
const groups: {describe: string; tests: TestCase[]}[] = []
|
||||
for (const tc of suite.testcase) {
|
||||
let grp = groups.find(g => g.describe === tc.$.classname)
|
||||
if (grp === undefined) {
|
||||
grp = {describe: tc.$.classname, tests: []}
|
||||
groups.push(grp)
|
||||
}
|
||||
grp.tests.push(tc)
|
||||
}
|
||||
|
||||
const content = groups
|
||||
.map(grp => {
|
||||
const header = grp.describe !== '' ? `#### ${grp.describe}\n\n` : ''
|
||||
const tests = table(
|
||||
['Result', 'Test', 'Time', 'Details'],
|
||||
[Align.Center, Align.Left, Align.Right, Align.None],
|
||||
...grp.tests.map(tc => {
|
||||
const name = tc.$.name
|
||||
const time = `${Math.round(tc.$.time * 1000)}ms`
|
||||
const result = getTestCaseIcon(tc)
|
||||
const ex = getTestCaseDetails(tc)
|
||||
return [result, name, time, ex]
|
||||
})
|
||||
)
|
||||
|
||||
return `${header}${tests}\n`
|
||||
})
|
||||
.join('\n')
|
||||
|
||||
return `### ${suite.$.name} ${icon}\n\n${content}`
|
||||
}
|
||||
|
||||
function getTestCaseIcon(test: TestCase): string {
|
||||
if (test.failure) return Icon.fail
|
||||
if (test.skipped) return Icon.skip
|
||||
return Icon.success
|
||||
}
|
||||
|
||||
function getTestCaseDetails(test: TestCase): string {
|
||||
if (test.skipped !== undefined) {
|
||||
return 'Skipped'
|
||||
}
|
||||
|
||||
if (test.failure !== undefined) {
|
||||
const failure = test.failure.join('\n')
|
||||
return exceptionCell(failure)
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
38
src/parsers/jest-junit/jest-junit-types.ts
Normal file
38
src/parsers/jest-junit/jest-junit-types.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
export interface JunitReport {
|
||||
testsuites: TestSuites
|
||||
}
|
||||
|
||||
export interface TestSuites {
|
||||
$: {
|
||||
name: string
|
||||
tests: number
|
||||
failures: number // assertion failed
|
||||
errors: number // unhandled exception during test execution
|
||||
time: number
|
||||
}
|
||||
testsuite: TestSuite[]
|
||||
}
|
||||
|
||||
export interface TestSuite {
|
||||
$: {
|
||||
name: string
|
||||
tests: number
|
||||
errors: number
|
||||
failures: number
|
||||
skipped: number
|
||||
time: number
|
||||
timestamp?: Date
|
||||
}
|
||||
testcase: TestCase[]
|
||||
}
|
||||
|
||||
export interface TestCase {
|
||||
$: {
|
||||
classname: string
|
||||
file?: string
|
||||
name: string
|
||||
time: number
|
||||
}
|
||||
failure?: string[]
|
||||
skipped?: string[]
|
||||
}
|
||||
10
src/parsers/test-parser.ts
Normal file
10
src/parsers/test-parser.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import {Endpoints} from '@octokit/types'
|
||||
|
||||
type OutputParameters = Endpoints['POST /repos/:owner/:repo/check-runs']['parameters']['output']
|
||||
|
||||
export type ParseTestResult = (content: string) => Promise<TestResult>
|
||||
|
||||
export interface TestResult {
|
||||
success: boolean
|
||||
output: OutputParameters
|
||||
}
|
||||
52
src/utils/markdown-utils.ts
Normal file
52
src/utils/markdown-utils.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
export enum Align {
|
||||
Left = ':---',
|
||||
Center = ':---:',
|
||||
Right = '---:',
|
||||
None = '---'
|
||||
}
|
||||
|
||||
export const Icon = {
|
||||
skip: '✖️', // ':heavy_multiplication_x:'
|
||||
success: '✔️', // ':heavy_check_mark:'
|
||||
fail: '❌' // ':x:'
|
||||
}
|
||||
|
||||
export function details(summary: string, content: string): string {
|
||||
return `<details><summary>${summary}</summary>${content}</details>`
|
||||
}
|
||||
|
||||
export function link(title: string, address: string): string {
|
||||
return `[${title}](${address})`
|
||||
}
|
||||
|
||||
type ToString = string | number | boolean | Date
|
||||
export function table(headers: ToString[], align: ToString[], ...rows: ToString[][]): string {
|
||||
const headerRow = `| ${headers.join(' | ')} |`
|
||||
const alignRow = `| ${align.join(' | ')} |`
|
||||
const contentRows = rows.map(row => `| ${row.join(' | ')} |`).join('\n')
|
||||
return [headerRow, alignRow, contentRows].join('\n')
|
||||
}
|
||||
|
||||
export function exceptionCell(ex: string): string {
|
||||
const lines = ex.split(/\r?\n/)
|
||||
if (lines.length === 0) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const summary = tableEscape(lines.shift()?.trim() || '')
|
||||
const emptyLine = /^\s*$/
|
||||
const firstNonEmptyLine = lines.findIndex(l => !emptyLine.test(l))
|
||||
|
||||
if (firstNonEmptyLine === -1) {
|
||||
return summary
|
||||
}
|
||||
|
||||
const contentLines = firstNonEmptyLine > 0 ? lines.slice(firstNonEmptyLine) : lines
|
||||
|
||||
const content = '<pre>' + tableEscape(contentLines.join('<br>')) + '</pre>'
|
||||
return details(summary, content)
|
||||
}
|
||||
|
||||
export function tableEscape(content: string): string {
|
||||
return content.replace('|', '\\|')
|
||||
}
|
||||
12
src/utils/xml-utils.ts
Normal file
12
src/utils/xml-utils.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
export function parseAttribute(str: string | undefined): string | number | undefined {
|
||||
if (str === '' || str === undefined) {
|
||||
return str
|
||||
}
|
||||
|
||||
const num = parseFloat(str)
|
||||
if (isNaN(num)) {
|
||||
return str
|
||||
}
|
||||
|
||||
return num
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue