Skip to content

Instantly share code, notes, and snippets.

@mohayonao
Last active December 25, 2015 14:19
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 mohayonao/6990165 to your computer and use it in GitHub Desktop.
Save mohayonao/6990165 to your computer and use it in GitHub Desktop.
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