📖 Scenario: You are building a simple NestJS application that manages a list of books. You want to ensure your API endpoints work correctly by writing end-to-end (E2E) tests using supertest. This will help catch bugs early and keep your app reliable.
🎯 Goal: Write E2E tests for the GET /books endpoint using supertest in a NestJS testing environment. You will set up the test module, configure the app, write the test to check the response, and finalize the test suite.
📋 What You'll Learn
Create a NestJS testing module with the BooksModule
Initialize the NestJS application for testing
Write a supertest test to check the GET /books endpoint returns status 200 and an empty array
Close the NestJS application after tests complete
💡 Why This Matters
🌍 Real World
E2E testing ensures your API works as expected from the user's perspective. It helps catch bugs before deployment and improves software quality.
💼 Career
Many companies require developers to write automated tests for backend APIs. Knowing how to write E2E tests with NestJS and supertest is a valuable skill for backend and full-stack roles.
Progress0 / 4 steps
1
Setup NestJS Testing Module
Import Test and TestingModule from @nestjs/testing. Create a testing module with BooksModule using Test.createTestingModule and compile it. Assign the compiled module to a variable called moduleFixture.
NestJS
Hint
Use Test.createTestingModule with imports: [BooksModule] and call compile() to get the testing module.
2
Initialize NestJS Application for Testing
Create a NestJS application instance from moduleFixture by calling createNestApplication(). Assign it to a variable called app. Then call await app.init() to initialize the app.
NestJS
Hint
Use moduleFixture.createNestApplication() to create the app, then call await app.init().
3
Write E2E Test for GET /books Endpoint
Use supertest to send a GET request to /books on the app.getHttpServer(). Write a test that expects the response status to be 200 and the response body to be an empty array []. Use await request(app.getHttpServer()).get('/books').expect(200).expect([]).
NestJS
Hint
Use request(app.getHttpServer()).get('/books').expect(200).expect([]) to test the endpoint.
4
Close the NestJS Application After Tests
Call await app.close() to properly shut down the NestJS application after tests finish.
NestJS
Hint
Call await app.close() to clean up the app after tests.
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?