1

我希望能够使用html-cov记者,即使测试通过了,所有的覆盖率值都是 0。

我使用 Grunt,Istanbul 来创建检测代码和 mocha 进行测试,这是我的 grunt 文件

module.exports = (grunt) ->

  release = (type = 'develop') ->
    switch type
      when 'master'
        grunt.task.run('release')
      else
        grunt.config.set('release',
          options:
            tag     : false
            pushTags: false
            npm     : false
        )

        grunt.task.run('release')

    return

  grunt.initConfig

    pkg: grunt.file.readJSON 'package.json'

    covervars:
      base   : 'tests/lib-cov'
      build  : '<%=covervars.base %>/build'
      reports: '<%=covervars.base %>/reports'

    watch:
      coffee:
        files: ['./src/**/*.coffee', './tests/**/*.spec.coffee']
        tasks: ['build']

    coffee:
      compile:
        options:
          bare: true
        expand : true
        cwd    : 'src'
        src    : '**/*.coffee'
        dest   : 'lib'
        ext    : '.js'

    clean:
      coverage: [
        '<%=covervars.base %>/*'
      ]

    instrument:
      files  : './lib/**/*.js'
      options:
        basePath: '<%=covervars.build %>/'

    reloadTasks:
      rootPath: '<%=covervars.build %>/lib/'

    storeCoverage:
      options:
        dir: '<%=covervars.reports %>/'

    makeReport:
      src    : '<%=covervars.reports %>/**/*.json'
      options:
        type : 'lcov',
        dir  : '<%=covervars.reports %>',
        print: 'detail'

    cafemocha:
      test:
        src    : './tests/server/**/*.spec.coffee'
        options:
          require    : ['./tests/common.coffee','./lib/']
          ignoreLeaks: false
          checkLeaks : true
          colors     : true
          ui         : 'bdd',
          reporter   : 'dot'

      coverage:
        src    : './tests/server/**/*.spec.coffee'
        options:
          require    : ['./tests/common.coffee','./<%=covervars.build %>/lib/']
          globals    : ['__coverage__']
          ignoreLeaks: false
          colors     : true
          ui         : 'bdd',
          reporter   : 'spec'
          coverage   :
            output: 'tests/lib-cov/coverage.html'

    karma:
      options   :
        configFile: 'tests/client/karma.conf.js'
      unit      :
        browsers : ['Firefox']
        singleRun: true
      continuous:
        browsers : ['Firefox', 'Chrome']
        singleRun: false

  grunt.loadNpmTasks 'grunt-release'
  grunt.loadNpmTasks 'grunt-karma'
  grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-cafe-mocha'
  grunt.loadNpmTasks 'grunt-contrib-clean'
  grunt.loadNpmTasks 'grunt-istanbul'

  grunt.registerTask 'test', ['cafemocha:test']
  grunt.registerTask 'cover', [
    'coffee', 'clean',
    'instrument',
    #'reloadTasks',
    'cafemocha:coverage',
    #'storeCoverage', 'makeReport'
  ]
  grunt.registerTask 'test:continuous', ['karma:continuous']

  grunt.registerTask 'build', ['coffee', 'test']
  grunt.registerTask 'releaseit', release

  grunt.registerTask 'default', ['watch']

检测代码如下所示: http: //pastebin.com/3wAt5VZT

istanbul lcov.info 文件的一部分(已正确生成)

TN:
SF:./lib/index.js
FNF:0
FNH:0
DA:1,1
DA:2,1
DA:10,1
DA:12,1
DA:14,1
DA:16,1
DA:18,1
DA:20,1
DA:22,1
DA:24,1
LF:10
LH:10
BRF:0
BRH:0
end_of_record

mocha 似乎没有获得测试覆盖率,但如果我启用storeCoverageand makeReport,instabul 会向我显示正确的文件数和每个文件中使用的代码百分比。mocha 只能使用jscoverage的覆盖范围吗?如果没有,我是否缺少任何步骤?

4

2 回答 2

4

我创建了自己的 grunt 插件来实现这一点。它执行 mocha 并通过 stdin 即时将其传递给 istanbul。https://github.com/pocesar/grunt-mocha-istanbul可以通过 npm 安装npm install grunt-mocha-istanbul

它有一个调用的 gruntjs 事件coverage,使您能够使用从检测代码生成的 lcov.info

于 2014-05-19T10:36:30.603 回答
1

我使用BlanketJS而不是Istanbul让这个工作。Blanket 无需为源文件创建单独的检测副本,而是实时评估覆盖范围。

为了让 Blanket 与 Mocha 和 Grunt 一起工作,我最终编写了自己的 Grunt 插件。该插件支持“强制执行”最小阈值,否则 Grunt 任务将失败。

我在这里写了一篇包含所有细节的博客文章:http: //www.geekdave.com/2013/08/02/automated-code-coverage-enforcement-for-mocha-using-grunt-and-blanket/

于 2013-08-03T22:34:44.750 回答