Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jestjs - 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.
question from:https://stackoverflow.com/questions/66056448/jest-coverage-reporting-on-all-committed-files-in-create-react-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...