2
votes

I want my jenkins build should break when code coverage does not meet defined threshold in karma.config.js. Karma should return non-zero error code to break continuous integration if code coverage does not meet with defined threshold. But in my case it exit with zero error code and thus causing non failure of tests if code coverage is less than threshold.

Versions

Angular CLI: 6.0.8

Angular: 6.1.9

Karma: 1.7.1

Code:

Below is the threshold I have set inside karma.config.js

coverageIstanbulReporter: {
  dir: require('path').join(__dirname, '../coverage'),
  reports: ['html', 'lcovonly'],
  fixWebpackSourcePaths: true,
  thresholds: {
    statements: 80,
    lines: 80,
    branches: 80,
    functions: 80
  }
}

I am executing the below command :

ng test --watch false --browsers PhantomJS --code-coverage

Karma is showing the below error but is returning 0 as error code:

ERROR [reporter.coverage-istanbul]: Coverage for branches (70.7%) does not meet global threshold (80%)

ERROR [reporter.coverage-istanbul]: Coverage for functions (79.05%) does not meet global threshold (80%)

Expected behavior

Karma to return non-zero error code if code coverage does not meet threshold to break continuous integration

Actual behavior

Karma spits out an error but exits like normal with zero as error code

While finding solution I came across this information :

"Commands like ng test, ng serve and ng build --watch that run in watch mode don't usually exit with an error. This is by design.

Non-watch commands like ng test --watch=false, ng build and ng e2e do exit with an error code though.

Also It can't seem to get both autowatch turned off and code coverage generated at the same time"

If anyone has any recommendations as to how I can fix this, or if anyone has any ideas about what I might be doing wrong please let me know.

1
any possibility to upgrade Karma? I seem to remember a similar problem with older versionsNormunds Kalnberzins
It wont be possible to upgrade karma now as there are other lot of dependencies which also might need to upgrade. Is there any solution which do not need Karma to be upgraded?Karan Khanna

1 Answers

0
votes

There was an issue on Github that addressed this exact issue and this issues already got closed by this PR.