如何将Jest E2E测试分为多个文件而不会丢失上下文?

发布于 2025-01-25 08:49:07 字数 556 浏览 2 评论 0原文

因此,我为我的后端编写了大量的E2E测试,并且由于所有测试方法都在一个文件中,因此这变得越来越不知所措。

我将所有这些都放在一个文件中的原因是,当创建我的应用程序时,Typeorm创建了内存数据库实例,我可以在其中进行所有测试 - 我需要与进行跨境测试相同的数据库来运行跨测试。

代码的这一部分至关重要。它初始化了应用程序(也在引擎盖下的DB初始化了DB):

let app: INestApplication;

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

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

有没有办法以某种方式传输beforeall()的上下文,以便可以从其他文件中的测试中访问它?

也许某种程度上使app全局?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2025-02-01 08:49:07

好的,一种解决方案是,您可以创建一个sqlite数据库,而不是在内存数据库中创建。使用:(

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

sqlite主要与mysql兼容)

这将使您的数据在测试之间持续存在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文