Setting Target Code Coverage with Karma/Istanbul

1.9k views Asked by At

I'm running the Karma Maven plugin with Karma-Coverage(Istanbul) for code coverage. Karma-Coverage seems to default at 80% for required (suggested?) code coverage, however I want to enforce 90% code coverage in the project. However, I can't figure out where to set this.

Is there a way to enforce a target code coverage with Karma/Istanbul?

2

There are 2 answers

1
MarcoL On

Use the check option as @SeanH's answer.

0
SeanH On

This is documented in the docs file in the repo. Look for the section on the "check" property.

This will be used to configure minimum threshold enforcement for coverage results. If the thresholds are not met, karma will return failure. Thresholds, when specified as a positive number are taken to be the minimum percentage required. When a threshold is specified as a negative number it represents the maximum number of uncovered entities allowed.

For example, statements: 90 implies minimum statement coverage is 90%. statements: -10 implies that no more than 10 uncovered statements are allowed.

global applies to all files together and each on a per-file basis. A list of files or patterns can be excluded from enforcement via the exclude property. On a per-file or pattern basis, per-file thresholds can be overridden via the overrides property.

coverageReporter: {
  check: {
    global: {
      statements: 50,
      branches: 50,
      functions: 50,
      lines: 50,
      excludes: [
        'foo/bar/**/*.js'
      ]
    },
    each: {
      statements: 50,
      branches: 50,
      functions: 50,
      lines: 50,
      excludes: [
        'other/directory/**/*.js'
      ],
      overrides: {
        'baz/component/**/*.js': {
          statements: 98
        }
      }
    }
  }
}

If you look in the code referenced by @MarcoCI, you'll see karma is doing a fairly standard options merge, which means you don't have to recreate the whole check object. Just the values you care about. Defaults are 0.