Created
January 20, 2016 03:34
-
-
Save devinivy/1e6e71abcb721fa8fee9 to your computer and use it in GitHub Desktop.
Testing an initialized hapi server
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
var Hapi = require('hapi'); | |
// Keep track of the server's init state | |
var internals = { | |
initialized: false | |
}; | |
var server = new Hapi.Server(); | |
server.connection(); | |
server.register([/* plugins */], function (err) { | |
if (err) { | |
// If there are any errors, report them | |
internals.initialized = err; | |
server.emit('app-initialized'); | |
return console.trace(err); | |
} | |
server.initialize(function (err) { | |
if (err) { | |
// If there are any errors, report them | |
internals.initialized = err; | |
server.emit('app-initialized'); | |
return console.trace(err); | |
} | |
// Init completed, so report that | |
internals.initialized = true; | |
server.emit('app-initialized'); | |
// If the server is not being require()'d, | |
// then continue to start the server | |
if (!module.parent) { | |
server.start(function (err) { | |
if (err) { | |
return console.trace(err); | |
} | |
console.log('Server started!!'); | |
}); | |
} | |
}); | |
}); | |
// This is what our tests will import | |
module.exports = function(cb) { | |
// If the server is already done with init, return it immediately | |
if (internals.initialized) { | |
return cb(internals.initialized || null, server); | |
} | |
// Otherwise, we'll just wait until it's reported to have some init state | |
server.on('app-initialized', function () { | |
cb(internals.initialized || null, server); | |
}); | |
}; |
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
var Lab = require('lab'); | |
var Code = require('code'); | |
var InitializedServer = require('./server') | |
// Test shortcuts | |
var lab = exports.lab = Lab.script(); | |
var expect = Code.expect; | |
var before = lab.before; | |
var experiment = lab.experiment; | |
var test = lab.test; | |
experiment('My server doing some stuff', function () { | |
var server; | |
before(function (done) { | |
// Guaranteed to return a server that is initialized or errored along the way | |
InitializedServer(function (err, server) { | |
server = server; | |
done(err); | |
}); | |
}); | |
/* Tests! */ | |
}); | |
experiment('My server doing some totally different stuff', function () { | |
var server; | |
before(function (done) { | |
// Guaranteed to return a server that is initialized or errored along the way | |
InitializedServer(function (err, server) { | |
server = server; | |
done(err); | |
}); | |
}); | |
/* Tests! */ | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment