How to run a git pre-commit hook to enforce that test coverage for the staged files doesn't decrease?
Asked Answered
P

1

13

I am using jest and istanbul in my ReactJS project to write test cases and check on the test coverage.

How do I ensure using a pre-commit hook that test coverage for any file, that I have staged to git, doesn't decrease from its current value before its committed?

Primarily answered 28/6, 2019 at 10:53 Comment(0)
P
4

You should check coverageThreshold documentation of jest from here

Below options are possible for global coverage threshold and file name pattern thresholds.

{
  ...
  "jest": {
    "coverageThreshold": {
      "global": {
        "branches": 50,
        "functions": 50,
        "lines": 50,
        "statements": 50
      },
      "./src/components/": {
        "branches": 40,
        "statements": 40
      },
      "./src/reducers/**/*.js": {
        "statements": 90
      },
      "./src/api/very-important-module.js": {
        "branches": 100,
        "functions": 100,
        "lines": 100,
        "statements": 100
      }
    }
  }
}

You can combine this with lint staged and husky to make the check in pre-commit.

In the end, your package.json would look like this:

{
  ...package.json
  "husky": {
    "hooks": {
      "pre-commit": "jest",
    }
  }
}
Pyxie answered 28/6, 2019 at 11:20 Comment(2)
I only want to ensure that with every commit the test coverage doesn't decrease for the staged file.How do I do this without actually defining any fixed threshold?Primarily
check npmjs.com/package/jest-coverage-validator and npmjs.com/package/jest-coverage-ratchetWorker

© 2022 - 2024 — McMap. All rights reserved.