Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest test coverage does not fail when threshold is not met

Utilizing create-react-app, when running tests in my CI pipeline, if the code coverage thresholds are not met, I expect the console to return a non-zero response.

package.json

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "test:coverage": "npm run test -- --coverage --watchAll=false",
  },
  "jest": {
    "collectCoverageFrom": [
      "src/components/**/*.js",
      "src/state/**/*.js",
      "src/templates/**/*.js",
      "src/routes/**/*.js"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 80,
        "lines": 80,
        "statements": 80
      }
    }
  }

When running test:coverage the console reports that thresholds were not met, but still returns 0. My understanding from the Jest documentation is that an error should be returned when coverage thresholds are not met.

https://jestjs.io/docs/en/configuration#coveragethreshold-object

Specifically...

If thresholds aren't met, jest will fail.

Is anyone familiar with this issue? I have been through Jest and CRA github issues with mixed results and most findings are related to outdated versions.

like image 973
Plummer Avatar asked Oct 23 '19 17:10

Plummer


1 Answers

To stop further execution when command fails:

command || exit 0

{
  "test:coverage": "npm run test -- --coverage --watchAll=false || exit 0"
}

ref: don't fail jenkins build if execute shell fails

like image 161
JimmyLv Avatar answered Sep 21 '22 11:09

JimmyLv