Skip to content

Add new option: customFormatter #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface ConfigOptions {
coverageDecreaseThreshold?: number;
/* Fail coverage check if per-file coverage is lower than this for new files only */
newFileCoverageThreshold?: number;
/* Function to generate a custom output based on the diff */
customFormatter?: (files: FilesResults, totals: FileResultFormat) => string;
}

export interface DiffCheckResults {
Expand Down
10 changes: 9 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ describe('diff', () => {
checkCriteria: ['lines'],
coverageThreshold: 100,
coverageDecreaseThreshold: 0,
newFileCoverageThreshold: 1
newFileCoverageThreshold: 1,
customFormatter: jest.fn()
};

coverageDiff.diff(fileNotCovered, fileFullCovered, mockedOptions);
Expand All @@ -63,6 +64,13 @@ describe('diff', () => {
mockedOptions.coverageDecreaseThreshold,
mockedOptions.newFileCoverageThreshold
);
expect(mockedOptions.customFormatter).toHaveBeenCalledWith(
'mockedFiles',
{
deltas: 'mockedTotals',
pcts: 'mockedPcts'
}
);
});
});
});
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export function diff(
checkCriteria,
coverageThreshold,
coverageDecreaseThreshold,
newFileCoverageThreshold
newFileCoverageThreshold,
customFormatter
} = options;

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

const results = resultFormatter(files, totals);
const results =
customFormatter?.(files, totals) || resultFormatter(files, totals);

return {
diff,
Expand Down