Jest test coverage does not fail when threshold is not met
Asked Answered
I

4

20

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.

Iridosmine answered 23/10, 2019 at 17:56 Comment(0)
E
7

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

Eisen answered 24/2, 2020 at 13:50 Comment(0)
C
6

Add "collectCoverage": true, in your jest configuration. After that jest run operation will fail, if the coverage is not met.

Centrifugal answered 15/7, 2022 at 10:54 Comment(1)
This enables coverage even for the case when you just want to run the tests or a subset of the tests. If you run a subset of tests, e.g. using a pattern matching, then those test runs are highly like to fail because a large portion of the tests are not run in those cases. The question here was about an npm script that specifically targets coverage. Generally, I believe you want to run coverage on your entire test suite, not just on a subset.Capitulate
D
4

Defining the config in the packjage.json file did not fail the jest command when coverage threshold was not met. But it worked when I defined it through the jest.config.js file!

Dispossess answered 8/7, 2021 at 21:3 Comment(0)
H
0

This will make the test fail if the conditions are not met, but please note that this will slow down your tests:

 "scripts": {
        "test": "jest --collectCoverage=true",
        "coverage" : "jest --coverage"
      },
Hippogriff answered 21/8, 2022 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.