What if you could catch hidden bugs in your API before anyone else sees them?
Why Testing GET endpoints in Express? - Purpose & Use Cases
Imagine you build a web server with many GET routes to fetch data. You manually open a browser or use tools like curl every time you want to check if each route works correctly.
Manually testing each GET endpoint is slow, repetitive, and easy to forget. You might miss bugs or break something without realizing it. It's hard to keep track of what you tested and what you didn't.
Testing GET endpoints with automated tests lets you quickly and reliably check your routes. You write code that sends requests and checks responses, so you catch errors early and save time.
curl http://localhost:3000/users
// Check response manuallyrequest(app).get('/users').expect(200).expect('Content-Type', /json/)
Automated GET endpoint testing makes your server more reliable and your development faster by catching problems before users do.
When adding a new feature to your API, automated GET tests ensure old routes still work perfectly without you having to check each one by hand.
Manual testing is slow and error-prone.
Automated GET endpoint tests save time and catch bugs early.
Reliable tests help maintain and grow your API confidently.
Practice
Solution
Step 1: Understand GET endpoint testing
Testing GET endpoints means checking if the server responds correctly when a client asks for data.Step 2: Identify the goal of testing
The goal is to confirm the server sends the right data and proper HTTP status code (like 200 for success).Final Answer:
To verify the server returns the correct data and status code -> Option AQuick Check:
Testing GET endpoints = verify data and status [OK]
- Confusing GET with POST request testing
- Testing unrelated features like CSS or database speed
- Ignoring status codes in tests
Solution
Step 1: Recall supertest syntax
Supertest uses request(app).get('/path') to simulate a GET request to the Express app.Step 2: Check the correct chaining
Chaining .expect(200, done) checks the status code and signals test completion.Final Answer:
request(app).get('/path').expect(200, done); -> Option BQuick Check:
supertest GET syntax = request(app).get(path).expect(status) [OK]
- Calling app.get() instead of request(app).get()
- Missing the done callback in async tests
- Incorrect method chaining order
const express = require('express');
const app = express();
app.get('/hello', (req, res) => res.json({ message: 'Hi!' }));
const request = require('supertest');
test('GET /hello returns message', async () => {
const response = await request(app).get('/hello');
return response.body.message;
});Solution
Step 1: Analyze the endpoint response
The GET /hello endpoint sends JSON { message: 'Hi!' } as response.Step 2: Check the test return value
The test awaits the GET request and returns response.body.message, which is 'Hi!'.Final Answer:
"Hi!" -> Option AQuick Check:
GET /hello returns message = "Hi!" [OK]
- Expecting response.body instead of response.body.message
- Forgetting to await the request
- Assuming string 'hello' instead of 'Hi!'
const request = require('supertest');
const app = require('./app');
test('GET /users returns 200', () => {
request(app)
.get('/users')
.expect(200);
});Solution
Step 1: Check async test handling
The test function is async but does not return or await the request promise, so Jest won't wait for it.Step 2: Confirm correct fix
Adding return before request(...) or making the test async and awaiting fixes the issue.Final Answer:
Missing return or await to handle async test -> Option CQuick Check:
Async test needs return or await [OK]
- Not returning or awaiting async calls in tests
- Assuming path or method is wrong without checking
- Confusing import errors with test async errors
app.get('/active-users', (req, res) => {
const users = [
{ id: 1, active: true },
{ id: 2, active: false },
{ id: 3, active: true }
];
res.json(users.filter(u => u.active));
});
// Test code options:
A) await request(app).get('/active-users').expect(200).expect(res => {
if (!res.body.every(u => u.active)) throw new Error('Inactive user found');
});
B) await request(app).get('/active-users').expect(200).expect(res => {
if (res.body.length !== 3) throw new Error('Wrong number of users');
});
C) await request(app).get('/active-users').expect(404);
D) await request(app).get('/active-users').expect(200).expect(res => {
if (!res.body.some(u => u.active === false)) throw new Error('Inactive user found');
});Solution
Step 1: Understand endpoint filtering
The endpoint returns only users with active: true, so no inactive users should appear.Step 2: Analyze test options
Checks for inactive users but throws error if found uses every(u => u.active) and throws if any inactive, correctly verifying the filter.
Incorrectly expects all users, including inactive expects length !== 3 (wrong, it's 2).
Wrong status code expectation expects 404 (wrong).
Incorrectly throws error when no inactive users found incorrectly throws error when no inactive users found.Step 3: Verify A logic
!every(u => u.active) detects any inactive user, matching the requirement.Final Answer:
Checks for inactive users but throws error if found -> Option DQuick Check:
Test filters out inactive users = no inactive found [OK]
- Expecting all users instead of filtered active only
- Using wrong status code in test
- Confusing every() and some() logic in tests
Ready to prove it?
Pass the test and this pattern moves unknown
