Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get test coverage from Jest while testing React.js App?

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

like image 235
nelsonic Avatar asked Feb 02 '15 17:02

nelsonic


People also ask

What is the use of jest in react testing?

Jest is a test runner that finds tests, runs the tests, and determines whether the tests passed or failed. Additionally, Jest offers functions for test suites, test cases, and assertions. React Testing Library provides virtual DOMs for testing React components.

How do I use React's test renderer and jest's snapshot feature?

Now let's use React's test renderer and Jest's snapshot feature to interact with the component and capture the rendered output and create a snapshot file: When you run yarn test or jest, this will produce an output file like this:

What is the best way to test a react app?

At Facebook, we use Jest to test React applications. If you are new to React, we recommend using Create React App. It is ready to use and ships with Jest! You will only need to add react-test-renderer for rendering snapshots.

Does create-react-app use jest?

create-react-app uses both Jest and React Testing Library by default. Additionally, [react-scripts] automatically sets up our server to watch for changes, so if the test file is modified, the server automatically compiles and runs the test without needing to restart your server.


1 Answers

npm test -- --coverage

note that extra -- in the middle, docs

like image 107
Ali Faris Avatar answered Sep 17 '22 22:09

Ali Faris