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?
-
Could you display the actual command that is running. It should be within the scripts object within the package.json. if within the command you have -- try removing it. an example would be jest . --coverage– connormcwoodMay 5, 2019 at 11:59
-
@connormcwood my script object is "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }– AnupMay 5, 2019 at 12:03
-
1There are lots of threads in relation to react-tests not terminating. You could try the following: set the value of the test to be CI=true react-scripts test --env=jsdom rather than just react-scripts test. See github.com/facebook/create-react-app/issues/1137– connormcwoodMay 5, 2019 at 12:08
-
3"test": "CI=true react-scripts test" solves the problem. Thanks– AnupMay 5, 2019 at 12:13
-
Quality, as the thread suggests, you have to state CI=true for some peculiar reason.– connormcwoodMay 5, 2019 at 12:17
6 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.
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
-
1for (React) the source for the above answer: create-react-app.dev/docs/running-tests/#travis-ci Aug 25, 2020 at 4:43
-
1
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 -- .
Most of the time this issue can be occur because of following reasons.
Not mentioning the required
npm-script
arguments in thepackage.json
file. If you usecreate-react-app
to create your react application, then it will not accept any command line arguments. To resolve this problem, add following line under thescript
tag in yourpackage.json
."test": "react-scripts test --coverage --watchAll", //mark --watchAll=false if you want.
Not mentioning the required
jest
configuration arguments in thepackage.json
orjest.config.js
files. You should mention the files which needed to include in your test coverage under the jest configurations. Add following configurations in yourpackage.json
.
package.json
"jest": {
"collectCoverageFrom": [
"src/**/*.js",
"!src/index.js", // files you need to avoid in test coverage
"!src/hooks/*.js",
"!src/context/*.js"
],
"coverageThreshold": {
"global": {
"branches": 90,
"functions": 90,
"lines": 90,
"statements": 90
}
},
"coverageReporters": [
"html",
"text"
]
},
Specifying a directory worked in my case
"test:cover": "react-scripts test --coverage src"
I tried all the solutions above, and for me it was still hanging with the message: Ran all test suites.
.
But this little hack helped:
"test:ci": "cross-env CI=true react-scripts test --forceExit --detectOpenHandles",
Explanation: The problem was coming from Jest not being able to close all processes. The above is a quick workaround. Ideally you should track the process that's stopping Jest from exiting.
In my case just added a new script "test:coverage": "react-scripts test --coverage"
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test:coverage": "react-scripts test --coverage",
"eject": "react-scripts eject"
},