PhantomJS LCOV reporter for Mocha
JavaScript
Switch branches/tags
Nothing to show
Latest commit 29e568b Oct 17, 2014 Mathieu Desvé Merge pull request #1 from wlabranche/patch-1
Updated README.md, fixed spelling error

README.md

mocha-phantom-lcov-reporter

Build Status Dependency Status Code Climate

NPM

PhantomJS LCOV reporter for Mocha.

This reporter combine three Mocha reporter: Spec, HTMLCov and Mocha Lcov Reporter. It work with NodeJS and in browser with PhantomJS.

The output lcov file is fully compatible with Coveralls

Usage

For prepare your sources files for coverage you can read the "Usage" section of Mocha Lcov Reporter or "Mocha + JSCoverage" and "Istanbul" section of Coveralls node helper. If you are using CoffeeScript, I recommend CoffeeCoverage.

For NodeJS

mocha -R mocha-phantom-lcov-reporter

In browser

# Gruntfile.coffee
grunt.loadNpmTasks('grunt-mocha')

grunt.initConfig
	mocha:
			all: 
				options:
					mocha:
						ignoreLeaks: false

					urls: ['http://localhost:<%= connect.test.options.port %>/']
					run: false
					reporter: 'mocha-phantom-coverage-reporter'
					timeout: 5000

And you must change the Phantom Bridge for pass coverage var at end of test

if (ev == 'end' && window._$jscoverage) {
        var cov = {};
        for(var prop in window._$jscoverage) {
                var file = window._$jscoverage[prop];
                file[0] = file.source;
                cov[prop] = file;
        }
        data.cov = cov;
}

sendMessage('mocha.' + ev, data);

See this example in generator-footguard bridge line 32

This exemple is issue to generator-footguard a project generator for Yeoman. You can see the result on test-footguard

Report

In your console you can see the spec reporter of Mocha and two files are created in coverage directory: coverage.lcov and coverage.html.

The file "coverage.html", it's the result of Mocha HTMLCov reporter. It's very helpful for debug coverage in local.

The file "coverage.lcov", it's file compatible with LCOV and Coveralls

Link to Coveralls

For link your project with Coveralls, start to add node-coveralls dependency in your project.

$ npm install coveralls --save-dev

For send coverage result to Coveralls

cat ./coverage/coverage.lcov | ./node_modules/coveralls/bin/coveralls.js

Coveralls parse all files in lcov file for add source, maybe your sources are not linked correctly you can specify the folder like this:

cat ./coverage/coverage.lcov | ./node_modules/coveralls/bin/coveralls.js src

Tips

If you work with Grunt and CoffeeCcript, I recommend you grunt-coffeecov.

# Gruntfile.coffee
grunt.loadNpmTasks('grunt-mocha')
grunt.loadNpmTasks('grunt-coffeecov')
grunt.loadNpmTasks('grunt-contrib-coffee')

grunt.initConfig
  connect:
    test:
      options:
        port: 3000
        hostname: '0.0.0.0'

  coffee:
    dist:
      expand: true
      cwd: 'src/coffee/'
      src: ['**/*.coffee']
      dest: '.tmp/js'
      ext: '.js'

  coffeecov:
    options:
      path: 'relative'
    dist:
      src: 'src/coffee/app'
      dest: '.tmp/js/app'

  mocha:
    all: 
      options:
        mocha:
          ignoreLeaks: false

        urls: ['http://localhost:3000/']
        run: false
        reporter: 'mocha-phantom-coverage-reporter'
        timeout: 10000

grunt.registerTask('test', [
  'coffee:dist'
  'coffeecov:dist'
  'connect:test'
  'mocha'
])