Question

I want to be able to use the html-cov reporter, even though the tests are passing, all the coverage values are 0.

I'm using Grunt, Istanbul for creating the instrumented code and mocha for the tests, this is my grunt file

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']

The instrumented code looks like this: http://pastebin.com/3wAt5VZT

a part of the istanbul lcov.info file (that is correctly generated)

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 doesn't seem to be picking up the test coverage, but if I enable storeCoverage and makeReport, instabul is showing me the correct number of files and the % of code used in each file. is mocha only able to use coverage from jscoverage? if not, am I missing any steps?

Was it helpful?

Solution

I created my own grunt plugin that does exactly this. It executes mocha and pass it to istanbul on-the-fly through stdin. https://github.com/pocesar/grunt-mocha-istanbul and can be installed through npm npm install grunt-mocha-istanbul

It has a gruntjs event called coverage that makes you able to consume the generated lcov.info from instrumented code

OTHER TIPS

I got this to work using BlanketJS instead of Istanbul. Instead of creating separate instrumented copies of your source files, Blanket evaluates the coverage live, on-the-fly.

To get Blanket working with Mocha and Grunt, I wound up writing my own Grunt plugin. The plugin supports "enforcement" of a minimum threshold, or else the Grunt task fails.

I wrote a blog post with all the details here: http://www.geekdave.com/2013/08/02/automated-code-coverage-enforcement-for-mocha-using-grunt-and-blanket/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top