From 2de647c2de11b38a68b021eed0a79641ee68f69c Mon Sep 17 00:00:00 2001 From: Matt Lewis Date: Fri, 26 May 2017 14:01:14 +0100 Subject: [PATCH] feat(thresholds): allow threshold logs not to be emitted as errors Closes #19 --- README.md | 1 + src/reporter.js | 18 +++++++++++++++--- test/reporter.spec.js | 31 +++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e59ff96..42b632f 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/src/reporter.js b/src/reporter.js index e55299f..f17e6ce 100644 --- a/src/reporter.js +++ b/src/reporter.js @@ -82,6 +82,7 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) { reporter.write(remappedCoverageMap); const thresholds = { + emitWarning: false, global: { statements: 0, lines: 0, @@ -102,11 +103,22 @@ 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 @@ -114,7 +126,7 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) { 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 => { @@ -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; } }); diff --git a/test/reporter.spec.js b/test/reporter.spec.js index 2496316..34173f1 100644 --- a/test/reporter.spec.js +++ b/test/reporter.spec.js @@ -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 + }); + }); }); });