2

I have a test that checks if a server's been configured correctly. The code for both the server and test are in the gist. However, running lab --environment TEST --verbose --coverage --reporter console --output stdout --reporter html --output coverage gives me this (HTML version provided):

snapshot361

I'd like to get 100% coverage for this file. For completeness, I've included the snippet code in-line:

app/server.js

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
}

test/server.js

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()
  })
})
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
jackyalcine
  • 469
  • 1
  • 8
  • 21
  • Please paste the code, not a picture of it. – Evan Carslake Aug 30 '15 at 00:47
  • @EvanCarslake: I've provided a link to it (it's the fourth word) but for completeness's sake, I'll paste said code snippets in here. Also that screenshot is of a HTML rendering of the page. – jackyalcine Aug 30 '15 at 00:50
  • ah, I didn't see it, my bad. – Evan Carslake Aug 30 '15 at 00:51
  • You using babel or a build step? I'm seeing some ES6 in there. – arb Sep 01 '15 at 13:55
  • @arb: Not directly; I've enabled harmony extensions for Node. Would the use of `const` and `let` affect the return value? – jackyalcine Sep 02 '15 at 02:44
  • 1
    I recall there are some using ES6 features currently with lab... Remember, the hapi universe is really only 100% tested and verified using standard Node 0.10.x. Not saying that's the issue here, but it might have something to do with it. Drop the ES6 features and the harmony flag and see what happens. – arb Sep 02 '15 at 14:05

0 Answers0