Skip to content

Commit 2abd471

Browse files
authored
Merge pull request #367 from timostroehlein/main
Improve comment generation and update docs
2 parents 871a33b + 1634504 commit 2abd471

File tree

8 files changed

+113
-83
lines changed

8 files changed

+113
-83
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,7 @@ Thumbs.db
9898
__tests__/runner/*
9999
lib/**/*
100100

101-
!__tests__/**/coverage
101+
!__tests__/**/coverage
102+
103+
# Jetbrains IDE files
104+
.idea

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ The report is based on the coverage report generated by your test runner.
8787
8888
```yaml
8989
- name: Comment Code Coverage on PR
90-
uses: dkhunt27/nx-code-coverage@v2
90+
uses: dkhunt27/action-nx-code-coverage@v2
9191
with:
9292
github-token: ${{ secrets.GITHUB_TOKEN }}
9393
no-coverage-ran: false
94-
coverage-folder: ./coverage
95-
coverage-base-folder: ./coverage-base
94+
coverage-folder: coverage
95+
coverage-base-folder: coverage-base
9696
gist-processing: true
9797
gist-token: ${{ secrets.COVERAGE_BADGES_GIST_TOKEN }}
9898
gist-id: d7e6b443a01eba615fd2a7c1215aec91

__tests__/__snapshots__/comment.test.ts.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when all results in one then return as expected 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app diff -</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr></tbody></table> <br/><table><tbody><tr><th>app diff +</th><th>50.00%</th><th>✅ ▴ +25.00%</th></tr></tbody></table> <br/><table><tbody><tr><th>app diff null</th><th>unknown%</th></tr></tbody></table> <br/>"`;
3+
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when all results in one then return as expected 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app diff -</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr><tr><th>app diff +</th><th>50.00%</th><th>✅ ▴ +25.00%</th></tr><tr><th>app diff null</th><th>unknown%</th></tr></tbody></table>"`;
44

5-
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when result coverage is undefined then unknown diff in code coverage comment 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app A</th><th>unknown%</th></tr></tbody></table> <br/>"`;
5+
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when result coverage is undefined then unknown diff in code coverage comment 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app A</th><th>unknown%</th></tr></tbody></table>"`;
66

7-
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when result diff is 0 then no code coverage comment 1`] = `"Code Coverage:<p></p>"`;
7+
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when result diff is 0 then no code coverage comment 1`] = `"<h2>Code Coverage</h2><table><tbody></tbody></table>"`;
88

9-
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when result diff is negative then negative diff in code coverage comment 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app A</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr></tbody></table> <br/>"`;
9+
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when result diff is negative then negative diff in code coverage comment 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app A</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr></tbody></table>"`;
1010

11-
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when result diff is positive then positive diff in code coverage comment 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app A</th><th>50.00%</th><th>✅ ▴ +25.00%</th></tr></tbody></table> <br/>"`;
11+
exports[`comment tests buildComment when hiding coverage reports when hiding unchanged when result diff is positive then positive diff in code coverage comment 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app A</th><th>50.00%</th><th>✅ ▴ +25.00%</th></tr></tbody></table>"`;
1212

13-
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when all results in one then return as expected 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app diff 0</th><th>50.00%</th><th>✅ 0.00%</th></tr></tbody></table> <br/><table><tbody><tr><th>app diff -</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr></tbody></table> <br/><table><tbody><tr><th>app diff +</th><th>50.00%</th><th>✅ ▴ +25.00%</th></tr></tbody></table> <br/><table><tbody><tr><th>app diff null</th><th>unknown%</th></tr></tbody></table> <br/>"`;
13+
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when all results in one then return as expected 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app diff 0</th><th>50.00%</th><th>✅ 0.00%</th></tr><tr><th>app diff -</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr><tr><th>app diff +</th><th>50.00%</th><th>✅ ▴ +25.00%</th></tr><tr><th>app diff null</th><th>unknown%</th></tr></tbody></table>"`;
1414

15-
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when result coverage is undefined then unknown diff in code coverage comment 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app A</th><th>unknown%</th></tr></tbody></table> <br/>"`;
15+
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when result coverage is undefined then unknown diff in code coverage comment 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app A</th><th>unknown%</th></tr></tbody></table>"`;
1616

17-
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when result diff is 0 then 0 diff in code coverage comment 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app A</th><th>50.00%</th><th>✅ 0.00%</th></tr></tbody></table> <br/>"`;
17+
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when result diff is 0 then 0 diff in code coverage comment 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app A</th><th>50.00%</th><th>✅ 0.00%</th></tr></tbody></table>"`;
1818

19-
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when result diff is negative then negative diff in code coverage comment 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app A</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr></tbody></table> <br/>"`;
19+
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when result diff is negative then negative diff in code coverage comment 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app A</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr></tbody></table>"`;
2020

21-
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when result diff is positive then positive diff in code coverage comment 1`] = `"Code Coverage:<p></p><table><tbody><tr><th>app A</th><th>50.00%</th><th>✅ ▴ +25.00%</th></tr></tbody></table> <br/>"`;
21+
exports[`comment tests buildComment when hiding coverage reports when not hiding unchanged when result diff is positive then positive diff in code coverage comment 1`] = `"<h2>Code Coverage</h2><table><tbody><tr><th>app A</th><th>50.00%</th><th>✅ ▴ +25.00%</th></tr></tbody></table>"`;
2222

2323
exports[`comment tests buildComment when not hiding coverage reports when all results in one then return as expected 1`] = `
24-
"Code Coverage:<p></p><table><tbody><tr><th>app diff 0</th><th>50.00%</th><th>✅ 0.00%</th></tr></tbody></table>
24+
"<h2>Code Coverage</h2><table><tbody><tr><th>app diff 0</th><th>50.00%</th><th>✅ 0.00%</th></tr></tbody></table>
2525
2626
<details><summary>Coverage Report</summary><table><tbody><tr><th>File </th><th> % Stmts </th><th> % Branch </th><th> % Funcs </th><th> % Lines </th><th> Uncovered Line #s </th></tr><tr><td>All&nbsp;files </td><td>&nbsp; 96.42 </td><td>&nbsp; 60 </td><td>&nbsp; 96.66 </td><td>&nbsp; 96.59 </td><td>&nbsp; </td></tr></tbody></table></details> <br/><table><tbody><tr><th>app diff -</th><th>50.00%</th><th>❌ ▾ -25.00%</th></tr></tbody></table>
2727

__tests__/comment.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('comment tests', () => {
1919
hideUnchanged
2020
}
2121
const actual = buildComment(input)
22-
expect(actual).toStrictEqual('Code Coverage:<p></p>')
22+
expect(actual).toStrictEqual('<h2>Code Coverage</h2>')
2323
})
2424
test('when result base/diff null then no diff in code coverage comment', async () => {
2525
const input: BuildCommentInputs = {
@@ -37,7 +37,7 @@ describe('comment tests', () => {
3737
}
3838
const actual = buildComment(input)
3939
expect(actual).toContain(
40-
'Code Coverage:<p></p><table><tbody><tr><th>app A</th><th>50.00%</th></tr></tbody></table>'
40+
'<h2>Code Coverage</h2><table><tbody><tr><th>app A</th><th>50.00%</th></tr></tbody></table>'
4141
)
4242
})
4343

dist/index.js

Lines changed: 24 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/comment.ts

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,63 +13,76 @@ export const buildComment = ({
1313
hideCoverageReports,
1414
hideUnchanged
1515
}: BuildCommentInputs): string => {
16-
const html = results.map(result => {
17-
let plus = ''
18-
let arrow = ''
19-
let diffHtml = ''
16+
const rows = results
17+
.map(result => {
18+
let plus = ''
19+
let arrow = ''
20+
let diffHtml = ''
2021

21-
// when no tests, not sure if output is undefined or 'Unknown'; TODO: add test case
22-
if (
23-
result.diff !== undefined &&
24-
result.diff !== null &&
25-
(result.diff as unknown) !== 'Unknown'
26-
) {
27-
if (result.diff < 0) {
28-
arrow = '▾'
29-
} else if (result.diff > 0) {
30-
plus = '+'
31-
arrow = '▴'
32-
}
22+
// when no tests, not sure if output is undefined or 'Unknown'; TODO: add test case
23+
if (
24+
result.diff !== undefined &&
25+
result.diff !== null &&
26+
(result.diff as unknown) !== 'Unknown'
27+
) {
28+
if (result.diff < 0) {
29+
arrow = '▾'
30+
} else if (result.diff > 0) {
31+
plus = '+'
32+
arrow = '▴'
33+
}
3334

34-
diffHtml = th(
35-
renderEmoji(result.diff),
36-
' ',
37-
arrow,
38-
' ',
39-
plus,
40-
result.diff.toFixed(2),
41-
'%'
42-
)
43-
}
35+
diffHtml = th(
36+
renderEmoji(result.diff),
37+
' ',
38+
arrow,
39+
' ',
40+
plus,
41+
result.diff.toFixed(2),
42+
'%'
43+
)
44+
}
4445

45-
// when no tests, not sure if is undefined or 'Unknown'; TODO: add test case
46-
let coverage
47-
if (
48-
result.coverage === undefined ||
49-
result.coverage === null ||
50-
(result.coverage as unknown) === 'Unknown'
51-
) {
52-
coverage = 'unknown'
53-
} else {
54-
coverage = result.coverage.toFixed(2)
55-
}
46+
// when no tests, not sure if is undefined or 'Unknown'; TODO: add test case
47+
let coverage
48+
if (
49+
result.coverage === undefined ||
50+
result.coverage === null ||
51+
(result.coverage as unknown) === 'Unknown'
52+
) {
53+
coverage = 'unknown'
54+
} else {
55+
coverage = result.coverage.toFixed(2)
56+
}
5657

57-
if (result.diff === 0 && hideUnchanged) {
58-
return ''
59-
} else {
60-
if (hideCoverageReports) {
61-
return `${table(
62-
tbody(tr(th(result.app), th(coverage, '%'), diffHtml))
63-
)} <br/>`
58+
if (result.diff === 0 && hideUnchanged) {
59+
return {
60+
row: '',
61+
htmlResults: ''
62+
}
6463
} else {
6564
const htmlResults = tabulate(result.details)
66-
return `${table(
67-
tbody(tr(th(result.app), th(coverage, '%'), diffHtml))
68-
)} \n\n ${details(summary('Coverage Report'), htmlResults)} <br/>`
65+
return {
66+
row: tr(
67+
th(result.app.replace(/^\/+/g, '')),
68+
th(coverage, '%'),
69+
diffHtml
70+
),
71+
htmlResults
72+
}
6973
}
70-
}
71-
})
74+
})
75+
.filter(row => row.row !== '')
76+
77+
const tableHtml = hideCoverageReports
78+
? table(tbody(rows.map(row => row.row).join('')))
79+
: rows
80+
.map(
81+
row =>
82+
`${table(tbody(row.row))} \n\n ${details(summary('Coverage Report'), row.htmlResults)} <br/>`
83+
)
84+
.join('')
7285

73-
const title = `Code Coverage:<p></p>`
74-
return fragment(title, html.join(''))
86+
const title = `<h2>Code Coverage</h2>`
87+
return fragment(title, tableHtml)
7588
}

src/json-coverage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ export const mergeFileLists = ({
7676
const found = baseSummaryFileList.find(item => item.app === summary.app)
7777
if (found) {
7878
base = buildMergeItem(found)
79-
baseCoveragePct = base.parsedTotal.statements.pct
79+
baseCoveragePct =
80+
base.parsedTotal.statements.pct.toString() === 'Unknown'
81+
? 0
82+
: base.parsedTotal.statements.pct
8083

8184
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON
8285
// use Number.EPSILON so rounding of 0.0005 is correct

0 commit comments

Comments
 (0)