π§ͺ 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)