angular/cli eject项目之后,测试无法生成coverage 报告问题

姚钊
2023-12-01

一个angular项目,被我通过eject之后需要补充单元测试。执行karma start ./karma.conf.js --single-run --coverage

之后,生成的coverage报告的icov.info为空。解决办法是在karma.conf.js中增加以下配置

angularCli: {

environment: 'dev',

codeCoverage: true

},

问题得到解决。

相关配置如下

1、package.json

{
    "name": "",
    "version": "0.0.0",
    "license": "MIT",
    "scripts": {
        "ng": "ng",
        "start": "webpack-dev-server --port=4200 --open --env=dev",
        "build": "webpack -p --env=prod",
        "test": "karma start ./karma.conf.js --single-run  --coverage",
        "citest": "ng test --single-run  --code-coverage",
        "lint": "ng lint",
        "e2e": "protractor ./protractor.conf.js",
        "pree2e": "webdriver-manager update --standalone false --gecko false --quiet"
    },
    "private": true,
    "dependencies": {
        "@angular/animations": "^5.2.0",
        "@angular/common": "^5.2.0",
        "@angular/compiler": "^5.2.0",
        "@angular/core": "^5.2.0",
        "@angular/forms": "^5.2.0",
        "@angular/http": "^5.2.0",
        "@angular/platform-browser": "^5.2.0",
        "@angular/platform-browser-dynamic": "^5.2.0",
        "@angular/router": "^5.2.0",
        "classlist.js": "^1.1.20150312",
        "clean-webpack-plugin": "^0.1.19",
        "core-js": "^2.4.1",
        "less": "^2.7.3",
        "moment": "^2.18.1",
        "ng-zorro-antd": "^0.7.1",
        "rxjs": "^5.5.6",
        "zone.js": "^0.8.19"
    },
    "devDependencies": {
        "@angular/cli": "1.7.4",
        "@angular/compiler-cli": "^5.2.0",
        "@angular/language-service": "^5.2.0",
        "@types/jasmine": "~2.8.3",
        "@types/jasminewd2": "~2.0.2",
        "@types/node": "~6.0.60",
        "codelyzer": "^4.0.1",
        "jasmine-core": "~2.8.0",
        "jasmine-spec-reporter": "~4.2.1",
        "karma": "~2.0.0",
        "karma-chrome-launcher": "~2.2.0",
        "karma-coverage-istanbul-reporter": "^1.2.1",
        "karma-jasmine": "~1.1.0",
        "karma-jasmine-html-reporter": "^0.2.2",
        "protractor": "~5.1.2",
        "ts-node": "~4.1.0",
        "tslint": "~5.9.1",
        "typescript": "~2.5.3",
        "webpack-dev-server": "~2.11.0",
        "webpack": "~3.11.0",
        "autoprefixer": "^7.2.3",
        "cssnano": "^3.10.0",
        "file-loader": "^1.1.5",
        "html-webpack-plugin": "^2.29.0",
        "less-loader": "^4.0.5",
        "postcss-import": "^11.0.0",
        "postcss-loader": "^2.0.10",
        "postcss-url": "^7.1.2",
        "raw-loader": "^0.5.1",
        "sass-loader": "^6.0.6",
        "source-map-loader": "^0.2.0",
        "istanbul-instrumenter-loader": "^3.0.0",
        "style-loader": "^0.19.1",
        "stylus-loader": "^3.0.1",
        "url-loader": "^0.6.2",
        "circular-dependency-plugin": "^4.2.1",
        "copy-webpack-plugin": "~4.4.1",
        "uglifyjs-webpack-plugin": "^1.1.8",
        "@angular-devkit/core": "0.3.2",
        "@ngtools/webpack": "1.10.2"
    }
}

2、karma.conf.js

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular/cli'],
        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-jasmine-html-reporter'),
            require('karma-coverage-istanbul-reporter'),
            require('@angular/cli/plugins/karma')
        ],
        client: {
            clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
            reports: ['html', 'lcovonly'],
            fixWebpackSourcePaths: true
        },
        angularCli: {
            environment: 'dev',
            codeCoverage: true
        },
        reporters: ['progress', 'kjhtml', 'coverage-istanbul'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false
    });
};

 

 类似资料: