Do not pass e2e tests in framework NestJS
If you want to write e2e tests with mocks you don't need to import the AppModule
you only should import your AppController
and AppService
, in this way you avoid to connect to your database and use mocks to test the entire application flow.
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppController } from './../src/app.controller';
import { AppService } from './../src/app.service';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [],
controllers: [AppController],
providers: [AppService],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(404)
.expect('{"statusCode":404,"error":"Not Found","message":"Cannot GET /"}'); //todo fix me
});
});
With this approach you get a clean testing module without TypeOrmModule
.
NOTE: if you need to mock the service, the Test
has a method overrideProvider
to override your service and mothods like useClass
, useValue
or useFactory
to provide your mock.
If you want to write an integration test to confirm that all together works fine you can override the configuration of your TypeOrmModule
, passing it to the testing module with a new DB configuration like this post describes.
I hope I've helped. Good luck and regards.