NestJS + Jest:如何在集成测试中接收外部 http 请求?

NestJS + Jest: how to receive external http requests within integration test?

目标

在我的设置中,我有两个应用程序。应用程序 A(来自第三方)接收用户输入,对其进行处理,然后通过 POST 将处理后的数据发送到应用程序 B(我的 nestjs 服务器)。我想 运行 一个集成测试,我想在其中根据用户输入验证应用程序 B 的内部变量。为此,我想在 B 中定义执行以下步骤的测试:

在 B 中定义测试应该可以检查集成测试的代码覆盖率。如果我编写另一个从外部测试的应用程序,我将无法执行此操作。

问题

我没有找到如何在玩笑测试中接收外部 http 请求的方法。我的方法是创建一个带有控制器的模块,然后监听端口。比如像这样

describe('Integration Test', () => {
    beforeAll(async () => {
        const testingModule: TestingModule = await Test.createTestingModule(
            {
                imports: [HttpModule],
                providers: [IntegrationTestService],
                controllers: [IntegrationTestController],
            },
        ).compile();

        await testingModule.listen(3000);
        // here I define my tests
    });
}

但是TestingModule没有提供“listen”的功能。我也没有使用通过 NestFactory.create(IntegrationTestModule).

创建的预定义普通模块使其工作

如有任何建议,我们将不胜感激!

要运行测试服务器上一个端口,需要调用testingModule.createNestApplication()like it shows in the docs under e2e tests。将此添加到 beforeAll() 的末尾将允许您接收 http 请求

const app = testingModule.createNestApplicaiton();
await app.listen(3000);