最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Output Jest text coverage reporter to .txt - Stack Overflow

programmeradmin3浏览0评论

Jest allows you to specify a coverage reporter in the package.json like so

{
    ...

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

    ...
}

However, this only outputs the .json summary to the coverage directory. It only prints the text version to the console. Is there a way to output the text version to a .txt file in the coverage folder as well?

I've been referring to the docs here, which says that it is patible with any of the Istanbul reporters. The text Istanbul reporter appears to have support for writing to a file. Is there any way to utilize it this?

Jest allows you to specify a coverage reporter in the package.json like so

{
    ...

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

    ...
}

However, this only outputs the .json summary to the coverage directory. It only prints the text version to the console. Is there a way to output the text version to a .txt file in the coverage folder as well?

I've been referring to the docs here, which says that it is patible with any of the Istanbul reporters. The text Istanbul reporter appears to have support for writing to a file. Is there any way to utilize it this?

Share Improve this question edited Nov 3, 2018 at 10:38 skyboyer 23.7k7 gold badges62 silver badges71 bronze badges asked Mar 22, 2018 at 1:51 TFischerTFischer 1,3482 gold badges25 silver badges45 bronze badges 3
  • Unfortunately I think the text coverageReporter is just an alias for text-summary which just goes to stdout. You can redirect it if you want, but it's an extra step and it might contain other noise: jest --coverage > coverage/coverage.txt – charmeleon Commented Mar 22, 2018 at 2:04
  • Yeah unfortunately that also contains the test results. I'm just trying to export the coverage results in the table to the .txt – TFischer Commented Mar 22, 2018 at 2:09
  • I'm also trying to do something like this, the pany I work for has a standard devops reporting platform and I need to export some of the jest test coverage stats for that – KerSplosh Commented May 31, 2018 at 7:57
Add a ment  | 

4 Answers 4

Reset to default 6

In your jest config, you can specify the file option, and optionally the dir options which defaults to the current working directory:

"jest": {
  "coverageReporters": [["text", { file: 'coverage.txt' }]]
}

These options are mentioned briefly in the jest docs, with details in the istanbul docs here.

Add json to coverageReports in your jest config:
"coverageReporters": ["json"]

Then install istanbul:
npm i -D istanbul

Add this script to package.json:

"scripts": {  
  "test": "jest --coverage && istanbul report --include coverage/coverage-final.json text > coverage.txt",  
  ...
}

The script will generate the code coverage report file coverage-final.json then istanbul will generate the expected output redirected to coverage.txt

@KerSplosh We ended up writing a custom script with shelljs. Basically it runs the tests, and writes the table to a file with 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);
}

This isn't ideal though. Preferably this would be done through the Jest configuration. But at the time I implemented this, I don't think it wasn't possible.

If you happen to use react scripts on top of jest:

Add these snippets to their sections in package.json:

"scripts": {
   "cover:report": "react-scripts test --coverage .> coverage/coverage-report.txt",
}
...
"jest": {
  "collectCoverageFrom": [
     "src/**/*.ts*"
  ],
  "coverageReporters": ["text"]
}

This generates coverage report in file coverage/coverage-report.txt. The dot in ".>" part tells the script to take all files (except for ignored ones) that match the "." pattern - which would typically be all of them.

Modify the "collectCoverageFrom" array of strings to include files/folders as needed.

This mand doesn't exit itself unfortunately so you have to Ctrl+C to end it when it just hangs in there after being done.

To run it in terminal: "yarn cover:report"

The result contains plaintext table of coverage results.

发布评论

评论列表(0)

  1. 暂无评论