'Jest finds tests but doesn't collect coverage
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 :)
Tries
- Changing the glob pattern to
"src/**/*.{js,jsx,ts,tsx}"
. - Removing
node_modules
and then runningyarn
to reinstall everything. - Removing
node_modules
andyarn.lock
, and then reinstall everything, which led to another bug, which I tried to solve installing that particular dependency, but it didn't work. - Cloning the repository from GitHub and then running the command on the fresh version.
- Switching to a different Node version (v10.16.2, and v11.7.0).
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
Seems to be working fine on Linux Mint 19.2. I'd suggest changing your jest config to something a bit more flexible:
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!<rootDir>/node_modules/"
],
"coverageThreshold": {
"global": {
"lines": 90,
"statements": 90
}
}
}
And then change your package.json
test script if you're using npm
(with yarn
you can just append --coverage
, for example: yarn test --coverage
; however, with npm
, it'll just ignore it). So I'd suggest either doing this:
"test": "react-scripts test --coverage",
Or, I'd recommend using yarn
over npm
. To install yarn, use one of the following methods.
Solution 4:[4]
What helped me is that instead of
npm run test
or
npm run test --watchall
I did this:
npm run test a
and
npm run test a -- --coverage
Solution 5:[5]
In our case the problem was with the jest rootDir
setting (in our package.json) which we had set to tests
. Jest therefore ignored our src/
folder where our actual source code was. The rolution was to tell jest about both folders:
package.json old
"jest": {
"rootDir: "tests"
}
package.json new
"jest": {
"roots": [
"src",
"tests"
]
}
Solution 6:[6]
Been there... You're missing one more parameter in Jest config:
collectCoverage: true
Solution 7:[7]
For me, I've changed the folder's name and forgot to update the collectCoverageFrom
key under the jest.config.ts
file.
Solution 8:[8]
In my case, I was testing React Native/Expo app with the option cacheDirectory: '.jest/cache'
. Deleting the .jest
directory has solved the issue for me.
Solution 9:[9]
In my case I was able to get to work by deleting cache and do npm install
. In windows cache location (~\AppData\Roaming\npm-cache)
Solution 10:[10]
Seems the problem is with the flag --watchAll Without setting it to false it does not generate coverage
react-scripts test --reporters=jest-junit --reporters=default --coverage --coverageDirectory='testresults/coverage' --coverageReporters=cobertura --coverageReporters=lcov --watchAll=false
Solution 11:[11]
Add this to your package.json
"jest": {
"testPathIgnorePatterns": [
"<rootDir>/path/to/ignore/"
]
}
This will solve the issue of yours instead of specifying a lot of these commands
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow