title: Nestjs / Typeorm rollback database state using transaction after each test
body: <p>I am trying to use Nestjs/Typeorm with transactions to start and rollback database state before/after each e2e test.</p>
  
  <p>I have included a code snippet attempt below. I am trying to override the EntityManager provider so that it is initialised with a QueryRunner instance so that I can start and rollback a transaction before/after each test. However, I cannot seem to get the repository (see comments in beforeEach method in code snippet below) to use my overridden EntityManager instance, to enable successful transaction usage.....I think this is why the transaction is not rolling back after each test has completed??</p>
  
  <pre class="lang-js prettyprint-override"><code>  let app: INestApplication;
    let testModule: TestingModule;
  
    afterEach(async () =&gt; {
      const em: EntityManager = testModule.get(getEntityManagerToken('default'));
      await em.queryRunner.rollbackTransaction();
    });
  
    beforeEach(async () =&gt; {
      const con: Connection = testModule.get(Connection);
      const em: EntityManager = testModule.get(getEntityManagerToken('default'));
      const repo: CourseRepository = testModule.get(CourseRepository);
      const result: boolean = repo.isEntityManagerMine(em); // false =&gt; the repo is not using the default entity manager
      const conResult: boolean = repo.isConnectionMine(em.connection); // true =&gt; the repo is using the same connection
      await em.queryRunner.startTransaction();
    });
  
    afterAll(async() =&gt; {
      await app.close();
      await testModule.close();
    });
  
    beforeAll(async () =&gt; {
      testModule = await Test.createTestingModule({
        imports: [AppModule],})
      .overrideProvider(getEntityManagerToken('default'))
      .useFactory({
        factory: (connection: Connection): EntityManager =&gt; {
          const queryRunner: QueryRunner = connection.createQueryRunner('master');
          const entityManager: EntityManager = connection.createEntityManager(queryRunner);
          return entityManager;
        },
        inject:[getConnectionToken('default')],
      })
      .compile();
  
      app = testModule.createNestApplication();
      await app.init();
    });
  
   // tests using request from supertest library
  </code></pre>