0
0
Expressframework~5 mins

Supertest for HTTP assertions in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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);
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);
Which method lets you test the response body content in Supertest?
A.expect()
B.assertBody()
C.checkBody()
D.expectBody()
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
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
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.