Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
module.exports = function(grunt) {
"use strict";
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-este-watch");
var testFailed = [];
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
esteWatch: {
options: {
dirs: [ "src/**/" ],
},
js: function(filepath) {
if (/_test\.js$/.test(filepath)) {
return "test:" + filepath;
}
grunt.config(["jshint", "files"], filepath);
return [ "jshint", "test:" + filepath ];
},
jshint: {
files: "src/*.js",
options: grunt.file.readJSON(".jshintrc")
}
}
});
grunt.registerTask("test", function() {
Object.keys(require.cache).forEach(function(filepath) {
if (!/\/node_modules\//.test(filepath)) {
delete require.cache[filepath];
}
});
var Mocha = require("mocha");
var mocha = new Mocha();
var reporter = "dot";
var args = arguments[0];
var files = [];
if (args) {
if (args === "travis") {
reporter = "list";
} else {
if (grunt.file.exists(args)) {
files.push(args);
}
var related = args.replace(/\.js$/, "_test.js");
if (grunt.file.exists(related)) {
files.push(related);
}
}
}
if (!files.length) {
files = grunt.file.expand("src/*_test.js");
}
files = files.concat(testFailed);
var set = {};
files = files.filter(function(file) {
if (!set[file] && /_test\.js$/.test(file)) {
set[file] = true;
return true;
}
return false;
});
files.forEach(function(file) {
mocha.addFile(file);
});
var done = this.async();
mocha.reporter(reporter).run(function(failures) {
if (failures) {
grunt.fail.fatal("test failed.");
testFailed = files;
} else {
testFailed = [];
}
if (args === "travis") {
files.forEach(function(file) {
var code = grunt.file.read(file);
if (/^\s*(describe|it)\.only\("/m.test(code)) {
grunt.fail.warn("test succeeded, but not completely.");
}
});
}
done();
});
});
grunt.registerTask("default", "esteWatch");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment