One useful API test
An API test proves that a route works from request to response. It should send a request, let your route handler run, and check what the client gets back.
For your HackIndia repo, one good API test is better than five shallow checks. Pick a route that matters to your project, like login, create order, fetch notes, or submit feedback.
What to check
Check three things. First, the status code. Second, the response body. Third, the important side effect, if there is one.
For example, a UPI split app may test POST /expenses. The test should confirm 201, the returned expense amount, and that the expense was saved.
Set up the route
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Express example
This example uses Node.js, Express, Jest, and Supertest. The route is small, but the pattern works for bigger apps too.
Notice that app.js exports the app. It does not call listen. That makes the test fast and avoids port conflicts on your laptop or in CI.
// app.js
const express = require('express');
const app = express();
app.use(express.json());
const expenses = [];
app.post('/expenses', (req, res) => {
const { title, amount } = req.body;
if (!title || typeof amount !== 'number') {
return res.status(400).json({ error: 'title and amount are required' });
}
const expense = { id: expenses.length + 1, title, amount };
expenses.push(expense);
return res.status(201).json(expense);
});
module.exports = app;
// server.js
const app = require('./app');
app.listen(3000, () => console.log('Server running on port 3000'));
// app.test.js
const request = require('supertest');
const app = require('./app');
test('creates an expense', async () => {
const response = await request(app)
.post('/expenses')
.send({ title: 'chai for team', amount: 120 });
expect(response.status).toBe(201);
expect(response.body).toMatchObject({
id: 1,
title: 'chai for team',
amount: 120
});
});
test('rejects an invalid expense', async () => {
const response = await request(app)
.post('/expenses')
.send({ title: 'chai for team' });
expect(response.status).toBe(400);
expect(response.body.error).toBe('title and amount are required');
});Use AI carefully
You can ask ChatGPT or Gemini to draft API tests from your route file. Paste the route and ask for tests using your actual test runner.
Then check the output. It should import your app correctly, use real endpoint paths, await async calls, and assert status plus body. Do not accept tests that only check that a function was called.
Avoid weak tests
A weak API test sends a request and only checks 200. That misses broken response data, missing validation, and wrong error messages.
Also avoid testing against your running local server, like http://localhost:3000, unless you really need it. It is slower and can fail if the port is busy in your hostel Wi-Fi setup or CI.
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done