33

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

6 Answers 6

44
npm test -- --coverage

note that extra -- in the middle, docs

Sign up to request clarification or add additional context in comments.

4 Comments

what does that extra -- do?
whatever follows -- are arguments that npm will pass to the script being run, in this case probably jest. Will result in jest --coverage
This should be the accepted answer.
I have a react app created from create-react-app, to get the coverage I ran npm test -- --coverage and it worked for me, Thanks.
19

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

jest --coverage

1 Comment

This failed for TypeScript React project.
4

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

test: react-scripts test --coverage

Comments

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.

Comments

1

In my package.json

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

to see test coverage

yarn test:coverage

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.