Last active
August 29, 2015 14:01
-
-
Save ben-bradley/12d094cb75fc31086a7e to your computer and use it in GitHub Desktop.
wrap mocha in child_process.spawn() to grab JSON output
This file contains 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
/* 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