0
0
NestJSframework~10 mins

E2E testing with supertest in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - E2E testing with supertest
Start Test Suite
Initialize NestJS App
Use supertest to send HTTP request
Receive HTTP response
Check response status and body
Pass or Fail Test
Close App and End Test
The test starts by setting up the NestJS app, then supertest sends an HTTP request. The response is checked for expected status and data, then the test ends.
Execution Sample
NestJS
import request from 'supertest';
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';

describe('AppController (e2e)', () => {
  let app;
  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
    app = moduleRef.createNestApplication();
    await app.init();
  });

  it('/ (GET)', () => {
    return request(app.getHttpServer())
      .get('/')
      .expect(200)
      .expect('Hello World!');
  });

  afterAll(async () => {
    await app.close();
  });
});
This code sets up a NestJS app, sends a GET request to '/', expects status 200 and 'Hello World!' response, then closes the app.
Execution Table
StepActionEvaluationResult
1Start test suiteN/ATest suite begins
2Create testing module with AppModuleModule createdModuleRef ready
3Create Nest app from moduleApp instance createdApp initialized
4Call app.init()App ready to receive requestsApp listening
5supertest sends GET request to '/'Request sentWaiting for response
6Receive responseStatus 200, Body 'Hello World!'Response matches expectation
7Test passesExpectations metTest success
8Call app.close()App shuts downResources freed
9Test suite endsN/AAll tests complete
💡 Test ends after app closes and all expectations pass
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 8Final
moduleRefundefinedModuleRef instanceModuleRef instanceModuleRef instanceModuleRef instanceModuleRef instanceModuleRef instance
appundefinedundefinedNest app instanceApp initializedApp initializedApp closedApp closed
responseundefinedundefinedundefinedundefined{status:200, body:'Hello World!'}{status:200, body:'Hello World!'}{status:200, body:'Hello World!'}
Key Moments - 3 Insights
Why do we call app.init() before sending requests?
app.init() prepares the NestJS app to handle HTTP requests. Without it, supertest cannot send requests properly. See step 4 in execution_table.
What happens if the response status is not 200?
The test fails because the expectation in step 6 is not met. The test runner will report an error at this step.
Why do we call app.close() after tests?
Calling app.close() frees resources and stops the server. This prevents hanging processes after tests finish, as shown in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the app state after step 4?
AApp is closed
BApp is not initialized
CApp is ready to receive requests
DApp is sending a request
💡 Hint
Check the 'Result' column at step 4 in execution_table
At which step does supertest receive the HTTP response?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for 'Receive response' action in execution_table
If app.close() is not called, what might happen?
AThe test process may hang after tests finish
BThe app will not start
CTests will fail immediately
DThe response status will be 500
💡 Hint
Refer to step 8 and key_moments about app.close()
Concept Snapshot
E2E testing with supertest in NestJS:
- Setup Nest app with Test.createTestingModule
- Call app.init() to start server
- Use supertest to send HTTP requests
- Check response status and body with expect
- Close app with app.close() after tests
- Ensures full app behavior tested via HTTP
Full Transcript
This visual execution shows how to do end-to-end testing in NestJS using supertest. First, the test suite starts and creates a testing module with the app's main module. Then, it creates a NestJS app instance and calls app.init() to prepare it for HTTP requests. Next, supertest sends a GET request to the root path '/'. The app responds with status 200 and body 'Hello World!'. The test checks these values and passes if they match. Finally, the app is closed to free resources and end the test cleanly. Key points include calling app.init() before requests and app.close() after tests to avoid hanging processes.