score:58

Accepted answer

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.

score:-1

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

score:0

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.

score:0

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)

score:0

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 

score:1

For me, I've changed the folder's name and forgot to update the collectCoverageFrom key under the jest.config.ts file.

score:2

Been there... You're missing one more parameter in Jest config:

collectCoverage: true

score:2

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"
  ]
}

score: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

score:9

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.

Working: enter image description here

score:23

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.


Related Query

More Query from same tag