A Mocha test runner for RequireJS

It took me a while to set up our ccnmtldjango template to work with Mocha. The time mostly stemmed from our using RequireJS, and my desire to run the tests with node.js, so the code needed to run on both the web browser and node.

Node already has a module loading technique (require()), but I still want to load our code with requirejs, since that's how the browser will do. It was essential for me to read requirejs's documentation on running it in node here.

Because I needed to set up requirejs in a manual way, I then needed to call mocha programmatically rather than from the command line (with mocha). Mocha fortunately had some documentation on how to do this here.

var requirejs = require('requirejs');
requirejs.config({
    paths: {
        'jquery': '../lib/jquery-1.11.3.min',
        'domReady': '../lib/require/domReady',
        'underscore': '../lib/underscore-min'
    },
    nodeRequire: require
});

var Mocha = require('mocha');
var fs = require('fs');
var path = require('path');

var basePath = './media/js/tests';
var mocha = new Mocha();

fs.readdirSync(basePath).filter(function(file) {
    return file !== __filename && file.substr(-3) === '.js';
}).forEach(function(file) {
    mocha.addFile(path.join(basePath, file));
});

mocha.run(function(failures) {
    process.on('exit', function() {
        process.exit(failures);
    });
});
  
<ref>