Karma,Angular和Husky的代码覆盖执行

时间:2017-04-25 09:34:24

标签: angular karma-runner code-coverage angular-cli istanbul

目前我正在尝试通过使用thresholdscoverageIstanbulReporter对象的angular-cli选项来建立代码覆盖率。我正在使用git hook来运行ng test --single-run --code-coverage。遗憾的是threshold的{​​{1}}值未受到尊重。

我指的是:Code Coverage Enforcement

这是我的80

karma.conf.js

=========最终解决方案=========
最后的module.exports = function (config) { config.set({ basePath: '', frameworks: ['jasmine', '@angular/cli'], plugins: [ require('karma-jasmine'), require('karma-chrome-launcher'), require('karma-firefox-launcher'), require('karma-jasmine-html-reporter'), require('karma-coverage-istanbul-reporter'), require('karma-spec-reporter'), require('@angular/cli/plugins/karma') ], client:{ clearContext: false // leave Jasmine Spec Runner output visible in browser }, files: [ { pattern: './src/test.ts', watched: false } ], preprocessors: { './src/test.ts': ['@angular/cli'] }, mime: { 'text/x-typescript': ['ts', 'tsx'] }, coverageIstanbulReporter: { reports: ['html', 'lcovonly', 'text-summary'], fixWebpackSourcePaths: true, thresholds: { statements: 80, lines: 80, branches: 80, functions: 80 } }, angularCli: { environment: 'dev' }, reporters: config.angularCli && config.angularCli.codeCoverage ? ['spec', 'coverage-istanbul'] : ['spec', 'kjhtml'], port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, browsers: ['Chrome'], singleRun: false }); };

package.json

最终{ "name": "ng-fun", "version": "0.0.1", "license": "", "scripts": { "precommit": "ng lint", "prepush": "ng test --code-coverage --single-run", "clean": "rimraf node_modules dist coverage", "clean:dist": "rimraf dist", "clean:install": "npm run clean && npm install", "ng": "ng", "start": "ng serve", "build:dev": "ng build", "build:prod": "ng build -prod", "test": "ng test", "test:coverage": "ng test --code-coverage", "test:sr:coverage": "ng test --single-run --code-coverage", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/animations": "4.0.3", "@angular/common": "4.0.3", "@angular/compiler": "4.0.3", "@angular/core": "4.0.3", "@angular/forms": "4.0.3", "@angular/http": "4.0.3", "@angular/platform-browser": "4.0.3", "@angular/platform-browser-dynamic": "4.0.3", "@angular/router": "4.0.3", "@ngrx/core": "1.2.0", "@ngrx/router-store": "1.2.6", "@ngrx/store": "2.2.2", "@ngrx/store-devtools": "3.2.4", "core-js": "2.4.1", "immutable": "3.8.1", "reset-css": "2.2.0", "rxjs": "5.3.0", "web-animations-js": "2.2.5", "zone.js": "0.8.4" }, "devDependencies": { "@angular/cli": "1.0.1", "@angular/compiler-cli": "4.0.3", "@types/jasmine": "2.5.38", "@types/node": "6.0.60", "codelyzer": "2.0.0", "husky": "0.13.3", "jasmine-core": "2.5.2", "jasmine-spec-reporter": "3.2.0", "karma": "1.4.1", "karma-chrome-launcher": "2.0.0", "karma-cli": "1.0.1", "karma-coverage-istanbul-reporter": "1.2.0", "karma-firefox-launcher": "1.0.1", "karma-jasmine": "1.1.0", "karma-jasmine-html-reporter": "0.2.2", "karma-spec-reporter": "0.0.31", "protractor": "5.1.0", "rimraf": "2.6.1", "ts-node": "2.0.0", "tslint": "4.5.0", "typescript": "2.2.2" } }

karma.conf.js

1 个答案:

答案 0 :(得分:3)

根据这个:https://github.com/mattlewis92/karma-coverage-istanbul-reporter/blob/master/CHANGELOG.md。在0.3.0版本中引入了阈值,我相信angular-cli v1.0.0会将0.2.0版本添加到你的包中.json

所以: 1.确保package.json中有{0.0}或更高版本的karma-coverage-istanbul-reporter 2.使用threshold而不是thresholds

此外,请注意,如果您更新到版本> 1.2.0。 threshold更新了两个对象globaleach,如下所述:https://github.com/mattlewis92/karma-coverage-istanbul-reporter/blob/master/README.md