DEV Community

Cover image for Increase coverage on every feature with Karma/Jest and Husky
Ismael Ramos ๐Ÿš€
Ismael Ramos ๐Ÿš€

Posted on โ€ข Edited on โ€ข Originally published at ismaelramos.dev

5 1 3 2

Increase coverage on every feature with Karma/Jest and Husky

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,
        },
      },
    },
    ...
};
Enter fullscreen mode Exit fullscreen mode

Or if you are using Jest:

// jest.config.js
{
...
"jest": {
  "coverageThreshold": {
    "global": {
      "branches": 50,
        "functions": 50,
        "lines": 50,
        "statements": 50
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

๐Ÿ‘ฅ Ideal for solo developers, teams, and cross-company projects

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay