Skip to content

Instantly share code, notes, and snippets.

@electricessence
Last active October 23, 2015 08:05
Show Gist options
  • Save electricessence/0a0076e86c436a2e5b03 to your computer and use it in GitHub Desktop.
Save electricessence/0a0076e86c436a2e5b03 to your computer and use it in GitHub Desktop.
Mocha Recusive Importer
// How to use:
// 1) Place this in your test root for mocha.
// 2) Change the 'root' value below to match the path.
// 3) DO NOT configure mocha with --recursive
var fs = require("fs");
var root = "./tests/mocha/";
function getFilesAt(path, ext) {
return fs
.readdirSync(path)
.filter(function (name) {
return (!ext || name.lastIndexOf(ext) == name.length - ext.length)
&& fs.statSync(path + '/' + name).isFile();
});
}
function getDirectoriesAt(path) {
return fs
.readdirSync(path)
.filter(function (name) { return fs.statSync(path + '/' + name).isDirectory(); });
}
function importRecursive(path, importFiles, base) {
if (path === void 0) { path = ""; }
if (importFiles === void 0) { importFiles = false; }
if (base === void 0) { base = ""; }
var dirPath = base + path;
if (importFiles)
console.log(dirPath);
getDirectoriesAt(root + dirPath)
.sort()
.forEach(function (dirname) {
describe(dirname, function () {
importRecursive(dirname, true, dirPath + '/');
});
});
if (importFiles) {
getFilesAt(root + dirPath, '.js')
.sort()
.forEach(function (filename) {
var filePath = dirPath + '/' + filename;
console.log(" ", filename);
describe(filename.replace(/\.js$/, ''), function () {
require('./' + filePath);
});
});
}
}
console.log("Importing Tests:");
importRecursive();
@electricessence
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment