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...
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.
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:
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.
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.
npm test -- --coverage
note that extra --
in the middle, docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With