npm test -- --coverage never exits

Anup picture Anup · May 5, 2019 · Viewed 14.7k times · Source

I am using create-react-app to create a react application. When I executes npm test -- --coverage the test never exists. npm test actually runs react-scripts test. Any Idea?

enter image description here

Answer

micmor picture micmor · May 28, 2019

Coverage won't work with Jest in watch mode.

Because "react-scripts test --env=jsdom" works in watch mode by default, the watch mode has to be switched off while generating the coverage output.

The following excerpt from the package.json contains a line "coverage" for illustration, how code coverage can be achieved within an app which was bootet by create-react-app.

It's just the modified "test" script, where the options --watchAll=false and --coverage are added in combination:

 "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "coverage": "react-scripts test --env=jsdom --watchAll=false --coverage",
    "eject": "react-scripts eject"
  }

Please note that it is obsolete to use standalone double-dash -- .