Skip to content

Instantly share code, notes, and snippets.

@jalcine
Last active August 30, 2015 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalcine/2c386d240aab312fa202 to your computer and use it in GitHub Desktop.
Save jalcine/2c386d240aab312fa202 to your computer and use it in GitHub Desktop.
'use strict'
const initializerNames = ['config', 'db', 'auth']
const Hapi = require('hapi')
let server = new Hapi.Server()
function applyInitializerToServer (initializerName) {
const functor = require('./server/' + initializerName + '.js')
const modifiedServer = functor(server)
server.log('Setting up initializer for ' + initializerName + '...')
if (modifiedServer === undefined) {
throw new ReferenceError('Server malformed.')
} else {
server = modifiedServer
}
}
module.exports = function implementServer () {
server = new Hapi.Server()
initializerNames.forEach(applyInitializerToServer)
return server
}
'use strict'
const testHelper = require('./helper.js')
const lab = exports.lab = testHelper.Lab.script()
lab.experiment('server', function () {
const assert = testHelper.Chai.assert
const Proxyquire = testHelper.Proxyquire
const serverPath = testHelper.rootPath + '/app/server.js'
lab.test('returns object if configured properly', function (done) {
const mockServer = require(serverPath)
assert.doesNotThrow(mockServer, 'server can be set up')
assert.isObject(mockServer(), 'server was obtained as object')
done()
})
lab.test('returns undefined if configured improperly', function (done) {
const mockedModule = {
'./server/db.js': function (server) { return undefined }
}
const mockServer = Proxyquire(serverPath, mockedModule)
assert.throws(mockServer,
ReferenceError, 'malformed',
'throws error about malformed server')
done()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment