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 runnpm init
to generate apackage.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 onpackage.json
- Run your test using
npm test
Download:
You can download the source code via GitHub. Downloaded source code can be run/executed with the following commands,
npm install
– Install packages dependenciesnpm start
– Start Server/Applicationnpm 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 !!
gotten several things , thanks.
Thank you 🙂
Hello, it’s a fantastic blog I ever have seen keep growing to keep sharing yours to us.
It’s really very very useful for me to complete my work. U r really damn man !
Great!!!!
Would you please write about testing the application without `server.inject` ?
Siva, curious, are your test targets mocked in isolation or are you describing an integration test? — thanks for any help.