22

If I run

npm test --coverage

the tests pass, but the coverage is not run.

When I change package.json to have

react-scripts test --coverage

Then, when I do npm test and a it runs the tests but not the coverage

The tests run along with the coverage report but the coverage shows all zeroes for coverage

 PASS  src/components/Header/SubHeader.test.js
  The <SubHeader /> component
    ✓ should render (4ms)
    ✓ should render with a goback button (1ms)
    ✓ should render

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   3 passed, 3 total
Time:        1.077s
Ran all test suites.

Finally, I realized that I can do

npm run test .

Question: Why don't the other methods work?

5
  • 7
    Which version of CRA and Jest are you using? I've had an issue in the past and using npm run test -- --coverage --watchAll=false did the trick for me (not optimal but...) Commented Sep 2, 2020 at 9:39
  • Note your first problem is explicitly called out in the docs - you're missing --, so --coverage goes to npm run not react-scripts test. That's not related to CRA/Jest, it's just how NPM scripts work. The second seems to be a bug: github.com/facebook/create-react-app/issues/7838. Commented Sep 2, 2020 at 10:00
  • Hey Michael, i edite my answer and figured out what was the deal. Please take a look. I guess there is more to explain why --coverage is not passed with npm, but . is passed Commented Sep 7, 2021 at 11:58
  • Hey Michael, finally i got the answer to your question. I asked SO community myselft about . being passed and it regards the option and positional parameter. Take a look at my answer, i edited it Commented Sep 14, 2021 at 13:56
  • I'm sure this solution will work for you: stackoverflow.com/a/76001983/5650332 Commented Apr 13, 2023 at 5:52

2 Answers 2

25

After revisiting this question i figured out that if you run this you will trigger coverage command and get all results.

SOLUTION 1

npm run test -- --coverage .

When you are passing arguments to npm script, you need to add -- before adding arguments like --coverage. This is just how npm works. See this answer for passing arguments to npm

SOLUTION 2

yarn test --coverage .

EDIT:

I asked a question regarding . being passed down the command and answer is pretty much simple. . is passed because it is a positional parameter, in contrast to --coverage that is option for npm test command: Why is . passed as argument to npm without -- delimiter?

---- Problem analysis ----

PROBLEM 1

npm test --coverage you are not passing --coverage argument to test script.

In the other case when you edit script to be:

react-scripts test --coverage and run npm test here you actually add argument --coverage, but in script itself, not from npm command

PROBLEM 2

When you do npm run test . you are passing . that means you want to include all files in this command.

For some reason . is passed to the npm run test script but not --coverage.

On the other side yarn passes all arguments without --.

Sign up to request clarification or add additional context in comments.

1 Comment

npm run test -- --coverage --watchAll=false
2

I do it this way:

npm run test -- --coverage --watchAll

the --watchAll makes it run code coverage for the entire project, instead of just the files that have been changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.