post logo icon of a cv file download
avatar logo

Increase coverage on every feature with Karma/Jest and Husky

Testing Coverage Git Husky

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:

1 - 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) {
    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.

2 - 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
. "$(dirname "$0")/_/"

[ -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.

Thanks for reading, and I hope you’ve learnt something new!

More recent posts here!