Skip to content

Instantly share code, notes, and snippets.

@jalcine jalcine/app-server.js
Last active Aug 30, 2015

Embed
What would you like to do?
'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
You can’t perform that action at this time.