Skip to content

Commit

Permalink
Add option to pad ratchet percentages (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
markis committed Feb 23, 2018
1 parent 82de15a commit 9518ff1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 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
27 changes: 18 additions & 9 deletions src/ratchet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,49 @@ 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;
} else if (num && category && num < 0 && num >= -category.covered) {
return -category.covered;
if (num && category) {
const ratchetPct = options.ratchetPercentagePadding
? Math.round(category.pct) - options.ratchetPercentagePadding
: category.pct;
if (num > 0 && num <= ratchetPct) {
return ratchetPct;
} else if (num < 0 && num >= -category.covered) {
return -category.covered;
}
}
}
43 changes: 43 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,49 @@ describe('jest-ratchet', () => {
}));
});

it('will pad the ratchet percentages', () => {
const PADDING = 2;
const threshold = {
coverageThreshold: {
global: {
branches: 50,
},
},
};
fs.__addMockFile(
/\/coverage-summary\.json$/,
JSON.stringify({
total: {
branches: {pct: 100},
},
}),
);
fs.__addMockFile(
/\/package\.json$/,
JSON.stringify({ ...threshold }),
);

const jestRatchet = new JestRatchet({
...mockConfig,
...threshold,
rootDir: './example',
}, {
ratchetPercentagePadding: PADDING,
});
jestRatchet.onRunComplete();

const writeFileSync = fs.writeFileSync as jest.Mock;
expect(writeFileSync.mock.calls[0][1])
.toEqual(
JSON.stringify({
coverageThreshold: {
global: {
branches: 98,
},
},
}));
});

it('will respect the --config flag', () => {
process.argv = ['', '', '--config', 'jestconfig.json'];
const threshold = {
Expand Down

0 comments on commit 9518ff1

Please sign in to comment.