![](/img/trans.png)
[英]TypeORM and NestJS: Creating database tables at the beginning of an e2e test
[英]Manage the test connection in e2e nestjs
我正在尝试仅为 e2e 测试创建一个数据库,但是当我需要删除它时,我删除了主数据库。
问题应该是 getConnectionToken 因为我在放置数据库期间使用它,但它在我的 beforeEach 中获得了主连接
const moduleRef = await Test.createTestingModule({
imports: [
MongooseModule.forRootAsync({
useFactory: () => ({
uri: 'mongodb://127.0.0.1:27017/test',
name: 'testing_connection',
})
}),
AppModule,
AuthModule,
UsersModule
]
}).compile();
毕竟我有
afterAll(async () => {
await (app.get(getConnectionToken()) as Connection).db.dropDatabase();
await app.close();
});
在测试期间,我可以在测试数据库上写入,但毕竟我删除了主连接。 如果我检查app.get(getConnectionToken()
我可以看到我有两个连接,第一个是我在app.module
上创建的连接,第二个应该是测试连接。
问题是,我怎样才能删除正确的数据库? 我可以在getConnectionToken
中传递一个参数,我尝试使用testing_connection
但没有幸运
您应该在beforeEach
或beforeAll
function 中添加await app.init()
来初始化 Nest 应用程序。如果您使用单连接尝试:
const connection = app.get(getConnectionToken()); "DatabaseConnection"
await connection.dropDatabase();
如果您使用多个连接,则应将getConnectionToken()
替换为连接名称(在您的情况下为testing_connection
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.