Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm test -- --coverage never exits

Tags:

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

like image 863
Anup Avatar asked May 05 '19 11:05

Anup


Video Answer


2 Answers

-- --coverage part won't work, and should use one of the commands below to set CI to true.

By default npm test runs the watcher with interactive CLI. However, you can force it to run tests once and finish the process by setting an environment variable called CI.

source: React docs

Windows (cmd.exe)

  • set CI=true && npm test

  • set CI=true && npm run build

Windows (Powershell)

  • ($env:CI = "true") -and (npm test)

  • ($env:CI = "true") -and (npm run build)

Linux, macOS (Bash)

  • CI=true npm test

  • CI=true npm run build


NOT included in the docs

For Docker (node and react):

docker run -e CI=true [myImage] npm run test

like image 154
Matin Sasan Avatar answered Oct 10 '22 01:10

Matin Sasan


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 -- .

like image 39
micmor Avatar answered Oct 10 '22 01:10

micmor