Skip to content

Commit

Permalink
Introduce ability to pass previous coverage value to get relative info
Browse files Browse the repository at this point in the history
  • Loading branch information
paescuj committed Oct 28, 2022
1 parent 599a305 commit 3e7ff38
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 27 deletions.
17 changes: 17 additions & 0 deletions __tests__/multi-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,21 @@ describe('multi report', () => {
"
`)
})

test('should generate markdown with coverage change', () => {
const result = getMultipleReport({
multipleFiles: [
`title1, ${__dirname}/../data/coverage_1/coverage-summary.json, 60`,
`title2, ${__dirname}/../data/coverage_1/coverage-summary_2.json, 90`,
],
} as never)

expect(result).toMatchInlineSnapshot(`
"| Title | Lines | Statements | Branches | Functions |
| --- | --- | --- | --- | --- |
| 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) |
| 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) |
"
`)
})
})
42 changes: 40 additions & 2 deletions __tests__/summary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('summary to markdown', () => {
}

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

test('should convert summary to markdown without title', () => {
const parsedSummary = summaryToMarkdown(summary, options, true)
const parsedSummary = summaryToMarkdown(summary, options, {
withoutHeader: true,
})
expect(parsedSummary).toMatchInlineSnapshot(
`"| <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) |"`
)
})

test('should convert summary to markdown with positive coverage change', () => {
const parsedSummary = summaryToMarkdown(summary, options, {
previousCoverage: '70',
})
expect(parsedSummary).toMatchInlineSnapshot(`
"| Lines | Statements | Branches | Functions |
| --- | --- | --- | --- |
| <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) |
"
`)
})

test('should convert summary to markdown with negative coverage change', () => {
const parsedSummary = summaryToMarkdown(summary, options, {
previousCoverage: '90',
})
expect(parsedSummary).toMatchInlineSnapshot(`
"| Lines | Statements | Branches | Functions |
| --- | --- | --- | --- |
| <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) |
"
`)
})

test('should convert summary to markdown without coverage change on wrong value', () => {
const parsedSummary = summaryToMarkdown(summary, options, {
previousCoverage: 'test',
})
expect(parsedSummary).toMatchInlineSnapshot(`
"| Lines | Statements | Branches | Functions |
| --- | --- | --- | --- |
| <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) |
"
`)
})
})
50 changes: 38 additions & 12 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.

7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ async function main(): Promise<void> {
}

if (options.coverageFile) {
const coverageReport = getCoverageReport(options)
const {
coverageHtml,
coverage: reportCoverage,
Expand All @@ -164,7 +163,7 @@ async function main(): Promise<void> {
functions,
lines,
statements,
} = coverageReport
} = getCoverageReport(options)
finalHtml += coverageHtml ? `\n\n${coverageHtml}` : ''

if (lines || coverageHtml) {
Expand All @@ -188,11 +187,11 @@ async function main(): Promise<void> {
}
}

if (multipleFiles?.length) {
if (multipleFiles.length) {
finalHtml += `\n\n${getMultipleReport(options)}`
}

if (multipleJunitFiles?.length) {
if (multipleJunitFiles.length) {
const markdown = await getMultipleJunitReport(options)
finalHtml += markdown ? `\n\n${markdown}` : ''
}
Expand Down
7 changes: 5 additions & 2 deletions src/multi-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ export function getMultipleReport(options: Options): string | null {
'| --- | --- | --- | --- | --- |\n'

for (const titleFileLine of lineReports) {
const { title, file } = titleFileLine
const { title, file, previousCoverage } = titleFileLine
const jsonContent = getContentFile(file)
const summary = parseSummary(jsonContent)

if (summary) {
const { color, coverage } = getCoverage(summary)
const contentMd = summaryToMarkdown(summary, options, true)
const contentMd = summaryToMarkdown(summary, options, {
withoutHeader: true,
previousCoverage,
})
table += `| ${title} ${contentMd}\n`

atLeastOneFileExists = true
Expand Down
33 changes: 28 additions & 5 deletions src/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,42 @@ function lineSummaryToTd(line: LineSummary): string {
export function summaryToMarkdown(
summary: Summary,
options: Options,
withoutHeader = false
props: { withoutHeader?: boolean; previousCoverage?: string } = {}
): string {
const { repository, commit, badgeTitle } = options
const { statements, functions, branches } = summary
const { color, coverage } = getCoverage(summary)
const readmeHref = `https://github.com/${repository}/blob/${commit}/README.md`
const badge = `<a href="${readmeHref}"><img alt="${badgeTitle}: ${coverage}%" src="https://img.shields.io/badge/${badgeTitle}-${coverage}%25-${color}.svg" /></a><br/>`

let coverageChange = ''
if (props.previousCoverage) {
const previousCoverage = parseInt(props.previousCoverage)
if (previousCoverage >= 0 && previousCoverage <= 100) {
coverageChange =
coverage === previousCoverage
? '■ Unchanged'
: coverage > previousCoverage
? `▲ Increased (+${coverage - previousCoverage}%)`
: `▼ Decreased (${coverage - previousCoverage}%)`
} else {
core.warning(
"Previous coverage is ignored because the value doesn't lie between 0 and 100"
)
}
}

const tableHeader =
'| Lines | Statements | Branches | Functions |\n' +
'| --- | --- | --- | --- |'
const tableBody =
`| ${badge} |` +
`| ${badge}${coverageChange} |` +
` ${lineSummaryToTd(statements)} |` +
` ${lineSummaryToTd(branches)} |` +
` ${lineSummaryToTd(functions)} |`
const table = `${tableHeader}\n${tableBody}\n`

if (withoutHeader) {
if (props.withoutHeader) {
return tableBody
}

Expand Down Expand Up @@ -86,13 +103,19 @@ export function getCoverage(
export function getSummaryReport(options: Options): SummaryReport {
const { summaryFile } = options

const summaryFileArr = (summaryFile ?? '').split(',')
const file = summaryFileArr[0].trim()
const previousCoverage = summaryFileArr[1]

try {
const jsonContent = getContentFile(summaryFile)
const jsonContent = getContentFile(file)
const summary = parseSummary(jsonContent)

if (summary) {
const { color, coverage } = getCoverage(summary)
const summaryHtml = summaryToMarkdown(summary, options)
const summaryHtml = summaryToMarkdown(summary, options, {
previousCoverage,
})

return { color, coverage, summaryHtml }
}
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ export interface CoverageReport {
export interface MultipleFilesLine {
title: string
file: string
previousCoverage?: string
}
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ export const parseLine = (line: string): MultipleFilesLine | null => {
}

const lineArr = line.split(',')
return { title: lineArr[0].trim(), file: lineArr[1].trim() }
return {
title: lineArr[0].trim(),
file: lineArr[1].trim(),
previousCoverage: lineArr[2],
}
}

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

0 comments on commit 3e7ff38

Please sign in to comment.