0
0
Expressframework~20 mins

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

Choose your learning style9 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.