Reputation: 657
I use the NestJS framework and typeorm. When working with a database, all data is successfully saved. There are no problems with the connection. I try to configure e2e test with Jest. Unfortunately, I got 2 errors:
TypeError: Cannot read property 'getHttpServer' of undefined
and
No repository for "User" was found. Looks like this entity is not registered in current "default" connection?
I tried to setup test env by using this tutorial.
My files:
app.e2e-spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
import { TypeOrmModule } from '@nestjs/typeorm';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
AppModule,
TypeOrmModule.forRoot({
'type': 'mssql',
'host': 'localhost',
'port': 1433,
'username': 'gift_draw_db',
'password': '',
'database': 'gift_draw_local',
'entities': ['./**/*.entity.ts'],
'synchronize': false,
}),
],
providers: [],
}).compile();
app = moduleFixture.createNestApplication();
console.error(process.env.DB_DATABASE_NAME, '<------------------------------'); // NEVER SEEN THIS
await app.init();
});
afterAll(async () => {
await app.close();
});
it('/api/ (GET)', async () => {
return request(app.getHttpServer()) // 1st error
.get('/api/')
.expect(200)
.expect('Working!');
});
});
app.module.ts
@Module({
imports: [
ConfigModule,
AuthModule,
DrawModule,
UserModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
user.e2e-spec.ts
describe('User', () => {
let app: INestApplication;
let repository: Repository<User>;
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [
UserModule,
TypeOrmModule.forRoot({
'type': 'mssql',
'host': 'localhost',
'port': 1433,
'username': 'gift_draw_db',
'password': '',
'database': 'gift_draw_local',
'entities': ['./**/*.entity.ts'],
'synchronize': false,
}),
],
}).compile();
app = module.createNestApplication();
repository = module.get('UserRepository');
await app.init();
});
afterEach(async () => {
await repository.query(`DELETE FROM users;`);
});
afterAll(async () => {
await app.close();
});
});
user.module.ts
@Module({
controllers: [UserController],
imports: [TypeOrmModule.forFeature([User])],
providers: [UserService],
exports: [UserService, TypeOrmModule],
})
export class UserModule {
}
config.module.ts
@Module({
imports: [
NestConfigModule.forRoot({
isGlobal: true,
load: [configuration]
}),
TypeOrmModule.forRoot(process.env.NODE_ENV === 'production' ? {...configProd} : {...configDev} )
],
})
export class ConfigModule {
}
ConfigModule has the same credentials as testing one.
Upvotes: 2
Views: 2241
Reputation: 111
The problem is that when you start the app the base dir is "dist" while when you start it with for the "e2e" tests, the base dir is "src".
You can change the entities definition to something similar to this:
entities: [__dirname + '/../**/*.entity.ts']
Upvotes: 5