hapi — Run Tests with Assertions using Code

Testing your hapi applications might require expected values and responses that match a set of data. The code library from hapi’s ecosystem is an assertions library based on chai.

Code exposes an expressive and readable language-style interface. You’ll get a grasp on features and the usage in the upcoming sections.

hapi Series Overview

Dependency Double Check: code

Within the previous tutorial on getting started with testing hapi application using lab, you installed and added the code dependency to your project. Double check that code is part of your devDependencies in the package.json file:

…
  "devDependencies": {
    "code": "~4.1.0",
    "lab": "~14.3.1"
  },
…

If code isn’t installed yet, go a head and do that now. Install code and save the package as a development dependency:

# Running Node.js v8+
npm i -D code

# Running Node.js v6-
npm i -D code@4  

Notice that the latest release of code is compatible with Node.js v8+. If you’re on Node.js v4 or v6, use code in version 4.

Code — an Assertion Library

Code is an assertions library that makes use of connecting words for readable assertions. Assertions in testing are a way to express result expectations.

Let’s illustrate the theory with a practical example: you’re developing a user sign up feature and want to test whether your route handler works properly for an email address that is already registered. You want the route handler to return an error with a proper status code and error message, telling the requesting user that there’s an existing account with the given data.

Your test(s) can should expect an error with an appropriate HTTP response status code. Code is a library that helps you to define these assertions.

Update package.json To Run Your Tests with Lab

Again, if you followed the previous tutorial getting started with testing hapi application using lab, you’ve already set up your app to use code as an assertion library.

Double check that the test command within the scripts block of your package.json contains the --assert code statement:

{
  "name": "futureflix",
  "version": "1.1.0",
  …
  "scripts": {
    "test": "lab --assert code --leaks --coverage"
  }
}

Your test command can have more flags. The important part for this tutorial is the --assert code flag. Lab uses code by default, but that’s not generally obvious. Make it clear for everyone on your team that you’re using code for assertions.

Expect Values in Your Tests

Let’s create assertions in tests of your project. We‘re using the Futureflix Starter Kit to illustrate the functionality. Use your own project to create assertions in your tests.

Assertions are part of your tests and you integrate them with the lab setup around experiments and tests. In the Futureflix starter kit, we create a new test file called 02-assertions.js within the test/getting-started directory. This will be the playground to illustrate different kinds of assertions.

02-assertions.js

'use strict'

const Lab = require('lab')  
const Code = require('code')

// Test files must require the lab module, and export a test script
const lab = (exports.lab = Lab.script())

// shortcuts from lab
const experiment = lab.experiment  
const test = lab.test

experiment('hapi testing with lab and assertions with code,', () => {  
  test('assert that 1 + 2 equals three', () => {})
})

Up to this part, the newly created test includes a basic lab test skeleton with a yet unused import of code. This will change in a second.

Your First Assertion

The grammar of code consists of connecting words to make assertions readable. When running Code.expect(), each supported word returns this and allows you to chain multiple methods and properties. Connecting words are a, an, and, at, be, have, in, to.

Now it’s your turn: create a basic assertion where code expects the sum of 1 + 2 to equal 3. Start your snippet with Code.expect(<variable>) to set the expected value.

02-assertions.js

'use strict'

const Lab = require('lab')  
const Code = require('code')

// Test files must require the lab module, and export a test script
const lab = (exports.lab = Lab.script())

// shortcuts from lab
const experiment = lab.experiment  
const test = lab.test

experiment('hapi testing with lab and assertions with code,', () => {  
  test('assert that 1 + 2 equals three', () => {
    Code.expect(1 + 2).to.equal(3)
    Code.expect('3').to.equal('3')
    Code.expect(3).to.not.equal('3')
  })
})

Your assertion should look like this: Code.expect(1 + 2).to.equal(3). The code snippet contains a second assertion where the string '3' should equal another string '3'.

This is for illustration purposes to show you that code supports different data types and you can compare strings the same way as math calculations.

For an assertion of 3 should equal '3', an integer 3 should equal a string 3, the assertion makes sure that both are different.

More Complex Assertions

Every statement chained to Code.expect() returns this and is chainable as well. Compose more complex statements than simple comparisons. The following code block shows you assertions on the data type and values.

test('assert different data types', () => {  
  expect(true).to.be.a.boolean()
  expect(true)
    .to.be.a.boolean()
    .and.to.not.equal(false)

  expect('this is a string').to.startsWith('this')
})

The two boolean assertions outline the opportunities that code gives you for comparisons and assertions.

There’s a third statement that make sure a given string startsWith another string.

Code exposes a rich API and you can combine most of the methods and properties.

Outlook

All examples in this tutorial are static assignments, comparisons and assertions. For your actual tests, you’ll use responses from a route handler or dynamically loaded data. We’ll dig into this in the next tutorial. Stay hungry!

We’re happy to hear your thoughts and appreciate your comment. Please don’t hesitate to use the comment form below. If Twitter is more your thing, find us @futurestud_io. Let us know what you think!

Enjoy coding & make it rock!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.