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