我有一个关于Istanbul Reporter用于在我的angular 6应用程序中报告我的单元测试范围的问题。
我的问题是:呈现coverage时,我在“测试文件”列表中看到了模拟,显然该模拟未经过测试,这给了我错误的coverage统计信息。
这是我的一位同事设置的karma.conf文件,我想知道您是否对如何排除这些模拟文件有任何想法。
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},
angularCli: {
environment: 'local'
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
我在StackOverflow上看到,可以通过在tsconfig.spec.json中添加排除项来完成此操作,但是即使重新运行代码覆盖率,它仍然会包含它们。
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/spec",
"module": "commonjs",
"types": [
"jasmine",
"node"
],
"typeRoots": [ "../node_modules/@types" ]
},
"files": [
"src/test.ts",
"src/polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
],
"exclude": [
"/**/*mock*.ts"
]
}
我的模拟文件位于每个模块/功能的 tests / mocks文件夹中,并称为“ mock-whatevertheymock.ts”
运行它的命令是
test:wc-dogs": "ng test --project=wc-dogs--code-coverage
感谢您的帮助
答案 0 :(得分:6)
谢谢大家,解决方案是在angular.json中添加codeCoverageExclude
选项
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "projects/wc-claims/src/test.ts",
"polyfills": "projects/wc-claims/src/polyfills.ts",
"tsConfig": "projects/wc-claims/tsconfig.spec.json",
"karmaConfig": "projects/wc-claims/karma.conf.js",
"styles": [
"projects/wc-claims/src/styles.css"
],
"scripts": [],
"assets": [
"projects/wc-claims/src/favicon.ico",
"projects/wc-claims/src/assets"
],
"codeCoverageExclude": [
"/**/*mock*.ts"
]
}
},
答案 1 :(得分:0)
您应该在业力配置中添加排除。
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},
angularCli: {
environment: 'local'
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
exclude: ["/**/*mock*.ts"],
singleRun: false
});
};