2016-11-28 57 views
1

我在我的Angular项目上运行karma/jasmine的测试覆盖率,并将覆盖范围输出到文件./coverage/coverage.json你如何检查与伊斯坦布尔单个文件的覆盖面?

我可以运行像这样的命令:

./node_modules/istanbul/lib/cli.js check-coverage --lines 90 
Output: ERROR: Coverage for lines (89.1%) does not meet global threshold (90%) 

这让我的全球测试覆盖率。

但我想要做的是检查只有一个文件的覆盖面。我会怎么做?

回答

0

它似乎并没有被内置到伊斯坦布尔,但我可以做的是创建一个总结JSON和终端运行它之后读取与节点。

首先,噶配置需要生成一个JSON总结与文件作为键:

coverageReporter: { 
    reporters: [ 
     {type: 'json-summary', subdir: './', file: 'coverage.json'} 
    ] 
} 

然后你可以运行一个终端任务中获得的git的所有暂存文件。

(gulp karma) 

ROOT_DIR=$(git rev-parse --show-toplevel) 
STAGED_FILES=($(git diff --cached --name-only --diff-filter=ACM | grep ".js$")) 

for file in ${STAGED_FILES}; do 
    echo "gulp coverage -f $ROOT_DIR/$file" 
    git show :$file | gulp coverage -f "$ROOT_DIR/$file" 
done; 

而且一饮而尽任务看起来是这样的:

gulp.task('coverage', function() { 
    var threshold = 90; 
    var coverageJSON = require('./coverage/coverage.json'); 

    var metrics = [ 'lines', 'statements', 'functions', 'branches' ]; 
    for (var i in metrics) { 
     if (coverageJSON[ argv.f ][ metrics[ i ] ].pct < threshold) { 
      console.log('ERROR:', argv.f, 'does not meet', threshold, 'percent coverage of', metrics[ i ]); 
      process.exit(0); 
     } 
    } 
}); 

技术上,终端部分可以使用节点exec做,但我想要一个shell命令,所以我可以做预提交执行。

0

我设法实现这与配置文件。

instrumentation: 
    excludes: [ '!yourfile.js' ] 

有窍门。

相关问题