在我的gitlab管道中,我想将总百分比值发送到服务器。但是开玩笑--coverage仅在/ coverage中提供了这些大型报告文件。我似乎无法解析其中的总价值。我是否缺少参数?
答案 0 :(得分:7)
内部玩笑正在使用Istanbul.js报告覆盖范围,您可以使用CLI arg将Jest's configuration修改为“文本摘要”或任何其他alternative reporter。
with npm
npm test -- --coverageReporters="text-summary"
with yarn
yarn test --coverageReporters="text-summary"
text-summary output:
=============================== Coverage summary ===============================
Statements : 100% ( 166/166 )
Branches : 75% ( 18/24 )
Functions : 100% ( 49/49 )
Lines : 100% ( 161/161 )
================================================================================
或者您可以编写自己的记者。
答案 1 :(得分:6)
感谢Teneff的回答,我选择了coverageReporter =“ json-summary”。
jest --coverage --coverageReporters="json-summary"
这将生成一个coverage-summary.json文件,可以轻松对其进行解析。我直接从json获取总值:
"total": {
"lines": { "total": 21777, "covered": 65, "skipped": 0, "pct": 0.3 },
"statements": { "total": 24163, "covered": 72, "skipped": 0, "pct": 0.3 },
"functions": { "total": 5451, "covered": 16, "skipped": 0, "pct": 0.29 },
"branches": { "total": 6178, "covered": 10, "skipped": 0, "pct": 0.16 }
}
答案 2 :(得分:1)
我自己需要这个,所以我创建了一个自定义记者。您需要在coverageReporters中启用json-summary报告程序,然后可以使用此自定义报告程序来显示总数:
const { readFile } = require('fs');
const { join } = require('path');
// Gitlab Regex: Total Coverage: (\d+\.\d+ \%)
module.exports = class CoverageReporter {
constructor(globalConfig) {
this.globalConfig = globalConfig;
this.jsonSummary = join(this.globalConfig.coverageDirectory, 'coverage-summary.json');
}
async onRunComplete() {
const coverage = require(this.jsonSummary);
const totalSum = ['lines', 'statements', 'functions', 'branches']
.map(i => coverage.total[i].pct)
.reduce((a, b) => a + b, 0)
const avgCoverage = totalSum / 4
console.debug()
console.debug('========= Total Coverage ============')
console.debug(`Total Coverage: ${avgCoverage.toFixed(2)} %`)
console.debug('=======================================')
}
}