DEV Community

Daniel Ioni
Daniel Ioni

Posted on

๐Ÿงช Testing the MyZubsterGateway API โ€“ A Step-by-Step Guide

๐Ÿงช 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ› ๏ธ 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

  1. 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
});

  1. 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;

  1. 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.
Enter fullscreen mode Exit fullscreen mode

๐Ÿ™ 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.
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”— Resources

GitHub Repository: DanielIoni-creator/MyZubsterGateway

Jest Documentation: https://jestjs.io/

Supertest Documentation: https://github.com/visionmedia/supertest
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฌ 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
Enter fullscreen mode Exit fullscreen mode

Built with โค๏ธ for the MyZubster community.

๐Ÿš€ Testing isn't just about finding bugs โ€“ it's about building confidence. ๐ŸŽ‰

Top comments (0)