0
0
Expressframework~10 mins

Supertest for HTTP assertions in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Supertest for HTTP assertions
Write Express app
Import Supertest
Create test request
Send HTTP request
Receive response
Assert response status and body
Test passes or fails
This flow shows how Supertest sends HTTP requests to an Express app and checks the responses step-by-step.
Execution Sample
Express
const request = require('supertest');
const express = require('express');
const app = express();
app.get('/hello', (req, res) => res.send('Hi!'));

request(app).get('/hello').expect(200).expect('Hi!').end();
This code creates a simple Express app and uses Supertest to check that GET /hello returns status 200 and body 'Hi!'.
Execution Table
StepActionRequest Method & URLResponse StatusResponse BodyAssertion Result
1Create Express app----
2Define GET /hello route----
3Supertest sends GET requestGET /helloPendingPendingPending
4Express app receives requestGET /hello---
5App sends responseGET /hello200Hi!-
6Supertest checks statusGET /hello200Hi!Pass
7Supertest checks bodyGET /hello200Hi!Pass
8Test ends---Pass
💡 Test ends after all assertions pass or fail
Variable Tracker
VariableStartAfter Step 3After Step 5Final
requestundefinedSupertest request objectSupertest response objectTest result (pass)
appExpress app instanceExpress app instanceExpress app instanceExpress app instance
Key Moments - 3 Insights
Why does Supertest need the Express app instance?
Supertest uses the app instance to send HTTP requests directly without starting a server, as shown in execution_table step 3.
What happens if the response status is not what we expect?
Supertest will mark the assertion as failed at step 6, stopping the test or reporting failure.
Can Supertest check response headers too?
Yes, you can add .expect('header-name', 'value') to check headers, similar to how body and status are checked in steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response body at step 5?
A"Hello"
B"Hi!"
CEmpty string
DUndefined
💡 Hint
Check the 'Response Body' column at step 5 in the execution_table.
At which step does Supertest verify the HTTP status code?
AStep 4
BStep 3
CStep 6
DStep 7
💡 Hint
Look at the 'Assertion Result' column for status check in execution_table.
If the app returned status 404 instead of 200, what would happen in the execution table?
AStep 6 assertion would fail
BStep 5 response status would be 200
CTest would pass anyway
DStep 3 request would change
💡 Hint
Refer to the 'Response Status' and 'Assertion Result' columns in execution_table.
Concept Snapshot
Supertest lets you test Express apps by sending HTTP requests directly.
Use request(app).get('/path').expect(status).expect(body) to check responses.
No need to start a server; Supertest handles it internally.
Assertions stop the test if they fail.
You can chain multiple .expect() calls for status, body, headers.
Full Transcript
Supertest is a tool to test Express HTTP servers easily. You write your Express app code, then use Supertest to send HTTP requests to it without starting a real server. The flow starts by creating the app, then Supertest sends a request like GET /hello. The app receives it and sends back a response. Supertest checks the response status and body against what you expect. If all checks pass, the test passes. If any check fails, the test fails. Variables like the request object and app instance change as the test runs. Common confusions include why the app instance is needed (to send requests directly), what happens on assertion failure (test fails immediately), and that Supertest can check headers too. The execution table shows each step from sending the request to checking the response. This helps beginners see how the test runs step-by-step.