diff --git a/src/__snapshots__/resultFormatter.test.ts.snap b/src/__snapshots__/resultFormatter.test.ts.snap index 775024c..51500fd 100644 --- a/src/__snapshots__/resultFormatter.test.ts.snap +++ b/src/__snapshots__/resultFormatter.test.ts.snap @@ -1,12 +1,45 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`resultFormatter should default to coverageThreshold for newFileCoverageThreshold 1`] = ` +"| Ok (reason) | File | Lines | Branches | Functions | Statements | +| ----------------------- | ----------- | ------------- | ------------- | ------------ | ------------- | +| 🔴
(Decreased >2%) | file1 | 80%
(+10%) | 14%
(-30%) | 3%
(+20%) | 20%
(-10%) | +| ✅ | file2 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| 🔴
(Below 1%) | file3 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| ✅ | (New) file4 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| 🔴
(Below 1%) | (New) file5 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | + +Total: + +| Lines | Branches | Functions | Statements | +| ----------- | ----------- | ----------- | ----------- | +| 100%(+100%) | 100%(+100%) | 100%(+100%) | 100%(+100%) |" +`; + exports[`resultFormatter should format files results as markdown table 1`] = ` -"| Ok | File (✨=New File) | Lines | Branches | Functions | Statements | -| -- | ----------------- | ------------- | ------------- | ------------ | ------------- | -| 🔴 | file1 | 80%
(+10%) | 14%
(-30%) | 3%
(+20%) | 20%
(-10%) | -| ✅ | file2 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | -| 🔴 | file3 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | -| ✅ | ✨ file4 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +"| Ok (reason) | File | Lines | Branches | Functions | Statements | +| ----------------------- | ----------- | ------------- | ------------- | ------------ | ------------- | +| 🔴
(Decreased >2%) | file1 | 80%
(+10%) | 14%
(-30%) | 3%
(+20%) | 20%
(-10%) | +| ✅ | file2 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| 🔴
(Below 3%) | file3 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| ✅ | (New) file4 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| 🔴
(Below 1%) | (New) file5 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | + +Total: + +| Lines | Branches | Functions | Statements | +| ----------- | ----------- | ----------- | ----------- | +| 100%(+100%) | 100%(+100%) | 100%(+100%) | 100%(+100%) |" +`; + +exports[`resultFormatter should format files results as markdown table with defaults 1`] = ` +"| Ok (reason) | File | Lines | Branches | Functions | Statements | +| ----------------------- | ----------- | ------------- | ------------- | ------------ | ------------- | +| 🔴
(Decreased >0%) | file1 | 80%
(+10%) | 14%
(-30%) | 3%
(+20%) | 20%
(-10%) | +| ✅ | file2 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| 🔴
(Below 100%) | file3 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| ✅ | (New) file4 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| 🔴
(Below 100%) | (New) file5 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | Total: @@ -24,3 +57,19 @@ Total: | ----------- | ----------- | ----------- | ----------- | | 100%(+100%) | 100%(+100%) | 100%(+100%) | 100%(+100%) |" `; + +exports[`resultFormatter should use defaults 1`] = ` +"| Ok (reason) | File | Lines | Branches | Functions | Statements | +| ----------------------- | ----------- | ------------- | ------------- | ------------ | ------------- | +| 🔴
(Decreased >0%) | file1 | 80%
(+10%) | 14%
(-30%) | 3%
(+20%) | 20%
(-10%) | +| ✅ | file2 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| 🔴
(Below 100%) | file3 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| ✅ | (New) file4 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | +| 🔴
(Below 100%) | (New) file5 | 20%
(+10%) | 8%
(+30%) | 2%
(+20%) | 5%
(+10%) | + +Total: + +| Lines | Branches | Functions | Statements | +| ----------- | ----------- | ----------- | ----------- | +| 100%(+100%) | 100%(+100%) | 100%(+100%) | 100%(+100%) |" +`; diff --git a/src/resultFormatter.test.ts b/src/resultFormatter.test.ts index 8473441..0d81668 100644 --- a/src/resultFormatter.test.ts +++ b/src/resultFormatter.test.ts @@ -69,6 +69,23 @@ const filesResults: FilesResults = { }, decreased: false, belowThreshold: false + }, + file5: { + isNewFile: true, + deltas: { + lines: 10, + statements: 10, + functions: 20, + branches: 30 + }, + pcts: { + lines: 20, + statements: 5, + functions: 2, + branches: 8 + }, + decreased: false, + belowThreshold: true } }; @@ -92,6 +109,20 @@ const totalResults: FileResultFormat = { describe('resultFormatter', () => { it('should format files results as markdown table', () => { + expect( + resultFormatter(filesResults, totalResults, 1, 2, 3) + ).toMatchSnapshot(); + }); + + it('should use defaults', () => { + expect(resultFormatter(filesResults, totalResults)).toMatchSnapshot(); + }); + + it('should default to coverageThreshold for newFileCoverageThreshold', () => { + expect(resultFormatter(filesResults, totalResults, 1, 2)).toMatchSnapshot(); + }); + + it('should format files results as markdown table with defaults', () => { expect(resultFormatter(filesResults, totalResults)).toMatchSnapshot(); }); diff --git a/src/resultFormatter.ts b/src/resultFormatter.ts index a95b416..53526f8 100644 --- a/src/resultFormatter.ts +++ b/src/resultFormatter.ts @@ -1,11 +1,20 @@ import markdownTable from 'markdown-table'; import { FilesResults, FileResultFormat } from './common'; +import { defaultOptions } from './index'; export const resultFormatter = ( files: FilesResults, - total: FileResultFormat + total: FileResultFormat, + coverageThreshold = defaultOptions.coverageThreshold!, + coverageDecreaseThreshold = defaultOptions.coverageDecreaseThreshold!, + newFileCoverageThreshold = coverageThreshold ): string => { - const formattedFiles = formatFilesResults(files); + const formattedFiles = formatFilesResults( + files, + coverageThreshold, + coverageDecreaseThreshold, + newFileCoverageThreshold + ); const formattedTotal = formatTotal(total); return `${formattedFiles}${formattedTotal}`; }; @@ -30,12 +39,17 @@ const formatTotal = (total: FileResultFormat): string => { return '\n\nTotal:\n\n' + markdownTable(table); }; -const formatFilesResults = (files: FilesResults): string => { +const formatFilesResults = ( + files: FilesResults, + coverageThreshold = defaultOptions.coverageThreshold!, + coverageDecreaseThreshold = defaultOptions.coverageDecreaseThreshold!, + newFileCoverageThreshold = coverageThreshold +): string => { let noChange = true; const table: Array<(string | number)[]> = []; const header = [ - 'Ok', - 'File (✨=New File)', + 'Ok (reason)', + 'File', 'Lines', 'Branches', 'Functions', @@ -45,9 +59,19 @@ const formatFilesResults = (files: FilesResults): string => { Object.keys(files).forEach((file) => { const { deltas, pcts, decreased, belowThreshold, isNewFile } = files[file]; + const reasons = []; + if (belowThreshold) { + reasons.push( + `Below ${isNewFile ? coverageThreshold : newFileCoverageThreshold}%` + ); + } + if (decreased) { + reasons.push(`Decreased >${coverageDecreaseThreshold}%`); + } + const row = [ - decreased || belowThreshold ? '🔴' : '✅', - `${isNewFile ? '✨ ' : ''}${file}`, + decreased || belowThreshold ? `🔴
(${reasons.join(' & ')})` : '✅', + `${isNewFile ? '(New) ' : ''}${file}`, `${pcts.lines}%
(${formatDelta(deltas.lines)})`, `${pcts.branches}%
(${formatDelta(deltas.branches)})`, `${pcts.functions}%
(${formatDelta(deltas.functions)})`,