【问题标题】:Angular.js code coverage using Karma w. Coffeescript使用 Karma w 的 Angular.js 代码覆盖率。咖啡脚本
【发布时间】:2013-05-11 01:30:36
【问题描述】:

我在使用 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'

【问题讨论】:

  • 没有单元测试的文件上 66% 的语句覆盖率来自于 Karma 加载文件时执行的一些语句。控制器声明 angular.module().controller()'use strict' 语句都在加载过程中进行评估。
  • 感谢您的评论。我对此很清楚。关键是 - 为什么其余的代码,即。单元测试根本不被评估?我已经用 Karma + Istanbul 进行了单元测试,但结果是一样的。
  • 你的 karma 配置文件是什么样的?
  • @NoahFreitas 很抱歉这么晚才回复。它不起作用的原因是单元测试中的错误导致加载 Angular.js 模拟两次并在几个看似随机的情况下禁用注入方法。我还即时将所有 .coffee 文件转换为 js,并在 Karma 中禁用了 coffeescipt 预编译,以确保编译没有问题。我认为这是一件好事,因为您在单元测试和实际代码中使用相同的预编译设置。回到办公室后,我将使用工作 karma.conf.js 发布答案。
  • 很抱歉,刚刚发布了一个在我的情况下完美运行的解决方案的答案。尽管有延迟,但希望您会发现它很有用:)

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


【解决方案1】:

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

原来我用grunt转换.coffee文件然后传.js文件方便多了到业力覆盖处理器:

业力配置

  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 sn-p:

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。但是,配置结构的差异主要是美学上的(例如,将 config 方法重构为 set 等)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-24
    • 2013-06-15
    • 1970-01-01
    • 2011-09-03
    • 2018-02-13
    • 2015-06-09
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多