AngularJS + Karma + Jasmine + Karma-Coverage:空白覆盖率报告

时间:2014-04-21 21:40:38

标签: angularjs unit-testing code-coverage karma-runner karma-jasmine

我正在尝试将我当前的AngularJS项目与Karma Coverage集成。请在下面找到 package.json 和我的 karma-config

的package.json

{
  "name": "Project",
  "description": "Description",
  "repository": "https://www.repo.com",
  "devDependencies": {
    "karma": "~0.10.9",
    "karma-junit-reporter": "~0.2.1",
    "karma-jasmine": "~0.1.5",
    "karma-ng-scenario": "~0.1",
    "karma-script-launcher": "~0.1.0",
    "karma-chrome-launcher": "~0.1.3",
    "karma-firefox-launcher": "~0.1.3",
    "karma-phantomjs-launcher": "~0.1.4",
    "karma-ng-html2js-preprocessor": "~0.1",
    "karma-coverage": "~0.1"
  }
}

Karma config

'use strict';

module.exports = function (config) {
  config.set({
    basePath: '../../public/',
    logLevel: config.LOG_DEBUG,
    frameworks: ['jasmine'],
    singleRun: true,
    files: [
        'libs/jquery/jquery-1.9.0.js',
        'libs/angular/1.2.10/angular.js',
        'libs/angular/**/*.js',
        'libs/angular/*.js',
        'libs/vendor/*.js',
        'libs/test/**/*.js',

        // fixtures
        {pattern: 'test/mock-data/helloworld/*.json', watched: true, served: true, included: false},

        'apps/helloworld/**/*.js',
        'apps/helloworld/*.js',
        'test/helloworld/unit/**/*.js',
        'test/helloworld/*.js',
        'views/helloworld/directives/*.html'
    ],

    exclude: [
        'libs/angular/1.2.10/*.min.js',
        'libs/angular/angular-animate.js'
    ],

    browsers: ['PhantomJS'],

    reporters: ['progress', 'junit', 'coverage'],

    preprocessor: {
        'apps/helloworld/**/*.js': ['coverage'],
        '*.html': ['ng-html2js']
    }
  })
};

当我尝试运行“node_modules / .bin / karma start conf / advisor / karma.conf.js”时,所有测试都成功运行,但是karma-coverage报告完全为空。我尝试了几种选择,但我不能让报告出现。

enter image description here

控制台输出永远不会运行coverage的预处理器。我可以看到它实际上是为html2js运行的。

DEBUG [plugin]: Loading karma-* from /Users/alansouza/workspace/helloworld/node_modules
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-chrome-launcher.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-coffee-preprocessor.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-    coverage.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-firefox-launcher.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-html2js-preprocessor.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-jasmine.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-junit-reporter.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-ng-html2js-preprocessor.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-ng-scenario.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-phantomjs-launcher.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-requirejs.
DEBUG [plugin]: Loading plugin /Users/alansouza/workspace/helloworld/node_modules/karma-script-launcher.
INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
DEBUG [launcher]: Creating temp dir at /var/folders/8_/vw105h0j3vn66cgzttktdjmm0000gn/T/karma-29140367
DEBUG [launcher]: /Users/alansouza/workspace/helloworld/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/bin/phantomjs  /var/folders/8_/vw105h0j3vn66cgzttktdjmm0000gn/T/karma-29140367/capture.js
DEBUG [watcher]: Excluded file  "/Users/alansouza/workspace/helloworld/public/libs/angular/1.2.10/angular-animate.min.js"
DEBUG [watcher]: Excluded file "/Users/alansouza/workspace/helloworld/public/libs/angular/1.2.10/angular-resource.min.js"
DEBUG [watcher]: Excluded file "/Users/alansouza/workspace/helloworld/public/libs/angular/1.2.10/angular-route.min.js"
DEBUG [watcher]: Excluded file "/Users/alansouza/workspace/helloworld/public/libs/angular/1.2.10/angular-sanitize.min.js"
DEBUG [watcher]: Excluded file "/Users/alansouza/workspace/helloworld/public/libs/angular/1.2.10/angular.min.js"
DEBUG [preprocessor.html2js]: Processing "/Users/alansouza/workspace/helloworld/public/views/advisor/directives/av-product-total.html".
DEBUG [preprocessor.html2js]: Processing "/Users/alansouza/workspace/helloworld/public/views/advisor/directives/av-product.html".
DEBUG [preprocessor.html2js]: Processing "/Users/alansouza/workspace/helloworld/public/views/advisor/directives/av-select-product.html".
DEBUG [watcher]: Resolved files:

问题:我在这里做错了吗?如何使业力覆盖加载我的src js文件?

2 个答案:

答案 0 :(得分:4)

您缺少preprocessors属性和coverageReporter属性。

将此部分添加到您的karma配置文件中:

preprocessors: {
      'apps/helloworld/**/*.js':['coverage']
   },
coverageReporter:{
      type:'html',
      dir:'C:/Dev/coverage/'
   },

您可以将coverageReporter输出目录更改为您想要的任何内容。

答案 1 :(得分:3)

我能够解决问题。这是一个失踪的“s”。我的业力配置最初是预处理器,其中应该是预处理器。请在下面找到正确的业力配置:

'use strict';

  module.exports = function (config) {
  config.set({
    basePath: '../../public/',
    logLevel: config.LOG_DEBUG,
    frameworks: ['jasmine'],
    singleRun: true,
    files: [
        'libs/jquery/jquery-1.9.0.js',
        'libs/angular/1.2.10/angular.js',
        'libs/angular/**/*.js',
        'libs/angular/*.js',
        'libs/vendor/*.js',
        'libs/test/**/*.js',

        // fixtures
        {pattern: 'test/mock-data/helloworld/*.json', watched: true, served: true, included: false},

        'apps/helloworld/**/*.js',
        'apps/helloworld/*.js',
        'test/helloworld/unit/**/*.js',
        'test/helloworld/*.js',
        'views/helloworld/directives/*.html'
    ],

    exclude: [
        'libs/angular/1.2.10/*.min.js',
        'libs/angular/angular-animate.js'
    ],

    browsers: ['PhantomJS'],

    reporters: ['progress', 'junit', 'coverage'],

    preprocessors: {
        'apps/helloworld/**/*.js': ['coverage'],
        '*.html': ['ng-html2js']
    }
    })
  };

这里最有趣的一点是:它之前用于ng-html2js的预处理器(我的指令测试正在通过)。当我介绍报道时,问题就开始发生了。