Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
Last active August 29, 2015 14:01
Show Gist options
  • Save ben-bradley/12d094cb75fc31086a7e to your computer and use it in GitHub Desktop.
Save ben-bradley/12d094cb75fc31086a7e to your computer and use it in GitHub Desktop.
wrap mocha in child_process.spawn() to grab JSON output
/* npm insatll -g mocha
* npm install express
* node ./<this>
* put tests in ./test/ and they can be called by hitting http://localhost:3030/test/:file
* you can test all tests by hitting http://localhost:3030/test/all
*/
var express = require('express'),
app = express();
var spawn = require('child_process').spawn;
var express_mocha = function(req, res) {
var stdout, stderr, _this, opts = [ '-R', 'json' ];
if (req.params.file) opts.push('./test/'+req.params.file+'.js');
_this = spawn('mocha', opts);
_this.stdout.on('data', function(data) { stdout = (stdout||'') + data; });
_this.stderr.on('data', function(data) { stderr = (stderr||'') + data; });
_this.on('close', function() {
if (stdout) res.send(JSON.parse(stdout));
else res.send(stderr);
});
};
app.get('/test/all', express_mocha);
app.get('/test/:file', express_mocha);
app.listen(3030);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment