Bird
Raised Fist0
NestJSframework~20 mins

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

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

Practice

(1/5)
1. What is the main purpose of using supertest in NestJS E2E testing?
easy
A. To style the frontend components
B. To mock database calls inside unit tests
C. To compile TypeScript code before running tests
D. To simulate HTTP requests and test the whole app behavior

Solution

  1. Step 1: Understand E2E testing goal

    E2E testing checks the entire app by simulating real HTTP requests and responses.
  2. Step 2: Role of supertest in NestJS

    Supertest helps send HTTP requests to the running NestJS app and verify responses, enabling full app behavior testing.
  3. Final Answer:

    To simulate HTTP requests and test the whole app behavior -> Option D
  4. Quick Check:

    E2E testing = simulate HTTP requests [OK]
Hint: Remember: supertest sends HTTP requests to test app end-to-end [OK]
Common Mistakes:
  • Confusing supertest with mocking libraries
  • Thinking supertest is for styling or compiling
  • Using supertest only for unit tests
2. Which of the following is the correct way to import and use supertest in a NestJS E2E test file?
easy
A. const request = import('supertest');
B. import * as request from 'supertest';
C. import request from 'supertest';
D. const request = require('supertest');

Solution

  1. Step 1: Check ES module import style

    In modern NestJS with ES modules, import * as request from 'supertest'; is the correct syntax to import all exports as an object.
  2. Step 2: Identify incorrect options

    const request = require('supertest'); uses CommonJS require, which is discouraged in ES module projects. import request from 'supertest'; assumes default export which supertest does not have. const request = import('supertest'); is invalid syntax.
  3. Final Answer:

    import * as request from 'supertest'; -> Option B
  4. Quick Check:

    Use ES module import * as for supertest [OK]
Hint: Use import * as request from 'supertest' in NestJS E2E tests [OK]
Common Mistakes:
  • Using require() instead of import
  • Assuming supertest has a default export
  • Using dynamic import incorrectly
3. Given this NestJS E2E test snippet, what will be the HTTP status code returned by the test?
const app = await NestFactory.create(AppModule);
await app.init();
return request(app.getHttpServer())
  .get('/users')
  .expect(200);
medium
A. 200
B. 404
C. 500
D. 301

Solution

  1. Step 1: Analyze the test request

    The test sends a GET request to '/users' on the initialized app and expects status 200.
  2. Step 2: Understand .expect(200) behavior

    The .expect(200) asserts the response status must be 200 for the test to pass.
  3. Final Answer:

    200 -> Option A
  4. Quick Check:

    Test expects status 200, so response = 200 [OK]
Hint: Check the .expect() status code to predict response [OK]
Common Mistakes:
  • Confusing expected status with actual response
  • Assuming 404 if route not defined without context
  • Ignoring .expect() assertion meaning
4. Identify the error in this NestJS E2E test code snippet:
const app = await NestFactory.create(AppModule);
return request(app.getHttpServer())
  .post('/login')
  .send({ username: 'user', password: 'pass' })
  .expect(201);
await app.close();
medium
A. Using .send() with POST request is invalid
B. Missing await before request call
C. Calling app.close() after return statement
D. Expecting status 201 instead of 200

Solution

  1. Step 1: Check code flow with return and await

    The return statement ends the function immediately, so await app.close() after return is unreachable and never runs.
  2. Step 2: Identify proper app closing

    To close the app properly, await app.close() must be called before returning the request promise.
  3. Final Answer:

    Calling app.close() after return statement -> Option C
  4. Quick Check:

    Code after return is unreachable [OK]
Hint: Place app.close() before return to ensure cleanup [OK]
Common Mistakes:
  • Ignoring unreachable code after return
  • Thinking .send() is invalid for POST
  • Assuming status 201 is always wrong
5. You want to write an E2E test in NestJS that sends a POST request with JSON data, verifies the response body contains a specific property, and then closes the app properly. Which code snippet correctly implements this?
hard
A. const app = await NestFactory.create(AppModule); await app.init(); const response = await request(app.getHttpServer()) .post('/items') .send({ name: 'item1' }) .expect(201); expect(response.body).toHaveProperty('id'); await app.close();
B. const app = await NestFactory.create(AppModule); const response = request(app.getHttpServer()) .post('/items') .send({ name: 'item1' }) .expect(201); expect(response.body).toHaveProperty('id'); await app.close();
C. const app = await NestFactory.create(AppModule); await app.init(); request(app.getHttpServer()) .post('/items') .send({ name: 'item1' }) .expect(201) .then(response => { expect(response.body).toHaveProperty('id'); }); await app.close();
D. const app = await NestFactory.create(AppModule); await app.init(); const response = await request(app.getHttpServer()) .post('/items') .send({ name: 'item1' }) .expect(200); expect(response.body).toHaveProperty('id'); await app.close();

Solution

  1. Step 1: Initialize and start the app

    Await NestFactory.create(AppModule) and app.init() to fully bootstrap and start the HTTP server before sending requests.
  2. Step 2: Send POST request and await response

    Await the supertest chain request(app.getHttpServer()).post('/items').send({ name: 'item1' }).expect(201) to capture the response object.
  3. Step 3: Verify response body and close app

    Assert expect(response.body).toHaveProperty('id') then await app.close() for proper cleanup.
  4. Final Answer:

    The snippet with await app.init(), await request.expect(201), body check, and await app.close() -> Option A
  5. Quick Check:

    Await init, await request, assert body, await close [OK]
Hint: Always await app.init(), request, and app.close() in E2E tests [OK]
Common Mistakes:
  • Not awaiting app.init() before requests
  • Not awaiting request call causing missing response
  • Expecting wrong status code (200 vs 201)
  • Calling app.close() too early or after return
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard