0

我正在尝试为我的角度 SPA 进行一些单元测试。我用 Karma 做了一些,它们工作得很好,但后来我决定安装 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 测试运行正确,但我无法获得覆盖率报告。

4

1 回答 1

0

又过了几个小时,我修好了。实际上不知道它有效,但似乎我应该设置真正的自动手表。这是我的 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/'
        }

    });
};
于 2016-07-01T12:38:52.527 回答