What is the main purpose of E2E testing in a NestJS application?
E2E testing checks the whole application flow from start to finish, ensuring all parts work together as expected, like a user interacting with the app.
Click to reveal answer
beginner
What role does Supertest play in E2E testing with NestJS?
Supertest helps simulate HTTP requests to the NestJS app, allowing tests to check responses without needing a real browser or client.
Click to reveal answer
intermediate
How do you start a NestJS application instance for E2E testing with Supertest?
You create a testing module with Test.createTestingModule, compile it, then create a Nest application instance and call app.init() before running tests.
Click to reveal answer
intermediate
Why is it important to close the NestJS app after E2E tests finish?
Closing the app frees resources and stops the server, preventing conflicts or memory leaks when running multiple tests.
Click to reveal answer
intermediate
What is a common pattern to test an HTTP POST request using Supertest in NestJS E2E tests?
Use supertest's request(app.getHttpServer()).post('/route').send(data).expect(statusCode) to send data and check the response status and body.
Click to reveal answer
Which method initializes the NestJS app instance for E2E testing?
Aapp.init()
Bapp.start()
Capp.run()
Dapp.launch()
✗ Incorrect
app.init() prepares the NestJS app instance for handling requests in tests.
What does Supertest simulate in E2E tests?
ADatabase queries
BFile uploads
CHTTP requests
DUser interface clicks
✗ Incorrect
Supertest simulates HTTP requests to test server responses.
In NestJS E2E tests, where do you get the HTTP server instance for Supertest?
Aapp.getHttpServer()
Bapp.getServer()
Capp.getRequestHandler()
Dapp.getListener()
✗ Incorrect
app.getHttpServer() returns the underlying HTTP server for Supertest to send requests.
Why should you call app.close() after E2E tests?
ATo reset the database
BTo stop the server and free resources
CTo clear test data
DTo restart the app automatically
✗ Incorrect
app.close() stops the server and frees resources after tests.
Which NestJS testing utility helps create a testing module for E2E tests?
ANestFactory.createTesting()
BTestingModule.create()
CTestModule.build()
DTest.createTestingModule()
✗ Incorrect
Test.createTestingModule() builds a module for testing purposes.
Explain the steps to set up and run an E2E test in NestJS using Supertest.
Think about setup, request simulation, assertions, and cleanup.
You got /6 concepts.
Describe why E2E testing with Supertest is useful for a NestJS application.
Focus on the benefits of testing the whole app behavior.
You got /4 concepts.
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
Step 1: Understand E2E testing goal
E2E testing checks the entire app by simulating real HTTP requests and responses.
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.
Final Answer:
To simulate HTTP requests and test the whole app behavior -> Option D
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
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.
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.
Final Answer:
import * as request from 'supertest'; -> Option B
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?
The return statement ends the function immediately, so await app.close() after return is unreachable and never runs.
Step 2: Identify proper app closing
To close the app properly, await app.close() must be called before returning the request promise.
Final Answer:
Calling app.close() after return statement -> Option C
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?