Skip to content
 
 

Latest commit

 

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Testing your Hapi server - Build inject method

What

Build a tool called inject, which makes requests to your server.

Why

We want to be able to test our server is routing and responding to requests appropriately. Hapi.js has its own inject method, which is called upon the server (it uses the shot module behind the scenes). You'll be creating something similar.

How

In server.js you'll see a hapi server with some basic routes.

In server.test.js there are some passing tests using hapi's server.inject. These are for inspiration and should indirectly inform you on what your inject module should do.

You can use the node core modules to help. We will be using Tape as our testing framework.

  • git clone this repo

  • npm install

  • Run your server with npm start

  • Execute your tests (in another tab) with npm test

Interface

At its most basic level inject takes a callback which will be run once all the data has come back from the server. That data can be accessed by response.payload

Eg:

tape('check route', t => {
  inject(response => {
    t.equal(response.payload, 'It Works!!',  'it does work')
    t.end()
  })
})

Further Instructions

Think about building upon your inject method so that it can take a url and method:

  • inject(options, cb) -> as in full url

'Register' the module with your server so that it knows where to listen, now we only need to pass it a path

  • var inject = require('inject')(server)

Play around and write more detailed tests for the routes & handlers

Extras

  • Deploy to npm for use in future projects.
  • Have a play with the hapi server.inject method to see what it's capable of.
  • Build a 'test runner', you'll see a partly filled file test-runner.js. Run your tests through your test runner with npm run testRunner

About

No description, website, or topics provided.

Resources

Stars

5 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages