0
0
NestJSframework~20 mins

E2E testing with supertest in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Supertest E2E Master
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 NestJS E2E test using Supertest?
Consider this E2E test snippet for a NestJS controller endpoint that returns a JSON object with a greeting message. What will the test output be when run?
NestJS
return request(app.getHttpServer())
  .get('/hello')
  .expect(200)
  .expect('Content-Type', /json/)
  .expect({ message: 'Hello, world!' });
ATest fails due to a Content-Type mismatch error.
BTest fails with a 404 error because the endpoint '/hello' does not exist.
CTest fails because the response message is different from the expected one.
DTest passes because the response matches the expected JSON and status code.
Attempts:
2 left
💡 Hint
Check the expected status code and JSON body in the test.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses Supertest to test a POST request with JSON body in NestJS?
You want to test a POST endpoint '/users' that accepts a JSON body with a 'name' property. Which code snippet correctly sends the JSON body and expects a 201 status?
Arequest(app.getHttpServer()).post('/users').body({ name: 'Alice' }).expect(201);
Brequest(app.getHttpServer()).post('/users').send({ name: 'Alice' }).expect(201);
Crequest(app.getHttpServer()).post('/users').json({ name: 'Alice' }).expect(201);
Drequest(app.getHttpServer()).post('/users').set('Content-Type', 'text/plain').send({ name: 'Alice' }).expect(201);
Attempts:
2 left
💡 Hint
Supertest uses .send() to send JSON bodies by default.
🔧 Debug
advanced
2:00remaining
Why does this NestJS E2E test with Supertest fail with a timeout?
This test tries to GET '/items' but never completes and times out. What is the most likely cause?
NestJS
it('should return items', () => {
  return request(app.getHttpServer())
    .get('/items')
    .expect(200)
    .expect(res => {
      expect(res.body.length).toBeGreaterThan(0);
    });
});
AThe NestJS app was not initialized or started before running the test.
BThe test is missing a call to .end() to finish the request.
CThe endpoint '/items' returns a 404 causing the test to hang.
DThe expect callback throws an error causing the test to timeout.
Attempts:
2 left
💡 Hint
Check if the app instance is properly created and listening before tests.
state_output
advanced
2:00remaining
What is the value of 'createdUserId' after this E2E test runs?
This test creates a user and saves the returned id in 'createdUserId'. What will be the value of 'createdUserId' after the test?
NestJS
let createdUserId;

it('creates a user', async () => {
  const response = await request(app.getHttpServer())
    .post('/users')
    .send({ name: 'Bob' })
    .expect(201);
  createdUserId = response.body.id;
});
Anull because the server returns no content on creation.
Bundefined because the response body does not contain 'id'.
CA string or number representing the new user's id returned by the server.
DAn error is thrown because 'createdUserId' is not declared.
Attempts:
2 left
💡 Hint
Check what the server returns in the response body after creating a user.
🧠 Conceptual
expert
3:00remaining
Which statement best describes the role of Supertest in NestJS E2E testing?
Choose the most accurate description of how Supertest works within NestJS E2E tests.
ASupertest simulates HTTP requests to the NestJS app's HTTP server to test endpoints as a real client would.
BSupertest mocks NestJS services internally to test controller logic without starting the server.
CSupertest replaces the NestJS HTTP server with a fake server to speed up tests.
DSupertest runs unit tests on individual NestJS components without network calls.
Attempts:
2 left
💡 Hint
Think about how E2E tests simulate real user interactions.