Testing Hapi.js With Jest

Jest is a testing framework developed by Facebook, and its often used to test React applications. However, it isn’t limited to just React it also support testing many web frameworks. In this article we will see how to test Hapi.js with Jest. Hapi.js rich framework for building applications and services hapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure.

Creating Web Server With Routes Using Hapi.js

  • Create a new directory YOUR_PROJECT_NAME and run npm init to generate a package.json file for our project.
  • Install Hapi.js package. Using the command,
    npm i @hapi/hapi
  • Now, Create a web server using Hapi.js with some GET & POST routes. Below is the code snippet for our application,
  • Run your web server using node server.js

Creating and Running Test Cases Using Jest

  • Install Jestjs package for development. Using the command,
    npm i --save-dev jest
  • Now, Create test cases for each routes with specified input and expected output. Below is the test cases for our application,
  • After creating test case, Add jest --verbose as your test script on package.json
  • Run your test using npm test


You can download the source code via GitHub. Downloaded source code can be run/executed with the following commands,

  • npm install – Install packages dependencies
  • npm start – Start Server/Application
  • npm test – Test Application

Please leave your valuable comments/suggestions/feedback on below comment box if any.
You can also contact me if your facing any problem/issues with the source code.
Thank You !!


SIVA SANKAR, Working as a Software Engineer, Blogging is my hobby. I completed my Bachelors of Engineering (Computer Science Engineering) in Chennai, India. and my Master of Engineering (Embedded System Technologies) in Tamilnadu, India.

6 thoughts on “Testing Hapi.js With Jest

  1. Siva, curious, are your test targets mocked in isolation or are you describing an integration test? — thanks for any help.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.