mercredi 23 mars 2016

How to mock a server error in hapi

I have set up a hapi server as follows:

'use strict'
// node modules
const Hapi = require('hapi')
const Inert = require('inert')
const Path = require('path')
// server config
const server = new Hapi.Server()
const port = 4000

server.connection({
  port: port
})

// Hapi plugins
const plugins = [
  Inert
]

server.register(plugins, (err) => {
  if (err) {
    throw err
  }
  server.route([
    {
      method: 'GET',
      path: '/',
      handler: (request, reply) => {
        reply.file(Path.join(__dirname, '../front/production/index.html'))
      }
    },
    {
      method: 'GET',
      path: '/{param*}',
      handler: {
        directory: {
          path: 'front/production'
        }

      }
    }
  ])
})

module.exports = server

I have written tests for it, but have a problem being able to cover this line:

if (err) {
  throw err <----
}

how can I mock a server error in server.register, so far I have tried:

server.inject({method: 'GET', url: '/notanendpoint'}...
server.inject({method: 'NOTMETHOD', url: '/'}...
server.inject({method: 'GET', url: '/', simulate: {error: true}}...

I've been looking through the hapi api docs and cant work out how to cover this line, any help would be greatly appreciated.

Let me know if I can add more description to my question or include what I have tried in my tests (let me know if you want to see more of my test file)

Aucun commentaire:

Enregistrer un commentaire