简体   繁体   中英

Setting Target Code Coverage with Karma/Istanbul

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?

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.

使用检查选项作为@SeanH 的答案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM