Mocha reporter to generate coverage report of istanbul instrumented code, for grunt This doesn't force you to use PhantomJS, or instrument code for server or client-side.
- Install it using
npm install grunt-mocha-istanbul --save-dev
- It needs
mocha
andgrunt
to be installed locally on your project (aka, having them in your devDependencies) - Call inside Gruntfile.js
grunt.loadNpmTasks('grunt-mocha-istanbul')
mocha_istanbul_check
was removed and became part of the options under thecheck
object
Most of the options that you pass to mocha is available in options
:
module.exports = function(grunt){
grunt.initConfig({
mocha_istanbul: {
coverage: {
src: 'test', // the folder, not the files,
options: {
mask: '*.spec.js'
}
},
coveralls: {
src: 'test', // the folder, not the files
options: {
coverage:true,
check: {
lines: 75,
statements: 75
},
root: './lib', // define where the cover task should consider the root of libraries that are covered by tests
reportFormats: ['','lcovonly']
}
}
}
});
grunt.event.on('coverage', function(lcovFileContents, done){
// Check below
done();
});
grunt.loadNpmTasks('grunt-mocha-istanbul');
grunt.registerTask('coveralls', ['mocha_istanbul:coveralls']);
grunt.registerTask('coverage', ['mocha_istanbul:coverage']);
};
If there's a mocha.opts
file inside the src
, it will warn if you are overwritting any options.
Coverage is written to coverage
folder by default, in the same level as the Gruntfile.js
The check
will fail the build if the thresholds are not met. It's a great possibility for CI-builds.
Mochas parameters, check [http://visionmedia.github.io/mocha/#usage]
Setting this to true makes the task emit a grunt event coverage
, that will contain the lcov data from
the file, containing the following callback function(lcovcontent, done)
, and you must manually call
done()
when you are finished, else the grunt task will hang. See more information below
Spits out the command line that would be called, just to make sure everything is alright
The mask for the tests to be ran. By default, mocha will execute the test
folder and all test files
Suppresses the output from Mocha and Istanbul
Name of the output of the coverage folder
Name of report formats. You can specify more than one. If you intend to use the coverage
option to
true
or do any checks, you must add: ['yourformat','lcovonly']
, since it's needed for the lcov.info
file to be created.
html - produces a bunch of HTML files with annotated source code
lcovonly - produces an lcov.info file
lcov - produces html + lcov files. This is the default format
cobertura - produces a cobertura-coverage.xml file for easy Hudson integration
text-summary - produces a compact text summary of coverage, typically to console
text - produces a detailed text table with coverage for all files
teamcity - produces service messages to report code coverage to TeamCity
The root path to look for files to instrument, defaults to .
. Can help to exclude directories that are not
part of the code whose coverage should be checked.
Number of statements threshold to consider the coverage valid
Number of lines threshold to consider the coverage valid
Number of branches threshold to consider the coverage valid
Number of functions threshold to consider the coverage valid
When you set the option coverage
to true
, you'll receive the coverage/lcov.info
file contents:
grunt.event.on('coverage', function(lcov, done){
console.log(lcov);
done(); // or done(false); in case of error
});
This is mainly useful so you can send it to, for example, coveralls (using coveralls):
grunt.event.on('coverage', function(lcov, done){
require('coveralls').handleInput(lcov, function(err){
if (err) {
return done(err);
}
done();
});
});
This way, Travis-CI can send the Istanbul generated LCOV directly to Coveralls.io website in this example, but you could create any transform for Jenkins, TeamCity, Hudson, etc.