首页 > 解决方案 > 如何将调试器附加到在 INestApplication.getHttpServer() 返回的服务器上运行的代码

问题描述

我正在运行一个用 NestJS 构建的应用程序。我有使用 supertest 库编写的集成测试。我使用 Jest 作为我的测试框架/测试运行器。我的集成测试看起来像这样。

describe('Item controller', () => {

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

    app = moduleRef.createNestApplication();
    await app.init();

    test('/Delete endpoint returns 404 if item does not exist', async () => {
        const nonExistentId = '081512e0-3d32-45cb-926c-f5140b99ca85'
        req(app.getHttpServer())
        .delete(`/items/${nonExistentId}`)
        .set('X-Company-Id', company.id)
        .expect(404)
      });
});

在 Visual Studio 代码中工作,我可以运行调试器并在测试中设置断点并逐行执行。但是,我需要能够单步执行在端点运行的代码。有什么办法可以将调试器附加到服务器上运行的代码上,该代码由app.getHttpServer().

在我没有使用超级测试通过请求到达端点之前。我正在创建实际控制器的实例并调用该函数。但是,这绕过了中间件。

标签: javascriptnode.jsjestjsnestjssupertest

解决方案


推荐阅读