24

We need to check test coverage for our React.js app and ideally get lcov.info output to send to a third-party coverage tracker like Coveralls or CodeClimate

Its unclear from the Jest API how to get test coverage information when running tests.

There's an Open Issue on GitHub: https://github.com/facebook/jest/issues/101 and associated Pull Request: https://github.com/facebook/jest/pull/178 but the PR still has not been merged by the Jest core team.

Is there an alternative way of getting coverage info that works today?

Note: I'm aware of @adrian-adkison suggestion in: https://stackoverflow.com/a/27479903/1148249 but @hankhsiao's fork is quite out-of-date with jest-cli since he submitted the Pull Request 2.5 months ago...

Community
  • 1
  • 1
nelsonic
  • 31,111
  • 21
  • 89
  • 120

6 Answers6

36
npm test -- --coverage

note that extra -- in the middle, docs

Ali Faris
  • 17,754
  • 10
  • 45
  • 70
18

@hankhsiao's fork has been merged. I have been using the latest https://github.com/facebook/jest and getting great results.

jest --coverage
Matt Norris
  • 8,596
  • 14
  • 59
  • 90
4

In your package.json under the script section add this piece of code -

test: react-scripts test --coverage
Dharman
  • 30,962
  • 25
  • 85
  • 135
akash ingole
  • 151
  • 1
  • 2
1

You can either run jest through

jest --coverage

or you can also set "collectCoverage" to true in your jest configuration inside your package.json. This way the coverage report will be generated everytime you run jest.

Although you should know that this can increase the time taken for your tests to run significantly.

Nahush Farkande
  • 5,290
  • 3
  • 25
  • 35
1

package.json

"scripts": {
    ...
    "test": "react-scripts test",
    ...
  },

run command

npm run test -- --watchAll=false  --coverage

you can also check in details

 /coverage/icov-report/src/...<file>.html
0

In my package.json

 "scripts": {
    ...
    "test": "react-scripts test",
    "test:coverage": "react-scripts test --coverage",
    ...
  },

to see test coverage

yarn test:coverage
Sanka Sanjeewa
  • 1,993
  • 14
  • 16