将Jest文本覆盖率报告输出到.txt

时间:2018-03-22 01:51:19

标签: javascript unit-testing jestjs istanbul

Jest允许您在package.json中指定coverage报告器,如此

{
    ...

    "jest": {
        "coverageDirectory": "./coverage",
        "coverageReporters": ["json", "text", ...]
    }

    ...
}

但是,这只会将.json摘要输出到coverage目录。它只将文本版本打印到控制台。有没有办法将文本版本输出到coverage文件夹中的.txt文件?

我指的是文档here,它说它与Istanbul reporters中的任何一个都兼容。文本伊斯坦布尔记者appears to have support for writing to a file。有没有办法利用它?

2 个答案:

答案 0 :(得分:2)

在玩笑的配置中将@NSManaged var parent: Parent 添加到json
coverageReports

然后安装istambul:
"coverageReporters": ["json"]

将此脚本添加到npm i -D istambul

package.json

该脚本将生成代码覆盖率报告文件"scripts": { "test": "jest --coverage && istanbul report --include coverage/coverage-final.json text > coverage.txt", ... } ,然后istambul将生成预期的输出,重定向到coverage-final.json

答案 1 :(得分:0)

@KerSplosh我们最终用shelljs编写了一个自定义脚本。基本上它运行测试,并将表写入fs的文件。

const shell = require("shelljs");
const path = require("path");
const fs = require("fs");

const result = shell.exec("yarn test --coverage");
fs.writeFileSync(
    path.resolve(".", "coverage.txt"),
    result.substring(result.indexOf("|\nFile") + 2)
);

if (result.code !== 0) {
    shell.exit(1);
}

但这并不理想。优选地,这将通过Jest配置来完成。但是当我实施这个时,我认为这是不可能的。