1

I have an angular 8 project and I am trying to set up a case where the user cannot commit unless certain % of code coverage passes.

I am using NX Workspace, and Husky to run Linting before committing. I want to run on top of this, ng test, and also % of coverage test for which I can provide the number. If all three pass, then commit the code.

I have this package.json file for now. How can I add on top of this?

  "scripts": {
    "ng": "ng",
    "nx": "nx",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "nx workspace-lint && ng lint",
    "e2e": "ng e2e",
    "affected:apps": "nx affected:apps",
    "affected:libs": "nx affected:libs",
    "affected:build": "nx affected:build",
    "affected:e2e": "nx affected:e2e",
    "affected:test": "nx affected:test",
    "affected:lint": "nx affected:lint",
    "affected:dep-graph": "nx affected:dep-graph",
    "affected": "nx affected",
    "format": "nx format:write",
    "format:write": "nx format:write",
    "format:check": "nx format:check",
    "update": "ng update @nrwl/workspace",
    "update:check": "ng update",
    "workspace-schematic": "nx workspace-schematic",
    "dep-graph": "nx dep-graph",
    "help": "nx help",
    "storybook": "start-storybook -p 6006",
    "build-storybook": "build-storybook"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run affected:lint"
    }
  },

...
Missak Boyajian
  • 1,965
  • 7
  • 32
  • 59

1 Answers1

0

You should be able to make a separate script that reads the standard output of the testing script to extract the coverage percentage with regular expressions.

Then it will store the percentage in a variable and compare it with the minimum coverage amount to set the appropriate error level that is then returned to git.

The script file will be run by husky.

e.g.

usr_coverage=12; # store the extracted coverage here
min_coverage=10;

if [ $usr_coverage -lt $min_coverage]; then
    echo "Coverage is too low.";
    exit 1;
fi
exit 0
beppe9000
  • 1,056
  • 1
  • 13
  • 28