How to fix the issue where Jest finds tests but doesn’t collect coverage?

Sometimes, we want to fix the issue where Jest finds tests but doesn’t collect coverage.

In this article, we’ll look at how to fix the issue where Jest finds tests but doesn’t collect coverage.

How to fix the issue where Jest finds tests but doesn’t collect coverage?

To fix the issue where Jest finds tests but doesn’t collect coverage, we can add the --watchAll option when we runs tests.

For instance, we run

react-scripts test --coverage --watchAll.

to calculate code coverage from all tests.

We can also change the config to include more files in the code coverage calculation with

"jest": {
  "collectCoverageFrom": [
    "src/**/*.{js,jsx,ts,tsx}",
    "!<rootDir>/node_modules/"
  ],
  "coverageThreshold": {
    "global": {
      "lines": 90,
      "statements": 90
    }
  }
}

in the Jest config.

We set collectCoverageFrom to calculate coverage from any js, jsx, ts, and tsx file in the src folder and its children.

Conclusion

To fix the issue where Jest finds tests but doesn’t collect coverage, we can add the --watchAll option when we runs tests.