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

TFi*_*her 7 javascript unit-testing istanbul jestjs

Jest 允许您在 package.json 中指定覆盖率报告器,如下所示

{
    ...

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

    ...
}
Run Code Online (Sandbox Code Playgroud)

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

我一直在参考这里的文档,它说它与任何伊斯坦布尔记者兼容。文本“伊斯坦布尔记者”似乎支持写入文件。有什么办法可以利用它吗?

TFi*_*her 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);
}
Run Code Online (Sandbox Code Playgroud)

但这并不理想。最好通过 Jest 配置来完成此操作。但在我实现这个的时候,我认为这是不可能的。