0
0
Expressframework~10 mins

Supertest for HTTP assertions in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import Supertest and create a request object for the Express app.

Express
const request = require('[1]');
const app = require('./app');

// Use request to test the app
Drag options to blanks, or click blank then click option'
Achai
Bhttp
Csupertest
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'express' instead of 'supertest'.
Using 'http' module which is lower level.
Forgetting to import any testing library.
2fill in blank
medium

Complete the code to test that a GET request to '/' returns status 200.

Express
request(app)
  .get('/')
  .expect([1], done);
Drag options to blanks, or click blank then click option'
A302
B404
C500
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Expecting 404 or 500 instead of 200.
Confusing redirect status 302 with success 200.
3fill in blank
hard

Fix the error in the test to check the response body contains 'Hello World'.

Express
request(app)
  .get('/')
  .expect('Content-Type', /json/)
  .expect([1], done);
Drag options to blanks, or click blank then click option'
A{ message: 'Hello World' }
BHello World
C/Hello World/
D'Hello World'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an object for JSON response.
Using regex where an object is expected.
4fill in blank
hard

Fill both blanks to test a POST request sending JSON and expecting status 201.

Express
request(app)
  .post('/users')
  .send([1])
  .expect([2], done);
Drag options to blanks, or click blank then click option'
A{ name: 'Alice' }
B200
C201
D{ age: 30 }
Attempts:
3 left
💡 Hint
Common Mistakes
Sending wrong JSON data.
Expecting 200 instead of 201 for creation.
5fill in blank
hard

Fill all three blanks to test a PUT request updating a user and checking JSON response.

Express
request(app)
  .put('/users/123')
  .send([1])
  .expect('Content-Type', /[2]/)
  .expect([3], done);
Drag options to blanks, or click blank then click option'
A{ name: 'Bob' }
Bjson
C{ success: true }
Dhtml
Attempts:
3 left
💡 Hint
Common Mistakes
Sending wrong data format.
Expecting HTML content type instead of JSON.
Not matching the expected JSON response.