我试图为我的角度SPA进行一些单元测试。我用Karma制作了一些,并且它们工作得很好,但后来我决定安装karma-coverage来查看测试覆盖率。我已经浪费了很多时间试图看到这份报告。 我安装了karma-coverage,现在我的package.json看起来像:
"dependencies": {
"angular-mocks": "^1.5.6",
"jasmine-core": "^2.4.1",
"karma": "^0.13.22",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.0.0",
"karma-firefox-launcher": "^1.0.0",
"karma-jasmine": "^1.0.2",
"karma-ng-html2js-preprocessor": "^1.0.0"
}
注意:我已经尝试过不同的业力和业力覆盖,但结果是一样的。 我的karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'bower_components/angular/angular.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'node_modules/angular-mocks/angular-mocks.js',
'**/*.module.js',
'**/**/*.module.js',
'*!(.module|.spec).js',
'!(bower_components|node_modules)/**/*!(.module|.spec).js',
'**/**/*.spec.js',
'**/*.html'
],
exclude: [
],
preprocessors: {
'**/*.js': ['coverage'],
'**/*.html': ['ng-html2js']
},
coverageReporter:{
type:'html',
dir:'coverage/'
},
reporters: ['progress', 'coverage'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
};
注意:我已尝试过,不同的预处理器设置,不同的coverageReporter设置。
但结果总是一样的:我试图打开http://localhost:9876/coverage/或http://localhost:9876/coverage/index.html并找不到404。我甚至尝试在C:/ Dev / coverage /上更改dir属性,但目录尚未创建。 Karma测试运行正确,但我无法获得覆盖率报告。
答案 0 :(得分:0)
经过几个小时我修好了。实际上并不知道它有效,但似乎我应该设置真正的autoWatch。这是我的karma.conf.js
//jshint strict: false
module.exports = function(config) {
config.set({
basePath: './',
files: [
'bower_components/angular/angular.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'node_modules/angular-mocks/angular-mocks.js',
'bower_components/jquery/dist/jquery.min.js',
'**/*.module.js',
'**/**/*.module.js',
'*!(.module|.spec).js',
'!(bower_components|node_modules)/**/*!(.module|.spec).js',
'**/**/*.spec.js',
'**/*.html'
],
autoWatch: true,
frameworks: ['jasmine'],
browsers: ['Chrome'],
plugins: [
'karma-chrome-launcher',
'karma-jasmine',
'karma-coverage',
'karma-ng-html2js-preprocessor'
],
reporters: ['progress', 'coverage'],
preprocessors: {
'**/*.html': ['ng-html2js'],
'!(bower_components|node_modules)/**/!(*spec).js': ['coverage']
},
coverageReporter: {
type : 'html',
dir : 'coverage/'
}
});
};