Bird
Raised Fist0
Expressframework~20 mins

Supertest for HTTP assertions in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Supertest Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Supertest assertion?
Given the Express app below, what will the Supertest assertion return?
Express
const express = require('express');
const app = express();
app.get('/hello', (req, res) => {
  res.status(200).json({message: 'Hello World'});
});

const request = require('supertest');

request(app)
  .get('/hello')
  .expect('Content-Type', /json/)
  .expect(200)
  .then(response => console.log(response.body))
  .catch(err => console.error(err));
A{"message":"Hello World"}
BSyntaxError
C{"message":"hello world"}
DTypeError
Attempts:
2 left
💡 Hint
Check the JSON response body and status code returned by the endpoint.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in Supertest usage?
Identify which Supertest code snippet will cause a syntax error.
Express
const request = require('supertest');
const app = require('./app');
Arequest(app).get('/').expect(200).end((err, res) => { if (err) throw err; });
Brequest(app).post('/login').send({user: 'test'}).expect(200).then(res => console.log(res.body));
Crequest(app).get('/').expect(200).then(res => console.log(res.body);
Drequest(app).get('/').expect('Content-Type', /json/).expect(200);
Attempts:
2 left
💡 Hint
Look carefully at parentheses and semicolons in the code.
🔧 Debug
advanced
2:00remaining
Why does this Supertest test fail with a timeout?
Consider this test code: const request = require('supertest'); const app = require('./app'); test('GET /data returns 200', () => { return request(app) .get('/data') .expect(200); }); Why might this test fail with a timeout error?
AThe Express app does not call res.end() or res.send(), so the response never finishes.
BThe test is missing a done callback parameter.
CSupertest does not support promises, so .expect(200) is invalid.
DThe route '/data' does not exist, so it returns 404 immediately.
Attempts:
2 left
💡 Hint
Think about what happens if the server never sends a response.
❓ state_output
advanced
2:00remaining
What is the value of res.body after this Supertest request?
Given this Express route and Supertest request, what is the value of res.body?
Express
const express = require('express');
const app = express();
app.use(express.json());

app.post('/sum', (req, res) => {
  const {a, b} = req.body;
  res.json({result: a + b});
});

const request = require('supertest');

request(app)
  .post('/sum')
  .send({a: 5, b: 7})
  .expect(200)
  .then(res => console.log(res.body));
A{}
B{"result":"57"}
C{"a":5,"b":7}
D{"result":12}
Attempts:
2 left
💡 Hint
Check how the route calculates the sum and what it sends back.
🧠 Conceptual
expert
2:00remaining
Which option best explains why Supertest can test Express apps without a running server?
Why can Supertest send HTTP requests to an Express app instance without calling app.listen()?
ASupertest requires the app to be running on localhost before tests can run.
BSupertest uses Node.js http module to inject requests directly into the Express app's request handler function.
CSupertest automatically starts a hidden server on a random port before sending requests.
DSupertest mocks the Express app methods to simulate responses without real HTTP traffic.
Attempts:
2 left
💡 Hint
Think about how Supertest integrates with Express internally.

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

  1. Step 1: Understand Supertest's role

    Supertest is a tool used to simulate HTTP requests to your Express server without needing a browser.
  2. Step 2: Identify its main use

    It helps test server responses like status codes and data returned, ensuring your server works as expected.
  3. Final Answer:

    To send fake HTTP requests and check server responses -> Option B
  4. 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

  1. Step 1: Identify import syntax

    In Node.js, the most common way to import Supertest is using CommonJS syntax: const request = require('supertest');.
  2. 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.
  3. Final Answer:

    const request = require('supertest'); -> Option C
  4. Quick Check:

    CommonJS require syntax = const request = require('supertest'); [OK]
Hint: Use CommonJS require syntax for Supertest in Node.js [OK]
Common Mistakes:
  • Using ES module import without setup
  • Trying to destructure or access wrong properties
  • Using invalid import syntax
3. Given the following test code, what will be the status code of the response?
import request from 'supertest';
import express from 'express';

const app = express();
app.get('/hello', (req, res) => res.status(200).send('Hi'));

test('GET /hello returns status 200', async () => {
  const response = await request(app).get('/hello');
  expect(response.status).toBe(201);
});
medium
A. 200
B. 404
C. 201
D. 500

Solution

  1. Step 1: Analyze the Express route

    The route /hello sends a response with status code 200 and body 'Hi'.
  2. 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.
  3. Final Answer:

    200 -> Option A
  4. 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

  1. Step 1: Review async/await usage

    The await is correctly used before request(app).post(...), so the promise resolves before assertions.
  2. 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.
  3. Final Answer:

    No error, code is correct -> Option A
  4. 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

  1. Step 1: Understand route behavior

    The route returns JSON only if query type=admin is present; otherwise, it returns 403 Forbidden.
  2. Step 2: Identify test for missing query parameter

    To test 403 when the parameter is missing, send a GET request without any query parameters.
  3. 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.
  4. Final Answer:

    await request(app).get('/secret').expect(403); -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard