如何将Jest E2E测试分为多个文件而不会丢失上下文?
因此,我为我的后端编写了大量的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,一种解决方案是,您可以创建一个
sqlite
数据库,而不是在内存数据库中创建。使用:(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:(
sqlite
is mostly compatible withmysql
)This will make your data persist between tests.