Skip to content

Commit

Permalink
feat(thresholds): allow threshold logs not to be emitted as errors
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
Matt Lewis committed May 26, 2017
1 parent bc957a1 commit 2de647c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module.exports = function (config) {
// enforce percentage thresholds
// anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode
thresholds: {
emitWarning: false, // set to `true` to not fail the test command when thresholds are not met
global: { // thresholds for all files
statements: 100,
lines: 100,
Expand Down
18 changes: 15 additions & 3 deletions src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
reporter.write(remappedCoverageMap);

const thresholds = {
emitWarning: false,
global: {
statements: 0,
lines: 0,
Expand All @@ -102,19 +103,30 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
if (userThresholds.global || userThresholds.each) {
Object.assign(thresholds.global, userThresholds.global);
Object.assign(thresholds.each, userThresholds.each);
if (userThresholds.emitWarning === true) {
thresholds.emitWarning = true;
}
} else {
Object.assign(thresholds.global, userThresholds);
}
}

function logThresholdMessage(message) {
if (thresholds.emitWarning) {
log.warn(message);
} else {
log.error(message);
}
}

let thresholdCheckFailed = false;

// Adapted from https://github.com/istanbuljs/nyc/blob/98ebdff573be91e1098bb7259776a9082a5c1ce1/index.js#L463-L478
const globalSummary = remappedCoverageMap.getCoverageSummary();
const failedGlobalTypes = checkThresholds(thresholds.global, globalSummary);
failedGlobalTypes.forEach(type => {
thresholdCheckFailed = true;
log.error(`Coverage for ${type} (${globalSummary[type].pct}%) does not meet global threshold (${thresholds.global[type]}%)`);
logThresholdMessage(`Coverage for ${type} (${globalSummary[type].pct}%) does not meet global threshold (${thresholds.global[type]}%)`);
});

remappedCoverageMap.files().forEach(file => {
Expand All @@ -126,11 +138,11 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
if (coverageIstanbulReporter.fixWebpackSourcePaths) {
file = util.fixWebpackFilePath(file);
}
log.error(`Coverage for ${type} (${fileSummary[type].pct}%) in file ${file} does not meet per file threshold (${thresholds.each[type]}%)`);
logThresholdMessage(`Coverage for ${type} (${fileSummary[type].pct}%) in file ${file} does not meet per file threshold (${thresholds.each[type]}%)`);
});
});

if (thresholdCheckFailed && results) {
if (thresholdCheckFailed && results && !thresholds.emitWarning) {
results.exitCode = 1;
}
});
Expand Down
31 changes: 31 additions & 0 deletions test/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,36 @@ describe('karma-coverage-istanbul-reporter', () => {
setTimeout(checkOutput, fileReadTimeout); // Hacky workaround to make sure the output file has been written
});
});

it('should emit the threshold log as a warning', done => {
const server = createServer({
coverageIstanbulReporter: {
reports: ['json-summary'],
dir: path.join(__dirname, 'fixtures', 'outputs'),
thresholds: {
emitWarning: true,
global: {
statements: 100,
lines: 100,
branches: 100,
functions: 100
}
}
}
});
server.start();

function checkOutput() {
const output = fs.readFileSync(OUTPUT_LOG_FILE).toString();
expect(output).to.contain('[WARN] reporter.coverage-istanbul - Coverage for statements (81.82%) does not meet global threshold (100%)');
expect(output).to.contain('[WARN] reporter.coverage-istanbul - Coverage for lines (81.82%) does not meet global threshold (100%)');
expect(output).to.contain('[WARN] reporter.coverage-istanbul - Coverage for functions (60%) does not meet global threshold (100%)');
done();
}

server.on('run_complete', () => {
setTimeout(checkOutput, fileReadTimeout); // Hacky workaround to make sure the output file has been written
});
});
});
});

0 comments on commit 2de647c

Please sign in to comment.