Skip to content

Instantly share code, notes, and snippets.

@srlm-io
Created November 22, 2014 05:06
Show Gist options
  • Save srlm-io/7402d9643cc2ba896db8 to your computer and use it in GitHub Desktop.
Save srlm-io/7402d9643cc2ba896db8 to your computer and use it in GitHub Desktop.
Hapi.js and Lab asynchronous server testing
"use strict";
var Hapi = require('hapi');
var Joi = require('joi');
var database = require('./database');
exports.init = function (startListening, doneCallback) {
var server = Hapi.createServer(nconf.get('listenIp'), nconf.get('port'), {
cors: {
origin: [
nconf.get('htmlOrigin')
],
credentials: true
}
});
database.init(function (err) {
if (err) {
console.log('database error: ' + err);
process.exit(1);
}
server.pack.register([
/* plugins */
], function (err) {
if (err) {
doneCallback(err);
return;
}
/* Setup routes here */
if (startListening) {
server.start(function () {
console.log('Server running at:', server.info.uri);
doneCallback(null, server);
});
} else {
doneCallback(null, server);
}
});
});
};
// Start server and listen if we're the root module
if (!module.parent) {
exports.init(true, function (err) {
if (err) {
console.log(err);
}
});
}
"use strict";
var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var after = lab.after;
var expect = Code.expect;
var requirePath = '../../index';
var sut = require(requirePath);
describe('some tests', function () {
var server;
before(function (done) {
sut.init(false, function (err, server_) {
server = server_;
done();
});
});
it('a test', function (done) {
console.log('we can use server here: ' + server);
done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment