I used TypeORMModule to provide config for the DataSource:
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: `.env.${process.env.NODE_ENV}`,
}),
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => {
return {
type: 'postgres',
// host has to be the container name of the database
host: config.get<string>('POSTGRES_HOST'),
port: parseInt(config.get('POSTGRES_PORT')),
username: config.get<string>('POSTGRES_USER'),
password: config.get<string>('POSTGRES_PASSWORD'),
database: config.get<string>('POSTGRES_DB'),
synchronize: true,
entities: [User],
};
},
}),
UsersModule,
],
})
How do I go about clearing the database for each test?
I got dataSource
from app.get(DataSource)
and applied delete:
afterEach(async () => {
const dataSource = app.get(DataSource);
await dataSource.createQueryBuilder().delete().from(User).execute();
});