1

I am currently trying to get the response object of a 'supertest' request.

If I call a get without an await, I get an httpCode 200, but with no body:

import { Test, TestingModule } from '@nestjs/testing';

import { AuthModule } from './auth.module';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';

describe('AuthService', () => {
   let app: INestApplication;
   beforeAll(async () => {
     const module: TestingModule = await Test.createTestingModule({
  providers: [AuthModule]
}).compile();
app = module.createNestApplication();
await app.init();
});

it('should be defined', async () => {
const res = request(app.getHttpServer())
  .get('/')
  .expect(200);

});

afterAll(async () => {
  app.close();
});
});

Jest gives me the following output. But I cannot refer to res.body

  AuthService
√ should be defined (5ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        15.961s, estimated 16s

Now, if I change the get call to an async call:

  it('should be defined', async () => {
const res = await request(app.getHttpServer())
  .get('/')
  .expect(200);

});

I get an fail test result:

  AuthService
× should be defined (35ms)

● AuthService › should be defined

expected 200 "OK", got 404 "Not Found"

  at Test.Object.<anonymous>.Test._assertStatus (node_modules/supertest/lib/test.js:268:12)
  at Test.Object.<anonymous>.Test._assertFunction (node_modules/supertest/lib/test.js:283:11)
  at Test.Object.<anonymous>.Test.assert (node_modules/supertest/lib/test.js:173:18)
  at Server.localAssert (node_modules/supertest/lib/test.js:131:12)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total

Without the async call, I cannot refer to the body. But I get everytime an 404, on the same get function. Just used await for the async call.

Kim Kern
  • 54,283
  • 17
  • 197
  • 195
Makuna
  • 558
  • 1
  • 6
  • 19

1 Answers1

5

The test without async passes only because the test finishes before your assertion expect(200) runs. In both cases, calling / will return a 404 error.

The main problem is that you are declaring a module a provider, instead of importing it:

await Test.createTestingModule({
  // should be imports instead of providers
  providers: [AuthModule]
})

Why are you setting up your AuthModule separately from the rest of your application? I would advise you to test your unit test in separation (only include the provider that is tested, mock everything else) and test your whole application in e2e tests (only mock distinct parts of your app if necessary, e.g., API calls to 3rd-party services); see this thread for more details.

I'd suggest to import the AppModule instead:

const module: TestingModule = await Test.createTestingModule({
  imports: [AppModule]
}).compile();
Kim Kern
  • 54,283
  • 17
  • 197
  • 195
  • I made it like u said. But now, I get the following message: Cannot find module '@project/module-dto' from 'config.service.ts' But I have imported the corresponding files in config.service.ts – Makuna Aug 26 '20 at 12:38
  • Did you import the AppModule? It's difficult to help without seeing your code. The problem seems to be very different now though. Easiest would be if you open a new question and include all relevant bits of your code, e. g., the config service/module. – Kim Kern Aug 26 '20 at 13:42
  • Thx, I will do it. – Makuna Aug 26 '20 at 13:56