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
π° Keploy CLI Output
π Generated Keploy Test Files
π³ API Curl Request
π 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)