DEV Community

Ankita Kumari
Ankita Kumari

Posted on

πŸš€ Building and Testing a Dockerized Book API with Keploy

Over the past week, I worked on an exciting project β€” building a RESTful Book API using Node.js, containerizing it with Docker, and automating test generation using Keploy. Here’s a complete walkthrough of what I built, learned, and achieved!

πŸ”§ Tech Stack

Layer Technology
Language JavaScript (Node.js)
Framework Express.js
Database PostgreSQL (via Docker)
DB Client pg (node-postgres)
Testing Jest, Supertest
Automation Keploy
Containerization Docker & Docker Compose

✨ Features
βœ… API Endpoints
πŸ‘₯ Users
GET /users – Get all users

POST /users – Create a user

PUT /users/:id – Update a user

DELETE /users/:id – Delete a user

πŸ“š Books
GET /api/books – Get all books

POST /api/books – Add a book

GET /api/books/:id – Get a book by ID

DELETE /api/books/:id – Delete a book

πŸ§ͺ Testing Strategy
My goal was to achieve 100% test coverage with the following layers:

βœ… Unit Tests – Test individual components

βœ… Integration Tests – Test DB operations

βœ… API Tests – Using Supertest

βœ… Auto-generated Tests – via Keploy

🐰 How Keploy Helped
I integrated Keploy CLI inside my Docker Compose setup. It captured real HTTP requests and database interactions and auto-generated test cases in the background!

πŸ“¦ Setup Command
docker run -it --network="book-api-server_default" \
-v "$(pwd)":/keploy -w /keploy \
-e KEPLOY_MODE=record \
-e KEPLOY_APP_HOST=node-app:5000 \
-e KEPLOY_DB_DSN="postgres://postgres:#Anku265482@pgdb:5432/bookdb?sslmode=disable" \
ghcr.io/keploy/keploy
After hitting a few API routes with curl, Keploy generated test cases in keploy-tests/

🧠 What I Learned
Managing multi-container apps with Docker Compose

Connecting Node.js with PostgreSQL in Docker

Handling database connection errors inside Docker

Writing clean, layered API architecture

Using Keploy to record, replay, and test real API usage

Solving tricky Git submodule and push errors

πŸ“Έ Screenshots
βœ… Test Coverage Report

Image description

🐰 Keploy CLI Output

Image description

πŸ“‚ Generated Keploy Test Files

Image description

🐳 API Curl Request

Image description

πŸ”— Links
GitHub Repo: https://github.com/ankita-2311/book-api-server

πŸ’¬ Feedback? Let’s connect on LinkedIn

🏁 Final Words
Keploy simplified testing for me β€” instead of writing boilerplate test cases, I just interacted with my API and let Keploy generate them. Highly recommended for any project with REST APIs!

If you enjoyed reading or found this helpful, do share and tag @KeployIO. πŸ’™

Top comments (0)