Skip to content

Commit

Permalink
Add option to pad ratchet percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
markis committed Feb 23, 2018
1 parent c722101 commit 5fee2da
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function onRunComplete(globalConfig: Config, options: RatchetOptions) {
const coverageRaw = readFileSync(coverageSummaryPath, 'utf-8');
const summary: IstanbulCoverage = JSON.parse(coverageRaw);
const threshold = globalConfig.coverageThreshold!;
const ratchetResult = ratchetCoverage(threshold, summary);
const ratchetResult = ratchetCoverage(threshold, summary, options);

updateFile(jestConfigPath, ratchetResult);
});
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export interface JestCoverageCategory {
}

export interface RatchetOptions {
disable?: boolean;
stop?: JestCoverage;
/** Pad the ratchet percentage by a specified number. */
ratchetPercentagePadding?: number;
}

export interface Config {
Expand Down
20 changes: 14 additions & 6 deletions src/ratchet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,47 @@ import {
IstanbulCoverageCategory,
JestCoverage,
JestCoverageCategory,
RatchetOptions,
} from './interfaces';

export function ratchetCoverage(
threshold: JestCoverage,
summary: IstanbulCoverage,
options: RatchetOptions,
): JestCoverage {
const result: any = {};
for (const key of Object.keys(threshold)) {
const summaryKey = key === 'global' ? 'total' : key;
result[key] = ratchetSingleCoverage(threshold[key], summary[summaryKey]);
result[key] = ratchetSingleCoverage(threshold[key], summary[summaryKey], options);
}
return result;
}

function ratchetSingleCoverage(
threshold: JestCoverageCategory,
summary: IstanbulCoverageCategories,
options: RatchetOptions,
) {
const { branches, functions, lines, statements } = threshold;
return {
branches: ratchetSingleNumberCoverage(branches, summary.branches),
functions: ratchetSingleNumberCoverage(functions, summary.functions),
lines: ratchetSingleNumberCoverage(lines, summary.lines),
statements: ratchetSingleNumberCoverage(statements, summary.statements),
branches: ratchetSingleNumberCoverage(branches, summary.branches, options),
functions: ratchetSingleNumberCoverage(functions, summary.functions, options),
lines: ratchetSingleNumberCoverage(lines, summary.lines, options),
statements: ratchetSingleNumberCoverage(statements, summary.statements, options),
};
}

function ratchetSingleNumberCoverage(
num: number | undefined,
category: IstanbulCoverageCategory,
options: RatchetOptions,
) {
if (num && category && num > 0 && num <= category.pct) {
return category.pct;
if (options.ratchetPercentagePadding) {
return Math.round(category.pct) - options.ratchetPercentagePadding;
} else {
return category.pct;
}
} else if (num && category && num < 0 && num >= -category.covered) {
return -category.covered;
}
Expand Down

0 comments on commit 5fee2da

Please sign in to comment.