What is Supertest used for in Express applications?
Supertest is a library used to test HTTP endpoints in Express apps by simulating requests and checking responses without needing the app to listen on a real network port.
Click to reveal answer
beginner
How do you start a Supertest test for an Express app?
You import Supertest and pass your Express app to it, like: <code>const request = require('supertest')(app);</code> Then you can chain HTTP methods like <code>.get()</code> or <code>.post()</code> to test routes.
Click to reveal answer
beginner
What method do you use in Supertest to check the HTTP status code of a response?
You use the .expect(statusCode) method to assert the response status code matches what you expect, for example .expect(200).
Click to reveal answer
intermediate
How can you test JSON response content with Supertest?
You can use .expect('Content-Type', /json/) to check the response type and .expect(responseBody) to check the exact JSON returned, or use a function inside .expect() to run custom checks on res.body.
Click to reveal answer
intermediate
Why is Supertest preferred over manual HTTP requests in testing Express apps?
Supertest runs tests directly on the Express app instance without needing to open network ports, making tests faster, more reliable, and easier to write and maintain.
Click to reveal answer
Which of these is the correct way to start a Supertest request for an Express app named 'app'?
Aconst request = supertest(app.listen());
Bconst request = supertest.create(app);
Cconst request = new supertest(app);
Dconst request = require('supertest')(app);
✗ Incorrect
You import Supertest and immediately call it with your Express app instance: require('supertest')(app).
How do you check that a GET request to '/users' returns status 200 using Supertest?
Arequest.get('/users').checkStatus(200);
Brequest.get('/users').status(200);
Crequest.get('/users').expect(200);
Drequest.get('/users').assert(200);
✗ Incorrect
The correct method to assert status code is .expect(200).
Which method lets you test the response body content in Supertest?
A.expect()
B.assertBody()
C.checkBody()
D.expectBody()
✗ Incorrect
The .expect() method can be used to check response body content by passing expected data or a function.
What does Supertest NOT require to run tests on an Express app?
AThe app to listen on a network port
BThe Express app instance
CNode.js environment
DA test runner like Jest or Mocha
✗ Incorrect
Supertest works directly with the app instance and does not need the app to listen on a real network port.
Which of these is a benefit of using Supertest for HTTP assertions?
AIt requires manual HTTP requests to external servers
BIt allows fast, reliable testing without network overhead
CIt slows down tests by opening network ports
DIt only works with REST APIs, not Express apps
✗ Incorrect
Supertest allows fast and reliable testing by running requests directly on the Express app instance without network overhead.
Explain how you would write a Supertest test to check that a POST request to '/login' returns a 401 status when credentials are wrong.
Think about chaining methods to send data and check response.
You got /4 concepts.
Describe why Supertest is useful for testing Express apps compared to manual HTTP requests.
Focus on speed, reliability, and ease of use.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using Supertest in Express applications?
easy
A. To create database connections automatically
B. To send fake HTTP requests and check server responses
C. To deploy the Express app to production
D. To style the Express server pages
Solution
Step 1: Understand Supertest's role
Supertest is a tool used to simulate HTTP requests to your Express server without needing a browser.
Step 2: Identify its main use
It helps test server responses like status codes and data returned, ensuring your server works as expected.
Final Answer:
To send fake HTTP requests and check server responses -> Option B
Quick Check:
Supertest = fake HTTP requests and response checks [OK]
Hint: Supertest sends fake requests to test server responses [OK]
Common Mistakes:
Thinking Supertest styles pages
Confusing Supertest with deployment tools
Assuming it manages databases
2. Which of the following is the correct way to import Supertest in a Node.js Express test file?
easy
A. import request from 'supertest';
B. const supertest = import('supertest');
C. const request = require('supertest');
D. require('supertest').request;
Solution
Step 1: Identify import syntax
In Node.js, the most common way to import Supertest is using CommonJS syntax: const request = require('supertest');.
Step 2: Check other options
import request from 'supertest'; uses ES module syntax which requires specific setup and may not work by default. const supertest = import('supertest'); uses invalid syntax. require('supertest').request; accesses a non-existent property.
The route /hello sends a response with status code 200 and body 'Hi'.
Step 2: Check the test assertion
The test expects status 201, but the actual response status is 200, so the test will fail if expecting 201.
Final Answer:
200 -> Option A
Quick Check:
Route sends 200, test expects 201, actual = 200 [OK]
Hint: Match expected status with route's actual status code [OK]
Common Mistakes:
Confusing expected and actual status codes
Assuming 201 is default success code
Ignoring route's explicit status
4. Identify the error in this Supertest test code snippet:
import request from 'supertest';
import app from './app';
test('POST /data returns 201', async () => {
const res = await request(app).post('/data')
.send({ name: 'Alice' })
.expect(201);
expect(res.body.name).toBe('Alice');
});
medium
A. No error, code is correct
B. Missing await before request call
C. Using .expect() and then checking res.body without await
D. POST method should not have a body
Solution
Step 1: Review async/await usage
The await is correctly used before request(app).post(...), so the promise resolves before assertions.
Step 2: Check .expect and body assertions
Using .expect(201) is valid and chaining expect(res.body.name).toBe('Alice') is correct after awaiting the response.
Final Answer:
No error, code is correct -> Option A
Quick Check:
Await + .expect + body check = correct usage [OK]
Hint: Await the request before assertions to avoid errors [OK]
Common Mistakes:
Forgetting await causing unresolved promises
Misusing .expect without awaiting
Assuming POST cannot have a body
5. You want to test an Express route that returns JSON only if a query parameter type=admin is present, otherwise it returns 403 Forbidden. Which Supertest code snippet correctly tests the 403 response when the query parameter is missing?
hard
A. await request(app).get('/secret').query({ type: 'user' }).expect(403);
B. await request(app).get('/secret?type=admin').expect(403);
C. await request(app).post('/secret').send({ type: 'admin' }).expect(403);
D. await request(app).get('/secret').expect(403);
Solution
Step 1: Understand route behavior
The route returns JSON only if query type=admin is present; otherwise, it returns 403 Forbidden.
Step 2: Identify test for missing query parameter
To test 403 when the parameter is missing, send a GET request without any query parameters.
Step 3: Evaluate options
await request(app).get('/secret').expect(403); sends GET without query, expecting 403, which matches the requirement. await request(app).get('/secret').query({ type: 'user' }).expect(403); sends type=user, which is not missing but wrong type, so may or may not return 403 depending on implementation. await request(app).post('/secret').send({ type: 'admin' }).expect(403); uses POST instead of GET. await request(app).get('/secret?type=admin').expect(403); sends type=admin which should return success, not 403.
Final Answer:
await request(app).get('/secret').expect(403); -> Option D
Quick Check:
Missing query param = GET without query = expect 403 [OK]
Hint: Test missing query by omitting .query() in GET request [OK]
Common Mistakes:
Sending wrong HTTP method (POST instead of GET)
Including wrong query parameters instead of missing
Expecting 403 when query param is present
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.