0
0
Expressframework~10 mins

Testing GET endpoints in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Testing GET endpoints
Start Test
Send GET Request
Server Receives Request
Server Processes Request
Server Sends Response
Test Receives Response
Check Status & Body
Pass or Fail Test
End Test
The test sends a GET request to the server, waits for the response, then checks if the response status and body match expectations.
Execution Sample
Express
import request from 'supertest';
import app from './app';

test('GET /hello returns 200 and greeting', async () => {
  const res = await request(app).get('/hello');
  expect(res.status).toBe(200);
  expect(res.text).toBe('Hello World!');
});
This test sends a GET request to '/hello' and checks if the response status is 200 and the body text is 'Hello World!'.
Execution Table
StepActionRequest SentServer ResponseTest CheckResult
1Send GET request to /helloGET /helloWaiting...Waiting...Pending
2Server receives requestGET /helloProcessingWaiting...Pending
3Server processes and sends responseGET /helloStatus: 200, Body: 'Hello World!'Waiting...Pending
4Test receives responseGET /helloStatus: 200, Body: 'Hello World!'Check status == 200Pass
5Test checks response bodyGET /helloStatus: 200, Body: 'Hello World!'Check body == 'Hello World!'Pass
6Test endsGET /helloStatus: 200, Body: 'Hello World!'All checks passedTest Passed
💡 Test ends after all assertions pass confirming GET endpoint works as expected.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
res.statusundefinedundefined200200200
res.textundefinedundefined'Hello World!''Hello World!''Hello World!'
testResultundefinedPendingPendingPassTest Passed
Key Moments - 3 Insights
Why does the test wait after sending the GET request before checking the response?
Because the server needs time to process the request and send back a response. The test waits for the response before checking status and body, as shown in steps 1 to 4 in the execution_table.
What happens if the response status is not 200?
The test will fail at the status check step (step 4). The test expects 200, so any other status causes a failure, stopping further checks.
Why do we check both status and body in the test?
Checking status ensures the request succeeded, and checking body confirms the server returned the correct content. Both are needed to fully verify the GET endpoint works, as shown in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response status at step 3?
A500
B404
C200
Dundefined
💡 Hint
Check the 'Server Response' column at step 3 in the execution_table.
At which step does the test check the response body?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Test Check' column in the execution_table for the step checking body.
If the server responded with status 404, what would happen in the test?
ATest fails at status check
BTest skips status check
CTest passes anyway
DTest passes but body check fails
💡 Hint
Refer to the key_moments about what happens if status is not 200.
Concept Snapshot
Testing GET endpoints in Express:
- Use a test tool like supertest to send GET requests.
- Await the server response asynchronously.
- Check response status code (e.g., 200).
- Check response body content.
- Pass test only if all checks succeed.
Full Transcript
This visual execution shows how to test GET endpoints in Express using a test framework and supertest. The test sends a GET request to the server, waits for the response, then checks the status code and response body. The execution table traces each step from sending the request, server processing, receiving the response, to checking assertions. Variables like response status and text update as the test progresses. Key moments clarify why waiting for the response is necessary and why both status and body are checked. The quiz questions reinforce understanding by referencing the execution steps and outcomes. This approach helps beginners see exactly how testing GET endpoints works step-by-step.