I've answered this on Locomotive's github for an issue, however I wanted to share here to get some feedback, as in better ways, cleaner, or any other input you might have.
I require multiple servers in an OAuth2 environment (auth, resource, dashboard..) of which mostly are Locomotive framework based.
I like functional tests since they best represent OAuth2 based authentication as opposed to only focusing my tests on a resource service where the authentication token might not best represent the user.
Here is the setup I've come up with for starting up the locomotive servers:
For your tests, say like in test/util.severs.js
var fork = require('child_process').fork,
async = require("async");
var authApp;
var serverStatus = [];
var start = function(done) {
authApp = fork( __dirname + '/../../authorization/server.js');
serverStatus.push(function(callback){
authApp.on('message', function(m){
//console.log('[AUTHORIZATION] SENT MESSAGE: ', m);
if (m.status == 'listening') return callback();
});
});
// add others servers if you swing that way!
async.parallel(serverStatus, function(){
done();
});
}
var stop = function(done) {
authApp.kill();
done();
}
module.exports = {
start: start,
stop: stop
};
Note that I'm using async here since I'm working with multiple servers (Environment is in OAuth2 which requires many servers to startup (IE resource, dashboard...)), ignore async if all you'll ever have is one server.
require the above file in your Mocha tests and do
servers = require('./util/servers');
...
before(servers.start);
// tests away!!!
after(servers.stop);
Then in each of my locomotive-project/server.js I do the following...
// if being started as a parent process, send() won't exist, simply define
// it as console.log or what ever other thing you do with logging stuff.
if (process.send === undefined){
process.send = function(msg){
console.log("status: ", msg.status);
};
}
process.send({status: 'starting'});
...
app.boot(function(err) {
if (err) {
process.send({status: 'error', message: err});
return process.exit(-1);
} else {
// notify any parents (fork()) that we are ready to start processing requests
process.send({status: 'listening'});
}
});
Using Locomotive 0.4.x here, could be different based on your version.
Is this the best way to go? Ideally I would like to move this over to Grunt, that is test server initialization (which is quite hefty as we build lots of data into the test DB), and then functional tests could run. So wondering if anyone has delved into grunt to do this instead of a before() task in Mocha.