Skip to content
61 changes: 55 additions & 6 deletions src/__snapshots__/resultFormatter.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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 |
| ----------------------- | ----------- | ------------- | ------------- | ------------ | ------------- |
| 🔴<br />(Decreased >2%) | file1 | 80%<br>(+10%) | 14%<br>(-30%) | 3%<br>(+20%) | 20%<br>(-10%) |
| ✅ | file2 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴<br />(Below 1%) | file3 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| ✅ | (New) file4 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴<br />(Below 1%) | (New) file5 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+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%<br>(+10%) | 14%<br>(-30%) | 3%<br>(+20%) | 20%<br>(-10%) |
| ✅ | file2 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴 | file3 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| ✅ | ✨ file4 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
"| Ok (reason) | File | Lines | Branches | Functions | Statements |
| ----------------------- | ----------- | ------------- | ------------- | ------------ | ------------- |
| 🔴<br />(Decreased >2%) | file1 | 80%<br>(+10%) | 14%<br>(-30%) | 3%<br>(+20%) | 20%<br>(-10%) |
| ✅ | file2 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴<br />(Below 3%) | file3 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| ✅ | (New) file4 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴<br />(Below 1%) | (New) file5 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+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 |
| ----------------------- | ----------- | ------------- | ------------- | ------------ | ------------- |
| 🔴<br />(Decreased >0%) | file1 | 80%<br>(+10%) | 14%<br>(-30%) | 3%<br>(+20%) | 20%<br>(-10%) |
| ✅ | file2 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴<br />(Below 100%) | file3 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| ✅ | (New) file4 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴<br />(Below 100%) | (New) file5 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |

Total:

Expand All @@ -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 |
| ----------------------- | ----------- | ------------- | ------------- | ------------ | ------------- |
| 🔴<br />(Decreased >0%) | file1 | 80%<br>(+10%) | 14%<br>(-30%) | 3%<br>(+20%) | 20%<br>(-10%) |
| ✅ | file2 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴<br />(Below 100%) | file3 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| ✅ | (New) file4 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |
| 🔴<br />(Below 100%) | (New) file5 | 20%<br>(+10%) | 8%<br>(+30%) | 2%<br>(+20%) | 5%<br>(+10%) |

Total:

| Lines | Branches | Functions | Statements |
| ----------- | ----------- | ----------- | ----------- |
| 100%(+100%) | 100%(+100%) | 100%(+100%) | 100%(+100%) |"
`;
31 changes: 31 additions & 0 deletions src/resultFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};

Expand All @@ -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();
});

Expand Down
38 changes: 31 additions & 7 deletions src/resultFormatter.ts
Original file line number Diff line number Diff line change
@@ -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}`;
};
Expand All @@ -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',
Expand All @@ -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}%`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this condition output be reversed? (or am I missing something)

);
}
if (decreased) {
reasons.push(`Decreased >${coverageDecreaseThreshold}%`);
}

const row = [
decreased || belowThreshold ? '🔴' : '✅',
`${isNewFile ? ' ' : ''}${file}`,
decreased || belowThreshold ? `🔴<br />(${reasons.join(' & ')})` : '✅',
`${isNewFile ? '(New) ' : ''}${file}`,
`${pcts.lines}%<br>(${formatDelta(deltas.lines)})`,
`${pcts.branches}%<br>(${formatDelta(deltas.branches)})`,
`${pcts.functions}%<br>(${formatDelta(deltas.functions)})`,
Expand Down