Skip to content

Commit

Permalink
Manually create anchors for headings
Browse files Browse the repository at this point in the history
  • Loading branch information
dorny committed Nov 18, 2020
1 parent 3e8db1a commit 95d3d1f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 156 deletions.
8 changes: 4 additions & 4 deletions __tests__/__snapshots__/jest-junit.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ exports[`jest-junit tests matches report snapshot 1`] = `
**6** tests were completed in **1.360s** with **1** passed, **1** skipped and **4** failed.
| Result | Suite | Tests | Time | Passed ✔️ | Failed ❌ | Skipped ✖️ |
| :---: | :--- | ---: | ---: | ---: | ---: | ---: |
| ❌ | [__tests__\\\\main.test.js](#testsmaintestjs-) | 4 | 0.486s | 1 | 3 | 0 |
| ❌ | [__tests__\\\\second.test.js](#testssecondtestjs-) | 2 | 0.082s | 0 | 1 | 1 |
| ❌ | [__tests__\\\\main.test.js](#ts-0-tests-main-test-js) | 4 | 0.486s | 1 | 3 | 0 |
| ❌ | [__tests__\\\\second.test.js](#ts-1-tests-second-test-js) | 2 | 0.082s | 0 | 1 | 1 |
## Test Suites
### __tests__\\\\main.test.js ❌
### <a id=\\"user-content-ts-0-tests-main-test-js\\" href=\\"#ts-0-tests-main-test-js\\">__tests__\\\\main.test.js</a>
#### Test 1
Expand All @@ -30,7 +30,7 @@ exports[`jest-junit tests matches report snapshot 1`] = `
| :---: | :--- | ---: | --- |
| | Exception in test | 0ms | <details><summary>Error: Some error</summary><pre> at Object.<anonymous> (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\__tests__\\\\main.test.js:21:11)<br> at Object.asyncJestTest (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmineAsyncInstall.js:106:37)<br> at C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:45:12<br> at new Promise (<anonymous>)<br> at mapper (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:28:19)<br> at C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:75:41<br> at processTicksAndRejections (internal/process/task_queues.js:97:5)</pre></details> |
### __tests__\\\\second.test.js ❌
### <a id=\\"user-content-ts-1-tests-second-test-js\\" href=\\"#ts-1-tests-second-test-js\\">__tests__\\\\second.test.js</a>
| Result | Test | Time | Details |
| :---: | :--- | ---: | --- |
Expand Down
133 changes: 44 additions & 89 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

33 changes: 0 additions & 33 deletions dist/licenses.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/github": "^4.0.0",
"github-slugger": "^1.3.0",
"xml2js": "^0.4.23"
},
"devDependencies": {
Expand Down
32 changes: 19 additions & 13 deletions src/parsers/jest-junit/jest-junit-parser.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
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 {slug} from '../../utils/slugger'
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)
summary: getSummary(success, junit)
}
}
}

function getSummary(success: boolean, junit: JunitReport, slugger: GithubSlugger): string {
function getSummary(success: boolean, junit: JunitReport): string {
const stats = junit.testsuites.$

const icon = success ? Icon.success : Icon.fail
Expand All @@ -37,16 +35,16 @@ function getSummary(success: boolean, junit: JunitReport, slugger: GithubSlugger
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 suitesSummary = junit.testsuites.testsuite.map((ts, i) => {
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 tsName = ts.$.name
const tsAddr = makeSuiteSlug(i, tsName).link
const tsNameLink = link(tsName, tsAddr)
return [result, tsNameLink, ts.$.tests, tm, pass, fail, skip]
})

const summary = table(
Expand All @@ -55,7 +53,7 @@ function getSummary(success: boolean, junit: JunitReport, slugger: GithubSlugger
...suitesSummary
)

const suites = junit.testsuites?.testsuite?.map(ts => getSuiteSummary(ts)).join('\n')
const suites = junit.testsuites?.testsuite?.map((ts, i) => getSuiteSummary(ts, i)).join('\n')
const suitesSection = `## Test Suites\n\n${suites}`

return `${heading}\n${headingLine}\n${summary}\n${suitesSection}`
Expand All @@ -65,7 +63,7 @@ function getSkippedCount(suites: TestSuites): number {
return suites.testsuite.reduce((sum, suite) => sum + suite.$.skipped, 0)
}

function getSuiteSummary(suite: TestSuite): string {
function getSuiteSummary(suite: TestSuite, index: number): string {
const success = !(suite.$?.failures > 0 || suite.$?.errors > 0)
const icon = success ? Icon.success : Icon.fail

Expand Down Expand Up @@ -98,7 +96,10 @@ function getSuiteSummary(suite: TestSuite): string {
})
.join('\n')

return `### ${suite.$.name} ${icon}\n\n${content}`
const tsName = suite.$.name
const tsSlug = makeSuiteSlug(index, tsName)
const tsNameLink = `<a id="${tsSlug.id}" href="${tsSlug.link}">${tsName}</a>`
return `### ${tsNameLink} ${icon}\n\n${content}`
}

function getTestCaseIcon(test: TestCase): string {
Expand All @@ -119,3 +120,8 @@ function getTestCaseDetails(test: TestCase): string {

return ''
}

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}`)
}
Loading

0 comments on commit 95d3d1f

Please sign in to comment.