Jest: code coverage summary and output file behave orthogonally

Created on 17 Feb 2017  ·  11Comments  ·  Source: facebook/jest

Do you want to request a feature or report a bug?

Bug.

What is the current behavior?

jest --verbose --coverage provides an output on stdout like:

--------------------|----------|----------|----------|----------|----------------|
File                |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
--------------------|----------|----------|----------|----------|----------------|
All files           |    60.39 |        0 |    44.44 |       62 |                |
 app                |    21.74 |        0 |        0 |    21.74 |                |
  utils.js          |    21.74 |        0 |        0 |    21.74 |... 33,35,43,50 |
 app/actions        |    67.18 |        0 |       50 |    69.29 |                |
  configurations.js |     61.9 |        0 |    41.18 |       65 |... 88,89,92,93 |
  networks.js       |      100 |      100 |      100 |      100 |                |
  profiles.js       |       60 |        0 |    43.75 |    61.36 |... 86,88,92,93 |
  templates.js      |       64 |        0 |    44.44 |    66.67 |... 43,44,47,48 |
--------------------|----------|----------|----------|----------|----------------|

But if you want to use the --outputFile option for the test output you must specify the --json option. When you use --json option this handy summary goes away. I tried to work around this by adding:

  "jest": {
    "coverageReporters": ["json", "text"]
  },

to package.json but that causes the output file to be empty until --json is again specified.

What is the expected behavior?

The ability to get an output file of the test results and to see the summary at the same time.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Linux or Mac

$ npm list jest
[email protected]
$ npm version
{ npm: '4.1.2',
  ares: '1.10.1-DEV',
  cldr: '30.0.3',
  http_parser: '2.7.0',
  icu: '58.2',
  modules: '51',
  node: '7.5.0',
  openssl: '1.0.2k',
  tz: '2016j',
  unicode: '9.0',
  uv: '1.10.2',
  v8: '5.4.500.48',
  zlib: '1.2.8' }

Most helpful comment

coverage-final.json doesn't have information about the total test coverage, it only lists data for each individual suite. So unless you write a parser to extract all that info and calculate a total from that, coverage-final.json is pretty useless, imo.

I agree with @alflennik - not having the coverage report in stdout when using --json seems like a bug.

All 11 comments

Do you want a file, like a coverage.txt with the code coverage?
when i do --outputFile it generates a file?

Sure. If I specify --outputFile I don't get any coverage info. So there's no way for me to capture test output and to get coverage info in the same run.

I have the same problem. Any update on this?

With default settings you get both the stdout reporter _and_ json report.
Defaults for coverageReporters are:

coverageReporters: ['json', 'text', 'lcov', 'clover'],

You'll find the report in coverage/coverage-final.json

@thymikee I think the intention was that the text reporter would be output to a file maybe coverage.txt and the text-summary reporter might go to a coverage-summary.txt

@thymikee what will be a keyword that I look for in coverage/coverage-final.json to get data about how many percentage of statements were covered in the test?

I can confirm this is still happening:

jest --coverage --watchAll has the stdout coverage report.
jest --coverage --watchAll --json --outputFile=jestResults.json has no stdout coverage report.

If I want the outputFile, I can't have the stdout coverage report.

This looks like a bug to me 🤷‍♂️

coverage-final.json doesn't have information about the total test coverage, it only lists data for each individual suite. So unless you write a parser to extract all that info and calculate a total from that, coverage-final.json is pretty useless, imo.

I agree with @alflennik - not having the coverage report in stdout when using --json seems like a bug.

This bug is still there as of today, doesn't look like its getting fixed.

I too wish I could have both a stdout and a file output for the coverage, so that I can see and parse it with my CI tool and make use of the normalized data.
Any project member could hop in and explain why this 2.5 years old issue is still standing, by any chance ?

I can get handy json summary by using nyc report
for example

$ jest --coverage  --silent --reporter=json-summary
$ ls -l coverage/coverage-final.json
$ nyc report -t coverage --reporter=json-summary

and then

$ jq .total.statements.pct coverage/coverage-summary.json
31.22

node_module nyc is needed additionally, so just info

Was this page helpful?
0 / 5 - 0 ratings