karma-coverage-istanbul-reporter
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "karma-coverage-istanbul-reporter", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A karma reporter that uses the latest istanbul 1.x APIs (with full sourcemap support) to report coverage.", | ||
@@ -10,2 +10,3 @@ "main": "src/reporter.js", | ||
"scripts": { | ||
"start": "npm run test:watch", | ||
"lint": "xo", | ||
@@ -15,5 +16,8 @@ "pretest": "npm run lint", | ||
"test:watch": "mocha --watch", | ||
"preversion": "npm test", | ||
"postversion": "npm publish", | ||
"codecov": "cat coverage/lcov.info | codecov" | ||
"codecov": "cat coverage/lcov.info | codecov", | ||
"commitmsg": "validate-commit-msg", | ||
"commit": "git-cz", | ||
"prerelease": "npm test", | ||
"release": "standard-version && git push --follow-tags origin master", | ||
"postrelease": "npm publish" | ||
}, | ||
@@ -45,2 +49,5 @@ "repository": { | ||
"codecov-lite": "^0.1.3", | ||
"commitizen": "^2.9.6", | ||
"cz-conventional-changelog": "^2.0.0", | ||
"husky": "^0.13.3", | ||
"istanbul-instrumenter-loader": "^2.0.0", | ||
@@ -55,2 +62,3 @@ "karma": "^1.3.0", | ||
"rimraf": "^2.5.4", | ||
"standard-version": "^4.0.0", | ||
"ts-loader": "^2.0.0", | ||
@@ -60,2 +68,3 @@ "tslint": "^5.1.0", | ||
"typescript": "^2.1.0", | ||
"validate-commit-msg": "^2.12.1", | ||
"webpack": "^2.2.0", | ||
@@ -77,3 +86,8 @@ "xo": "^0.18.0" | ||
] | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "node_modules/cz-conventional-changelog" | ||
} | ||
} | ||
} |
@@ -67,6 +67,14 @@ # karma-coverage-istanbul-reporter | ||
thresholds: { | ||
statements: 100, | ||
lines: 100, | ||
branches: 100, | ||
functions: 100 | ||
global: { // thresholds for all files | ||
statements: 100, | ||
lines: 100, | ||
branches: 100, | ||
functions: 100 | ||
}, | ||
each: { // thresholds per file | ||
statements: 100, | ||
lines: 100, | ||
branches: 100, | ||
functions: 100 | ||
} | ||
} | ||
@@ -73,0 +81,0 @@ |
'use strict'; | ||
const istanbul = require('istanbul-api'); | ||
const fixWebpackSourcePaths = require('./util').fixWebpackSourcePaths; | ||
const util = require('./util'); | ||
const BROWSER_PLACEHOLDER = '%browser%'; | ||
function checkThresholds(thresholds, summary) { | ||
const failedTypes = []; | ||
Object.keys(thresholds).forEach(key => { | ||
const coverage = summary[key].pct; | ||
if (coverage < thresholds[key]) { | ||
failedTypes.push(key); | ||
} | ||
}); | ||
return failedTypes; | ||
} | ||
function CoverageIstanbulReporter(baseReporterDecorator, logger, config) { | ||
@@ -22,3 +35,3 @@ baseReporterDecorator(this); | ||
const baseReporterOnRunComplete = this.onRunComplete; | ||
this.onRunComplete = function (browsers) { | ||
this.onRunComplete = function (browsers, results) { | ||
baseReporterOnRunComplete.apply(this, arguments); | ||
@@ -52,3 +65,3 @@ | ||
if (fileCoverage.inputSourceMap && coverageIstanbulReporter.fixWebpackSourcePaths) { | ||
fileCoverage.inputSourceMap = fixWebpackSourcePaths(fileCoverage.inputSourceMap); | ||
fileCoverage.inputSourceMap = util.fixWebpackSourcePaths(fileCoverage.inputSourceMap); | ||
} | ||
@@ -73,20 +86,53 @@ if ( | ||
const thresholds = coverageIstanbulReporter.thresholds; | ||
if (thresholds) { | ||
// Adapted from https://github.com/istanbuljs/nyc/blob/98ebdff573be91e1098bb7259776a9082a5c1ce1/index.js#L463-L478 | ||
let thresholdCheckFailed = false; | ||
const summary = remappedCoverageMap.getCoverageSummary(); | ||
Object.keys(thresholds).forEach(key => { | ||
const coverage = summary[key].pct; | ||
if (coverage < thresholds[key]) { | ||
thresholdCheckFailed = true; | ||
log.error(`Coverage for ${key} (${coverage}%) does not meet global threshold (${thresholds[key]}%)`); | ||
const thresholds = { | ||
global: { | ||
statements: 0, | ||
lines: 0, | ||
branches: 0, | ||
functions: 0 | ||
}, | ||
each: { | ||
statements: 0, | ||
lines: 0, | ||
branches: 0, | ||
functions: 0 | ||
} | ||
}; | ||
const userThresholds = coverageIstanbulReporter.thresholds; | ||
if (userThresholds) { | ||
if (userThresholds.global || userThresholds.each) { | ||
Object.assign(thresholds.global, userThresholds.global); | ||
Object.assign(thresholds.each, userThresholds.each); | ||
} else { | ||
Object.assign(thresholds.global, userThresholds); | ||
} | ||
} | ||
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]}%)`); | ||
}); | ||
remappedCoverageMap.files().forEach(file => { | ||
const fileSummary = remappedCoverageMap.fileCoverageFor(file).toSummary().data; | ||
const failedFileTypes = checkThresholds(thresholds.each, fileSummary); | ||
failedFileTypes.forEach(type => { | ||
thresholdCheckFailed = true; | ||
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]}%)`); | ||
}); | ||
/* istanbul ignore if */ | ||
if (thresholdCheckFailed && config.singleRun) { | ||
process.on('exit', () => { | ||
process.exit(1); | ||
}); | ||
} | ||
}); | ||
if (thresholdCheckFailed && results) { | ||
results.exitCode = 1; | ||
} | ||
@@ -93,0 +139,0 @@ }); |
@@ -1,16 +0,24 @@ | ||
function fixWebpackSourcePaths(sourceMap) { | ||
function fixWebpackFilePath(filePath) { | ||
const isWin = process.platform.startsWith('win'); | ||
if (filePath.indexOf('!') !== -1) { | ||
filePath = filePath.split('!').pop(); | ||
} | ||
if (filePath.indexOf('?') !== -1) { | ||
filePath = filePath.split('?')[0]; | ||
} | ||
// Workaround for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/9 | ||
if (isWin) { | ||
filePath = filePath.replace(/\\/g, '/'); | ||
} | ||
return filePath; | ||
} | ||
function fixWebpackSourcePaths(sourceMap) { | ||
return Object.assign({}, sourceMap, { | ||
sources: sourceMap.sources.map(source => { | ||
if (source.indexOf('!') !== -1) { | ||
source = source.split('!').pop(); | ||
} | ||
if (source.indexOf('?') !== -1) { | ||
source = source.split('?')[0]; | ||
} | ||
// Workaround for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/9 | ||
if (isWin) { | ||
source = source.replace(/\\/g, '/'); | ||
} | ||
source = fixWebpackFilePath(source); | ||
if (sourceMap.sourceRoot && source.startsWith(sourceMap.sourceRoot)) { | ||
@@ -25,1 +33,2 @@ source = source.replace(sourceMap.sourceRoot, ''); | ||
module.exports.fixWebpackSourcePaths = fixWebpackSourcePaths; | ||
module.exports.fixWebpackFilePath = fixWebpackFilePath; |
15571
6
142
109
24