Challenge - 5 Problems
Supertest Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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));
Attempts:
2 left
💡 Hint
Check the JSON response body and status code returned by the endpoint.
✗ Incorrect
The Express route returns a JSON object with message 'Hello World' and status 200. Supertest checks for JSON content type and status 200, so the response body logged is {message: 'Hello World'}.
📝 Syntax
intermediate2: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');
Attempts:
2 left
💡 Hint
Look carefully at parentheses and semicolons in the code.
✗ Incorrect
Option C is missing a closing parenthesis for the then() method, causing a syntax error.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Think about what happens if the server never sends a response.
✗ Incorrect
If the Express route handler does not send or end the response, Supertest waits indefinitely, causing a timeout.
❓ state_output
advanced2: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));
Attempts:
2 left
💡 Hint
Check how the route calculates the sum and what it sends back.
✗ Incorrect
The route extracts a and b from the JSON body, adds them as numbers, and returns {result: 12}.
🧠 Conceptual
expert2: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()?
Attempts:
2 left
💡 Hint
Think about how Supertest integrates with Express internally.
✗ Incorrect
Supertest creates HTTP requests that call the Express app's handler function directly, bypassing the need for a real network server.