๐งช Testing the MyZubsterGateway API โ A Step-by-Step Guide
Testing is one of the most important parts of building reliable software. In this article, I'll walk you through how I added automated tests to the MyZubsterGateway API, and why this matters for the project.
๐ฏ What We Accomplished
We just completed a key task: adding automated tests for the booking endpoints of the MyZubsterGateway API. This was issue #5 in our GitHub repository.
๐ What You'll Learn
Why testing is important
How to write API tests with Jest and Supertest
How to handle authentication during testing
How to structure test files for a Node.js/Express API
๐ ๏ธ Technologies We Used
Technology Purpose
Jest Testing framework
Supertest HTTP assertions for testing APIs
Express Web framework for the API
Node.js Runtime environment
๐ What We Created
- A Test File for Bookings (tests/bookings.test.js)
This file contains tests for all booking-related endpoints:
javascript
const request = require('supertest');
const app = require('../server.test');
describe('Bookings API', () => {
// Tests for GET /api/bookings
// Tests for POST /api/bookings
// Tests for GET /api/bookings/:id
// Tests for PUT /api/bookings/:id
// Tests for DELETE /api/bookings/:id
});
- A Simplified Server for Testing (server.test.js)
Instead of loading the entire production server, we created a lightweight version that only includes the routes we need for testing:
javascript
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const bookingRoutes = require('./routes/bookings');
app.use('/api/bookings', bookingRoutes);
module.exports = app;
- Authentication Bypass for Tests
To avoid having to log in during tests, we modified the routes/bookings.js file to check for a special environment variable:
javascript
const auth = (req, res, next) => {
if (process.env.NODE_ENV === 'test') {
return next(); // Skip authentication during tests
}
// ...normal authentication logic
};
๐ Tests We Added
Test What It Verifies
GET /api/bookings Returns a 200 status and an array
POST /api/bookings Creates a new booking and returns 201
POST /api/bookings (invalid) Returns 400 if required fields are missing
GET /api/bookings/:id Returns the correct booking details
GET /api/bookings/:id (not found) Returns 404 if booking doesn't exist
PUT /api/bookings/:id Updates a booking and returns 200
PUT /api/bookings/:id (not found) Returns 404 if booking doesn't exist
DELETE /api/bookings/:id Deletes a booking and returns 204
DELETE /api/bookings/:id (not found) Returns 404 if booking doesn't exist
๐ How to Run the Tests
bash
Navigate to the project directory
cd ~/MyZubsterGateway
Run the tests with test mode enabled
NODE_ENV=test npm test -- tests/bookings.test.js
Expected output:
text
PASS tests/bookings.test.js
Bookings API
GET /api/bookings
โ should return 200 and an array of bookings (38 ms)
POST /api/bookings
โ should create a new booking and return 201 (29 ms)
โ should return 400 if required fields are missing (4 ms)
...
Test Suites: 1 passed, 1 total
Tests: 9 passed, 9 total
๐ง Why This Matters
Testing might seem like extra work, but it brings huge benefits:
Fewer bugs โ You catch problems before they reach production.
Faster development โ You can make changes confidently, knowing tests will catch regressions.
Better documentation โ Tests show exactly how the API is supposed to behave.
Easier collaboration โ New contributors can run tests to verify their changes.
๐ What's Next
With the booking tests in place, we can now:
Add tests for other endpoints โ Users, orders, payments, etc.
Set up CI/CD โ Run tests automatically on every pull request.
Improve test coverage โ Aim for 80%+ coverage.
๐ Resources
GitHub Repository: DanielIoni-creator/MyZubsterGateway
Jest Documentation: https://jestjs.io/
Supertest Documentation: https://github.com/visionmedia/supertest
๐ฌ Connect with Me
If you're interested in testing, Node.js, or the MyZubster project, let's connect!
๐ Dev.to: https://dev.to/danielioni
๐ฆ Twitter: https://x.com/myzubster
๐ผ LinkedIn: https://linkedin.com/in/daniel-ioni-62b2b9423/
๐ GitHub: https://github.com/DanielIoni-creator
Built with โค๏ธ for the MyZubster community.
๐ Testing isn't just about finding bugs โ it's about building confidence. ๐
Top comments (0)