How to split jest e2e tests into multiple files without losing context?

Viewed 392

So I've written plenty of e2e tests for my backend and this is becoming overwhelming as all of test methods are in one file.

Reason I have all of them in one file is that when my app is created, TypeORM creates in-memory database instance on which I do all of the tests - I need same database to be running across tests as I am doing cross-entities tests.

This part of code is crucial. It initializes app (which also initializes db under the hood):

let app: INestApplication;

beforeAll(async () => {
  const moduleFixture = await Test.createTestingModule({
    imports: [AppModule],
  }).compile();

  app = moduleFixture.createNestApplication();
  await app.init();
});

Is there a way to somehow transfer beforeAll()'s context so that it could be accessed from tests located in other files?

Maybe somehow make app global?

1 Answers

Ok so kind of a solution is to instead of creating in memory database, you can create a sqlite database and force TypeORM to create a file (which will be your database) by using:

export const e2eConfig: SqliteConnectionOptions = {
  type: 'sqlite',
  database: 'db.db',
  entities: entities,
  synchronize: true,
};

(sqlite is mostly compatible with mysql)

This will make your data persist between tests.

Related