I trying to collect test coverage for this project using
yarn test --coverage # i.e. "react-scripts test --coverage"
My jest config is this:
"jest": { "collectCoverageFrom": [ "src/**/*.ts*" ], "coverageThreshold": { "global": { "lines": 90, "statements": 90 } } }
and my folder structure is as follows:
. ├── package.json ├── postcss.config.js ├── public/ ├── README.md ├── src/ │ ├── App.css │ ├── App.test.tsx │ ├── App.tsx │ ├── assets/ │ ├── components/ │ │ ├── Emoji/ │ │ │ ├── Emoji.test.tsx │ │ │ ├── Emoji.tsx │ │ │ └── index.ts │ │ └── Home/ │ │ ├── Home.css │ │ ├── Home.test.tsx │ │ ├── Home.tsx │ │ └── index.ts │ ├── index.css │ ├── index.tsx │ ├── react-app-env.d.ts │ └── serviceWorker.ts ├── tsconfig.json ├── yarn-error.log └── yarn.lock
Jest is being able to find all the tests but it fails to collect coverage:
PASS src/components/Home/Home.test.tsx PASS src/components/Emoji/Emoji.test.tsx PASS src/App.test.tsx ----------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ----------|----------|----------|----------|----------|-------------------| All files | 0 | 0 | 0 | 0 | | ----------|----------|----------|----------|----------|-------------------| Test Suites: 3 passed, 3 total Tests: 3 passed, 3 total Snapshots: 0 total Time: 3.432s Ran all test suites.
What am I missing? What should I add to the configuration to get the coverage?
Any hint is welcome :)
"src/**/*.{js,jsx,ts,tsx}"
.node_modules
and then running yarn
to reinstall everything.node_modules
and yarn.lock
, and then reinstall everything, which led to another bug, which I tried to solve installing that particular dependency, but it didn't work.Jest is collecting coverage only on the function under tests, not from the entire project. This means that despite we are seeing 100% coverage here, potentially we are testing only a fraction of our code. Now Jest is identify correctly what needs to be tested.
Jest, a flexible, easy-to-use testing framework, will be used to test a simple Typescript application. Codecov, a tool for monitoring code coverage, will be used to measure the test coverage for your Jest unit tests.
How to Calculate Test Coverage. Calculating test coverage is actually fairly easy. You can simply take the number of lines that are covered by a test (any kind of test, across your whole testing strategy) and divide by the total number of lines in your application.
The quick fix I said in my comment, using --watchAll
instead, eg: react-scripts test --coverage --watchAll
.
Just for future reference, I think ideally we should be using --watch
, which would only run on changed files, but it gave me the same trouble. I think it's related to this issue '--coverage --watch' should calculate coverage for all files at first iteration and also this issue No coverage when running in watch mode. I'm not sure why it worked for some people and not you, presumably something to do with Git and staging of files.
Not necessarily the solution in the original questioner's case, but i ran into the exact same problem and this was my solution:
I found that when upgrading jest (from 23 to 26) that i had this issue, and the resolution was to run with the --no-cache
option. Presumably they changed something about these coverage reports internally such that the cached data was incompatible.
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