Skip to content

Instantly share code, notes, and snippets.

@pghalliday
Created July 20, 2012 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pghalliday/3150484 to your computer and use it in GitHub Desktop.
Save pghalliday/3150484 to your computer and use it in GitHub Desktop.
module.exports = function(grunt) {
// Add our custom tasks.
// These include:
// test - Run unit tests with Mocha (overrides the nodeunit test task)
grunt.loadTasks('grunt/tasks');
// Project configuration.
grunt.initConfig({
lint: {
files: ['**/*.js']
},
test: {
files: ['**/*.test.js']
},
watch: {
files: ['**/*.js'],
tasks: 'lint test'
},
jshint: {
options: {
etc...
]
}
},
mocha: {
options: {
reporter: 'nyan'
},
}
});
// Default task.
grunt.registerTask('default', 'lint test');
};
module.exports = function(grunt) {
var Mocha = require('mocha');
grunt.registerMultiTask('test', 'Run unit tests with Mocha', function() {
// tell grunt this is an asynchronous task
var done = this.async();
// Clear all the files we can in the require cache in case we are run from watch.
// NB. This is required to ensure that all tests are run and that all the modules under
// test have been reloaded and are not in some kind of cached state
for (var key in require.cache) {
if (require.cache[key]) {
delete require.cache[key];
if (require.cache[key]) {
console.warn('Mocha grunt task: Could not delete from require cache:\n' + key);
}
} else {
console.warn('Mocha grunt task: Could not find key in require cache:\n' + key);
}
}
// load the options if they are specified
var options = grunt.config(['mocha', this.target, 'options']);
if (typeof options !== 'object') {
options = grunt.config('mocha.options');
}
// create a mocha instance with our options
var mocha = new Mocha(options);
// add files to mocha
grunt.file.expandFiles(this.file.src).forEach(function(file) {
mocha.addFile(file);
});
// run mocha asynchronously and catch errors!! (again, in case we are running this task in watch)
try {
mocha.run(function(failureCount) {
console.log('Mocha completed with ' + failureCount + ' failing tests');
done(failureCount === 0);
});
} catch (e) {
console.log('Mocha exploded!');
console.log(e);
done(false);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment