Skip to content

Instantly share code, notes, and snippets.

@johncoder
Created February 10, 2012 03:08
Show Gist options
  • Save johncoder/1785953 to your computer and use it in GitHub Desktop.
Save johncoder/1785953 to your computer and use it in GitHub Desktop.
Running All Test Files in Mocha
var fs = require("fs"),
path = require("path"),
cp = require("child_process");
var conventionalTestDirectories = [".\\test", ".\\tests"],
conventionChain = function() {};
function checkForDirectoryExists(directoryPath, nextDirectory, executeTests) {
console.log("Checking for tests in " + directoryPath);
path.exists(directoryPath, function(exists) {
if (exists) {
executeTests(directoryPath);
} else if (!nextDirectory) {
console.log("Could not find any tests to run.");
} else {
nextDirectory();
}
});
}
function executeAllTestsInDirectory(directoryPath) {
var command = "";
fs.readdir(directoryPath, function(err, files) {
for(var i = 0; i < files.length; i++) {
files[i] = directoryPath + "\\" + files[i];
}
command = "node node_modules\\mocha\\bin\\mocha " + files.join(" ");
console.log("found " + files.length + " files in " + directoryPath);
console.log("running command: " + command);
cp.exec(command, function (error, stdout, stderr) {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
});
}
for(var i = 0; i < conventionalTestDirectories.length; i++) {
conventionChain = (function(testDirectory, previousDirectory, i) {
return function() { checkForDirectoryExists(testDirectory, previousDirectory, executeAllTestsInDirectory); };
})(conventionalTestDirectories[i], conventionChain, i);
}
conventionChain();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment