I'm using Jest to test a NestJS application and I'm trying to create a git hook with husky that will not allow a commit if tests coverage are under 95%, I haven't tried anything yet cause I really don't know how to even describe my question properly. But just to be more clear, what I need is a git hook just like this:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm run format
npm run lint
npm run test
git add -u
create a git hook with husky that will not allow a commit if tests coverage are under 95%
It seems you already have a test hook on husky, and if that npm run test
is using Jest as I'm assuming it does, all you need to do is add a Jests configuration to your package.json
.
Open package.json
and search for a property like:
"jest": {
That's where you put your Jest configuration parameters. Just add the following properties to it:
"collectCoverage": true,
"coverageThreshold": {
"global": {
"lines": 95
}
}
You're going to want to change lines
property value to whatever percentage you want.
As a side note, I'm pretty sure this is default Jest configuration, but you also need to select the folders and files you want to run test coverage analysis on, since you already have a test hook you probably already have this, but just to make sure:
"collectCoverageFrom": [
"./src/**/*.(t|j)s"
],
This will run a coverage test on every file, .JS or .TS, inside /src directory.
Now whenever you commit something, or try to run npm run test
, it will also run test coverage and block commit if it's below the selected value.