2016-11-28 32 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' ] 

有竅門。

相關問題