Skip to content

Commit 3e7ff38

Browse files
committed
Introduce ability to pass previous coverage value to get relative info
1 parent 599a305 commit 3e7ff38

File tree

9 files changed

+138
-27
lines changed

9 files changed

+138
-27
lines changed

__tests__/multi-files.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,21 @@ describe('multi report', () => {
6969
"
7070
`)
7171
})
72+
73+
test('should generate markdown with coverage change', () => {
74+
const result = getMultipleReport({
75+
multipleFiles: [
76+
`title1, ${__dirname}/../data/coverage_1/coverage-summary.json, 60`,
77+
`title2, ${__dirname}/../data/coverage_1/coverage-summary_2.json, 90`,
78+
],
79+
} as never)
80+
81+
expect(result).toMatchInlineSnapshot(`
82+
"| Title | Lines | Statements | Branches | Functions |
83+
| --- | --- | --- | --- | --- |
84+
| title1 | <a href="https://github.com/undefined/blob/undefined/README.md"><img alt="undefined: 78%" src="https://img.shields.io/badge/undefined-78%25-yellow.svg" /></a><br/>▲ Increased (+18%) | 76.74% (33/43) | 100% (0/0) | 33.33% (2/6) |
85+
| title2 | <a href="https://github.com/undefined/blob/undefined/README.md"><img alt="undefined: 79%" src="https://img.shields.io/badge/undefined-79%25-yellow.svg" /></a><br/>▼ Decreased (-11%) | 77.27% (34/44) | 100% (0/0) | 33.33% (2/6) |
86+
"
87+
`)
88+
})
7289
})

__tests__/summary.test.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('summary to markdown', () => {
129129
}
130130

131131
test('should convert summary to markdown with title', () => {
132-
const parsedSummary = summaryToMarkdown(summary, options, false)
132+
const parsedSummary = summaryToMarkdown(summary, options)
133133
expect(parsedSummary).toMatchInlineSnapshot(`
134134
"| Lines | Statements | Branches | Functions |
135135
| --- | --- | --- | --- |
@@ -139,9 +139,47 @@ describe('summary to markdown', () => {
139139
})
140140

141141
test('should convert summary to markdown without title', () => {
142-
const parsedSummary = summaryToMarkdown(summary, options, true)
142+
const parsedSummary = summaryToMarkdown(summary, options, {
143+
withoutHeader: true,
144+
})
143145
expect(parsedSummary).toMatchInlineSnapshot(
144146
`"| <a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/README.md"><img alt="Coverage: 78%" src="https://img.shields.io/badge/Coverage-78%25-yellow.svg" /></a><br/> | 76.74% (33/43) | 100% (0/0) | 33.33% (2/6) |"`
145147
)
146148
})
149+
150+
test('should convert summary to markdown with positive coverage change', () => {
151+
const parsedSummary = summaryToMarkdown(summary, options, {
152+
previousCoverage: '70',
153+
})
154+
expect(parsedSummary).toMatchInlineSnapshot(`
155+
"| Lines | Statements | Branches | Functions |
156+
| --- | --- | --- | --- |
157+
| <a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/README.md"><img alt="Coverage: 78%" src="https://img.shields.io/badge/Coverage-78%25-yellow.svg" /></a><br/>▲ Increased (+8%) | 76.74% (33/43) | 100% (0/0) | 33.33% (2/6) |
158+
"
159+
`)
160+
})
161+
162+
test('should convert summary to markdown with negative coverage change', () => {
163+
const parsedSummary = summaryToMarkdown(summary, options, {
164+
previousCoverage: '90',
165+
})
166+
expect(parsedSummary).toMatchInlineSnapshot(`
167+
"| Lines | Statements | Branches | Functions |
168+
| --- | --- | --- | --- |
169+
| <a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/README.md"><img alt="Coverage: 78%" src="https://img.shields.io/badge/Coverage-78%25-yellow.svg" /></a><br/>▼ Decreased (-12%) | 76.74% (33/43) | 100% (0/0) | 33.33% (2/6) |
170+
"
171+
`)
172+
})
173+
174+
test('should convert summary to markdown without coverage change on wrong value', () => {
175+
const parsedSummary = summaryToMarkdown(summary, options, {
176+
previousCoverage: 'test',
177+
})
178+
expect(parsedSummary).toMatchInlineSnapshot(`
179+
"| Lines | Statements | Branches | Functions |
180+
| --- | --- | --- | --- |
181+
| <a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/README.md"><img alt="Coverage: 78%" src="https://img.shields.io/badge/Coverage-78%25-yellow.svg" /></a><br/> | 76.74% (33/43) | 100% (0/0) | 33.33% (2/6) |
182+
"
183+
`)
184+
})
147185
})

dist/index.js

Lines changed: 38 additions & 12 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/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ async function main(): Promise<void> {
155155
}
156156

157157
if (options.coverageFile) {
158-
const coverageReport = getCoverageReport(options)
159158
const {
160159
coverageHtml,
161160
coverage: reportCoverage,
@@ -164,7 +163,7 @@ async function main(): Promise<void> {
164163
functions,
165164
lines,
166165
statements,
167-
} = coverageReport
166+
} = getCoverageReport(options)
168167
finalHtml += coverageHtml ? `\n\n${coverageHtml}` : ''
169168

170169
if (lines || coverageHtml) {
@@ -188,11 +187,11 @@ async function main(): Promise<void> {
188187
}
189188
}
190189

191-
if (multipleFiles?.length) {
190+
if (multipleFiles.length) {
192191
finalHtml += `\n\n${getMultipleReport(options)}`
193192
}
194193

195-
if (multipleJunitFiles?.length) {
194+
if (multipleJunitFiles.length) {
196195
const markdown = await getMultipleJunitReport(options)
197196
finalHtml += markdown ? `\n\n${markdown}` : ''
198197
}

src/multi-files.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ export function getMultipleReport(options: Options): string | null {
2626
'| --- | --- | --- | --- | --- |\n'
2727

2828
for (const titleFileLine of lineReports) {
29-
const { title, file } = titleFileLine
29+
const { title, file, previousCoverage } = titleFileLine
3030
const jsonContent = getContentFile(file)
3131
const summary = parseSummary(jsonContent)
3232

3333
if (summary) {
3434
const { color, coverage } = getCoverage(summary)
35-
const contentMd = summaryToMarkdown(summary, options, true)
35+
const contentMd = summaryToMarkdown(summary, options, {
36+
withoutHeader: true,
37+
previousCoverage,
38+
})
3639
table += `| ${title} ${contentMd}\n`
3740

3841
atLeastOneFileExists = true

src/summary.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,42 @@ function lineSummaryToTd(line: LineSummary): string {
3737
export function summaryToMarkdown(
3838
summary: Summary,
3939
options: Options,
40-
withoutHeader = false
40+
props: { withoutHeader?: boolean; previousCoverage?: string } = {}
4141
): string {
4242
const { repository, commit, badgeTitle } = options
4343
const { statements, functions, branches } = summary
4444
const { color, coverage } = getCoverage(summary)
4545
const readmeHref = `https://github.com/${repository}/blob/${commit}/README.md`
4646
const badge = `<a href="${readmeHref}"><img alt="${badgeTitle}: ${coverage}%" src="https://img.shields.io/badge/${badgeTitle}-${coverage}%25-${color}.svg" /></a><br/>`
4747

48+
let coverageChange = ''
49+
if (props.previousCoverage) {
50+
const previousCoverage = parseInt(props.previousCoverage)
51+
if (previousCoverage >= 0 && previousCoverage <= 100) {
52+
coverageChange =
53+
coverage === previousCoverage
54+
? '■ Unchanged'
55+
: coverage > previousCoverage
56+
? `▲ Increased (+${coverage - previousCoverage}%)`
57+
: `▼ Decreased (${coverage - previousCoverage}%)`
58+
} else {
59+
core.warning(
60+
"Previous coverage is ignored because the value doesn't lie between 0 and 100"
61+
)
62+
}
63+
}
64+
4865
const tableHeader =
4966
'| Lines | Statements | Branches | Functions |\n' +
5067
'| --- | --- | --- | --- |'
5168
const tableBody =
52-
`| ${badge} |` +
69+
`| ${badge}${coverageChange} |` +
5370
` ${lineSummaryToTd(statements)} |` +
5471
` ${lineSummaryToTd(branches)} |` +
5572
` ${lineSummaryToTd(functions)} |`
5673
const table = `${tableHeader}\n${tableBody}\n`
5774

58-
if (withoutHeader) {
75+
if (props.withoutHeader) {
5976
return tableBody
6077
}
6178

@@ -86,13 +103,19 @@ export function getCoverage(
86103
export function getSummaryReport(options: Options): SummaryReport {
87104
const { summaryFile } = options
88105

106+
const summaryFileArr = (summaryFile ?? '').split(',')
107+
const file = summaryFileArr[0].trim()
108+
const previousCoverage = summaryFileArr[1]
109+
89110
try {
90-
const jsonContent = getContentFile(summaryFile)
111+
const jsonContent = getContentFile(file)
91112
const summary = parseSummary(jsonContent)
92113

93114
if (summary) {
94115
const { color, coverage } = getCoverage(summary)
95-
const summaryHtml = summaryToMarkdown(summary, options)
116+
const summaryHtml = summaryToMarkdown(summary, options, {
117+
previousCoverage,
118+
})
96119

97120
return { color, coverage, summaryHtml }
98121
}

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,5 @@ export interface CoverageReport {
9797
export interface MultipleFilesLine {
9898
title: string
9999
file: string
100+
previousCoverage?: string
100101
}

src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export const parseLine = (line: string): MultipleFilesLine | null => {
7979
}
8080

8181
const lineArr = line.split(',')
82-
return { title: lineArr[0].trim(), file: lineArr[1].trim() }
82+
return {
83+
title: lineArr[0].trim(),
84+
file: lineArr[1].trim(),
85+
previousCoverage: lineArr[2],
86+
}
8387
}
8488

8589
/** Helper function to filter null entries out of an array. */

0 commit comments

Comments
 (0)