Created
January 24, 2013 17:52
-
-
Save ninjascience/4625729 to your computer and use it in GitHub Desktop.
Gruntfile.js for testing with Jasmine and RequireJS and using Istanbul for coverage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
'use strict'; | |
// Project configuration. | |
grunt.initConfig({ | |
meta : { | |
// Specify where our test files are. Once we get all the tests switched over we can use 'test/js/**/*.spec.js' to automatically load all tests. | |
specs : ['test/js/src/**/*.spec.js'], | |
bin : { | |
coverage: 'js/bin/coverage' | |
} | |
}, | |
// Our jasmine task, with a sub-task called 'test'. | |
jasmine : { | |
options : { | |
specs : '<%= meta.specs %>', | |
template: require('grunt-template-jasmine-requirejs'), | |
templateOptions: { | |
requireConfig: { | |
baseUrl: 'js/src/' | |
} | |
} | |
}, | |
test : { | |
// Run with the default settings | |
}, | |
coverage : { | |
options: { | |
templateOptions: { | |
requireConfig: { | |
baseUrl: '<%= meta.bin.coverage %>/js/src/' | |
} | |
}, | |
helpers: 'js/coverage_helper.js' | |
} | |
} | |
}, | |
// Instrument the files for coverage | |
instrument : { | |
files : 'js/src/**/*.js', | |
options : { | |
basePath : '<%= meta.bin.coverage %>' | |
} | |
}, | |
storeCoverage : { | |
options : { | |
dir : '<%= meta.bin.coverage %>' | |
} | |
}, | |
makeReport : { | |
src : '<%= meta.bin.coverage %>/*.json', | |
options : { | |
type : 'lcov', | |
dir : '<%= meta.bin.coverage %>' | |
} | |
}, | |
// Setup a simple web server that can run our tests headlessly. | |
connect: { | |
options: { | |
base: '.', | |
port: 9911 | |
}, | |
test: { | |
options: { | |
port: 9911, | |
keepalive: false | |
} | |
}, | |
runner: { | |
options: { | |
port: 9912, | |
keepalive: false | |
} | |
} | |
} | |
}); | |
grunt.loadNpmTasks('grunt-istanbul'); | |
grunt.loadNpmTasks('grunt-contrib-jasmine'); | |
grunt.loadNpmTasks('grunt-contrib-connect'); | |
grunt.loadNpmTasks('grunt-contrib-watch'); | |
/** Grunt Tasks **/ | |
// Instrument JS and run tests to get a coverage report | |
grunt.registerTask('coverage', ['instrument', 'jasmine:coverage', | |
'storeCoverage', 'makeReport']); | |
// Run tests headlessly and report the results in the command line. | |
grunt.registerTask('test', ['connect:test', 'jasmine:test']); | |
// Start a simple web server and build the spec runner. | |
// You can hit the spec runner in a browser after running this at 'http://localhost:9912/_SpecRunner.html | |
grunt.registerTask('runner', ['jasmine:test:build', 'connect:runner', 'watch:tests']); | |
// Default task. | |
grunt.registerTask('default', ['test']); | |
// needed to make grunt-istanbul storeReport | |
grunt.event.on('jasmine.coverage', function (coverage) { | |
global.__coverage__ = coverage; | |
}); | |
}; |
I haven't been able to find much on the combination of Grunt/Jasmine/Require/Istanbul. This gist has gotten me a long way. Thanks!
I'm silently failing on storeCoverage, even using --verbose. @vorakumar, if you found a solution to your problem, could you post it here? Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the sample Gruntfile!
I am using this file for a sample Jasmine test with RequireJS. And I am able to run tests as part of coverage task however 'storeCoverage' task results into error: 'No coverage information was collected'. I pretty much have same setup as you do. Any suggestions on how to get coverage working?
Thanks again.