5

我在使用 Angular.js + Jasmine 运行伊斯坦布尔代码覆盖工具时遇到了一些困难。我在 Coffeescript 中编码,但由于 Instanbul 还不支持它,所以每次保存时源都会转换为 JS。

基本上,我在这里看不到测试和测试代码之间的关系,因为根本没有单元测试的文件仍然获得 66% 的覆盖率,这,嗯……根本没有意义。

正如我在标题中提到的,我使用 Karma 作为测试运行器,但命令行会产生相同的结果。

示例 Angular.js 控制器(编译的 .coffee):

'use strict';
angular.module('app.controllers').controller('HelpIndexCtrl', [
  '$scope', function($scope) {
    return $scope.foo = 'bar';
  }
]);

和单元测试:

'use strict'
describe "controllers", ->
  beforeEach angular.mock.module "app.controllers"
  scope = rootScope = {}
  describe "HelpIndexCtrl", -> inject ($controller)->
    ctrl = $controller 'HelpIndexCtrl', $scope:scope
    it 'should have working scope', ->
      expect(scope.foo).toBe 'bar'

一般覆盖结果

示例控制器

示例控制器

4

1 回答 1

3

这是在我的案例中完美运行并为多个大中型项目提供支持的解决方案(我使用的是 karma@0.9.4):

事实证明,使用grunt转换.coffee文件然后将.js文件传递​​给 karma 覆盖处理器对我来说要方便得多:

业力配置

  module.exports = function (karma) {
    karma.set({
      basePath: '../',
      frameworks: ['jasmine'],
      files: [

        // -------- START: IMPORTS ----------


        "vendor/angular-ui-utils/modules/ie-shiv/ie-shiv.js",
        "vendor/jquery/jquery.js",
        "vendor/es5-shim/es5-shim.js",
        "vendor/lodash/lodash.js",
        "vendor/angular/angular.js",

        // and so on for the next 80 lines...



        // -------- END: IMPORTS ----------


        'vendor/angular-mocks/angular-mocks.js',
        "vendor/sinonjs/sinon.js",
        'vendor/angular-*/angular-*.js',



        'public/js/templates.js',

        'test/js/**/*.js',


        //////////////////
        // Custom Mocks //
        //////////////////
        'test/js-unit/**/*.mock.js',

        //////////////////
        // CoffeeScript //
        //////////////////
        'test/js-unit/**/*.spec.js'
      ],
      reporters: ['progress', 'coverage', 'junit'],

      plugins: [
        'karma-jasmine',
        'karma-script-launcher',
        'karma-phantomjs-launcher',
        'karma-junit-reporter',
        'karma-coverage',
        'karma-coffee-preprocessor',
        'karma-growl-reporter'
      ],


      junitReporter: {
        outputFile: 'test-results.xml'
      },

      // web server port
      // CLI --port 3334
      port: 3334,

      // cli runner port
      // CLI --runner-port 9100
      runnerPort: 9100,

      // enable / disable colors in the output (reporters and logs)
      // CLI --colors --no-colors
      colors      : true,
      logLevel    : karma.LOG_DISABLE,
      autoWatch   : true,
      loggers     : [],
      browsers    : ['PhantomJS'],

      // If browser does not capture in given timeout [ms], kill it
      // CLI --capture-timeout 5000
      captureTimeout: 5000,

      // Auto run tests on start (when browsers are captured) and exit
      // CLI --single-run --no-single-run
      singleRun: true,

      // report which specs are slower than 500ms
      // CLI --report-slower-than 500
      reportSlowerThan: 500,

      coverageReporter : {
        type: 'html',
        dir: 'test/coverage/'
      },

      preprocessors: {
        'test/js/**/*.js': 'coverage'
      }
    });

  }

GruntFile.json 片段:

coffee:
  compile:
    files:
      'public/js/app.js' : ['app/**/*.coffee']
    options:
      sourceMap: yes
      join: yes
      bare: yes
  compileForTests:
    options:
      bare: yes
    expand: yes
    flatten: no
    cwd: 'app/'
    src: ['**/*.coffee']
    dest: 'test/js/'
    ext: '.js'
  compileTests:

重要的

请注意,后续的 karma 次要版本需要不同的配置设置。此配置不适用于 karma@0.9.3。但是,配置结构的差异主要是美学上的(例如,将配置方法重构为set等)。

于 2013-09-17T16:01:42.590 回答