Table of content
Have you ever add new features and after some time, coverage seems to be decreasing, even if you are creating or updating some tests?
I'm going to explain how we can always be sure that coverage increase and not the opposite. It's a combination of two things:
Check your coverage percentage
First, we need to be sure that coverage does not decrease over a certain percentage.
This is an example for those using Karma:
// karma.conf.js
module.exports = function (config) {
config.set({
...
coverageReporter: {
...
check: {
global: {
statements: 42.7,
lines: 44.0,
branches: 41.4,
functions: 26.0,
},
},
},
...
};
Or if you are using Jest:
// jest.config.js
{
...
"jest": {
"coverageThreshold": {
"global": {
"branches": 50,
"functions": 50,
"lines": 50,
"statements": 50
}
}
}
This will provoke the command to fail (usually npm test or equivalent), if at least one of them does not reach the minimum required.
Execute tests before pushing
The second thing you have to do, is to run all your tests just before pushing changes to the repository.
This is just an idea, maybe you want to do it before you commit those changes, I leave it to you.
To do that, I'm going to use husky. You just need to install it, and then create your first hook like this:
// .husky/pre-push
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
[ -n "$CI" ] && exit 0
npm run test
You have an angular app where you can find this examples which is using karma and husky in this repo.
I hope you have learned something new. If you think this might help other people, please hit the like button so that others can read it. โค๏ธ
If you have any thoughts or questions, feel free to leave a comment!
Top comments (0)