Output Jest text coverage reporter to .txt
Asked Answered
D

4

8

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 compatible 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?

Doughnut answered 22/3, 2018 at 1:51 Comment(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.txtNadeen
Yeah unfortunately that also contains the test results. I'm just trying to export the coverage results in the table to the .txtDoughnut
I'm also trying to do something like this, the company I work for has a standard devops reporting platform and I need to export some of the jest test coverage stats for thatGrooved
P
5

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.

Pigskin answered 1/9, 2022 at 22:35 Comment(0)
S
2

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

Sturmabteilung answered 29/9, 2018 at 19:56 Comment(1)
Minor correction: npm i -D istanbul (for anybody who copy+pastes it and wonders why npm couldn't find the package... you know... people like me)Acidfast
D
0

@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.

Doughnut answered 31/5, 2018 at 12:43 Comment(0)
M
0

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 command 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.

Mayflower answered 27/1, 2022 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.