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
npm initto generate a
package.jsonfile 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
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 --verboseas your test script on
- Run your test using
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 !!