Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nestjs/Typeorm在每次测试后使用事务回滚数据库状态_Nestjs_Typeform - Fatal编程技术网

Nestjs/Typeorm在每次测试后使用事务回滚数据库状态

Nestjs/Typeorm在每次测试后使用事务回滚数据库状态,nestjs,typeform,Nestjs,Typeform,我正在尝试使用Nestjs/Typeorm和事务在每次e2e测试之前/之后启动和回滚数据库状态 我在下面包含了一个代码段尝试。我试图覆盖EntityManager提供程序,以便使用QueryRunner实例初始化它,以便在每次测试之前/之后启动和回滚事务。但是,我似乎无法让存储库(请参阅下面代码片段中beforeEach方法中的注释)使用我的重写EntityManager实例,以成功使用事务…..我认为这就是为什么在每个测试完成后事务不会回滚的原因 let-app:不适用; let testMo

我正在尝试使用Nestjs/Typeorm和事务在每次e2e测试之前/之后启动和回滚数据库状态

我在下面包含了一个代码段尝试。我试图覆盖EntityManager提供程序,以便使用QueryRunner实例初始化它,以便在每次测试之前/之后启动和回滚事务。但是,我似乎无法让存储库(请参阅下面代码片段中beforeEach方法中的注释)使用我的重写EntityManager实例,以成功使用事务…..我认为这就是为什么在每个测试完成后事务不会回滚的原因

let-app:不适用;
let testModule:TestingModule;
每次之后(异步()=>{
const em:EntityManager=testModule.get(getEntityManagerToken('default');
等待em.queryRunner.rollbackTransaction();
});
beforeach(异步()=>{
const con:Connection=testModule.get(连接);
const em:EntityManager=testModule.get(getEntityManagerToken('default');
const repo:CourseRepository=testModule.get(CourseRepository);
const result:boolean=repo.isEntityManagerMine(em);//false=>repo未使用默认的实体管理器
const conResult:boolean=repo.isConnectionMine(em.connection);//true=>repo正在使用相同的连接
等待em.queryRunner.startTransaction();
});
毕竟(异步()=>{
等待app.close();
等待testModule.close();
});
之前(异步()=>{
testModule=await Test.createTestingModule({
导入:[AppModule],})
.overrideProvider(getEntityManagerToken('default'))
.useFactory({
工厂:(连接:连接):EntityManager=>{
const queryRunner:queryRunner=connection.createQueryRunner('master');
const entityManager:entityManager=connection.createEntityManager(queryRunner);
返回实体管理器;
},
inject:[getConnectionToken('default')],
})
.compile();
app=testModule.createNestApplication();
等待app.init();
});
//使用来自supertest库的请求进行测试

但是您肯定想要测试运行前的状态,所以不能删除数据库?!可能得走那条路。。。。尽管为了提高性能,我更愿意回滚事务,但不明白为什么存储库没有使用提供者提供的EntityManager实例……我使用
await getConnection().synchronize(true)
用于删除数据库,这对于我来说已经足够快了。也许可以尝试一下,如果你没有明显的性能损失,那就好了。谢谢@KimKern,可能需要尝试一下……但是你肯定想要测试运行前的状态,所以清除数据库不是一个选项?!可能得走那条路。。。。尽管为了提高性能,我更愿意回滚事务,但不明白为什么存储库没有使用提供者提供的EntityManager实例……我使用
await getConnection().synchronize(true)
用于删除数据库,这对于我来说已经足够快了。也许可以尝试一下,如果你没有明显的性能损失,那就很好了。谢谢@KimKern,可能需要尝试一下。。。