Question

Jest coverage: How can I get a total percentage of coverage?

In my gitlab pipeline I want to send the total percentage value to a server. But jest --coverage only gives me these large reporting files in /coverage. I can't seem to parse the total value out of it. Am I missing a parameter?

 48  58626  48
1 Jan 1970

Solution

 55

Thanks to Teneff's answer, I go with the coverageReporter="json-summary".

 jest --coverage --coverageReporters="json-summary" 

This generates a coverage-summary.json file which can easily be parsed. I get the total values directly from the 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 }
  }
2020-04-01

Solution

 48

Internally jest is using Istanbul.js to report coverage and you can modify Jest's configuration with CLI arg to "text-summary" or any other alternative reporter.

jest --coverageReporters="text-summary"

text-summary output:

=============================== Coverage summary ===============================
Statements   : 100% ( 166/166 )
Branches     : 75% ( 18/24 )
Functions    : 100% ( 49/49 )
Lines        : 100% ( 161/161 )
================================================================================

Or you can write your own reporter.

2020-04-01