【问题标题】:NestJS + Jest: how to receive external http requests within integration test?NestJS + Jest:如何在集成测试中接收外部 http 请求?
【发布时间】:2022-01-06 23:58:37
【问题描述】:

目标

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

  • 将预定义的用户输入发送给 A
  • 收到来自 A 的 POST
  • 做一些处理
  • 验证处理

在 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 不提供“监听”功能。我也没有使用通过NestFactory.create(IntegrationTestModule) 创建的预定义普通模块使其工作。

欢迎提出任何建议!

【问题讨论】:

标签: jestjs controller nestjs integration-testing code-coverage


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2019-06-11
    • 2019-08-19
    • 2017-01-12
    • 2021-12-22
    • 2022-01-06
    • 2015-04-10
    • 2020-05-15
    相关资源
    最近更新 更多