Skip to content

Commit 0c7d5e6

Browse files
authored
Add new option: customFormatter (#37)
* Add new option customFormatter
1 parent 2ef6a47 commit 0c7d5e6

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export interface ConfigOptions {
3535
coverageDecreaseThreshold?: number;
3636
/* Fail coverage check if per-file coverage is lower than this for new files only */
3737
newFileCoverageThreshold?: number;
38+
/* Function to generate a custom output based on the diff */
39+
customFormatter?: (files: FilesResults, totals: FileResultFormat) => string;
3840
}
3941

4042
export interface DiffCheckResults {

src/index.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ describe('diff', () => {
5050
checkCriteria: ['lines'],
5151
coverageThreshold: 100,
5252
coverageDecreaseThreshold: 0,
53-
newFileCoverageThreshold: 1
53+
newFileCoverageThreshold: 1,
54+
customFormatter: jest.fn()
5455
};
5556

5657
coverageDiff.diff(fileNotCovered, fileFullCovered, mockedOptions);
@@ -63,6 +64,13 @@ describe('diff', () => {
6364
mockedOptions.coverageDecreaseThreshold,
6465
mockedOptions.newFileCoverageThreshold
6566
);
67+
expect(mockedOptions.customFormatter).toHaveBeenCalledWith(
68+
'mockedFiles',
69+
{
70+
deltas: 'mockedTotals',
71+
pcts: 'mockedPcts'
72+
}
73+
);
6674
});
6775
});
6876
});

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export function diff(
2121
checkCriteria,
2222
coverageThreshold,
2323
coverageDecreaseThreshold,
24-
newFileCoverageThreshold
24+
newFileCoverageThreshold,
25+
customFormatter
2526
} = options;
2627

2728
const { regression, files, totals, diff, belowThreshold } = diffChecker(
@@ -33,7 +34,8 @@ export function diff(
3334
newFileCoverageThreshold
3435
);
3536

36-
const results = resultFormatter(files, totals);
37+
const results =
38+
customFormatter?.(files, totals) || resultFormatter(files, totals);
3739

3840
return {
3941
diff,

0 commit comments

Comments
 (0)