'Jest coverage reporting on all committed files in create-react-app
Having difficulty with generating Jest coverage reports for all committed files in my create-react-app.
Initially, code coverage was generating as expected, however something has changed in my environment, and now only files changed since the last commit are showing coverage.
I see there are many other posts about this issue, however I am unable to resolve it myself.
package.json:
{...
"devDependencies": {
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.6.3",
"react-test-renderer": "^17.0.1"
},
"jest": {
"testMatch": [ "**/tests/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ],
"coverageReporters": ["json","html","lcov", "text"]
}
...}
Project structure:
|
+--node_modules
+--src
|
+ __tests__
+ App.js
- package.json
Running command npm test -- --coverage produces the following output:
No tests found related to files changed since last commit.
Press `a` to run all tests, or run Jest with `--watchAll`.
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------
Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press q to quit watch mode.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press Enter to trigger a test run.
Solution 1:[1]
You are in watch mode, which has a known issue when running coverage on a subset of files. Try using --watchAll so that all tests run and coverage can be generated.
npm test -- --coverage --watchAll
I like to make a special npm run-script in my package.json called coverage for this purpose.
Solution 2:[2]
You need to run this script react-scripts test --coverage with a included in order to run all tests.
Before:
react-scripts test --coverage
After:
react-scripts test --coverage a
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | evelynhathaway |
| Solution 2 | ouflak |
