How test your Hapi API with Lab

Marcos Bérgamo
3 min readDec 7, 2015
There’s no way back when you became a mad scientist

Brief

Started a mad API but, when the next step is wanted I got a problem. How is the better approach to testing my API? Mocha? Jasmine? Wows?

In Hapi ecosystem we have Lab a powerful and clean engine for you test you api made easy. For this propose we'll focused in e2e(end to end) tests for our MAD API.

Proposes Mad API

Just for this example proposes, imagine a simple API with just one endpoint called TODO.

The TODO endpoint, have the methods LIST, GET, CREATE, UPDATE and DESTROY just another REST API with a CRUD(Create, Read, Update, Delete).

Tools

We will just use Lab and Chai. Chai is an Assertion library. We've some other tools like: Should, Code and the standard Assertion library in Node. But I'm more close with Chai and this library help me a lot because I can use promises in some cases and other cool stuffs in Chai just installing a Chai plugin.

Code is good and are in Hapi organization in Github. Feel free to choose the assertion library you're more confident.

Inside your Laboratory

I like creating an simple index.spec.js in my tests root directory for bootstrapping your dependencies and waiting when database is connected.

--

--