【问题标题】:NestJS SuperTest Jest did not exit one second after the test run has completedNestJS SuperTest Jest 在测试运行完成后一秒没有退出
【发布时间】:2021-12-26 05:48:19
【问题描述】:

我想从正文中保存一些数据,并将其用于我的测试中的下一个请求。我正在做一个发布请求并取回一个ID。我想用这个 id 在其他测试中获取数据。

我的代码如下所示:

it('/auth/register (POST)', async () => {
   const test = await request(app.getHttpServer())
      .post('/api/auth/register')
      .send({username: "Zoe"})
      .expect(201)
   token = test.body.token
})

令牌正在设置并且代码运行,但是我的玩笑测试不会停止,我会收到错误:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

我知道我可以执行 forceExit,但我想彻底清除并理解问题。

当我做一个开玩笑的返回方法时,它正在工作..但是我知道如何将身体存储在某个地方..

it('/auth/register (POST)', () => {
   request(app.getHttpServer())
      .post('/api/auth/register')
      .send({username: "Zoe"})
      .expect(201)
})

【问题讨论】:

    标签: javascript testing jestjs nestjs supertest


    【解决方案1】:

    我通过在应用程序关闭时关闭数据库连接解决了这个问题

    import { Module } from '@nestjs/common';
    import { getConnection } from 'typeorm';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { DatabaseModule } from './database/database.module';
    import { FormsModule } from './forms/forms.module';
    
    @Module({
      imports: [
        DatabaseModule,
        FormsModule, 
      ],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {
      onApplicationShutdown(){
        getConnection().close();
      }
    }
    
    ```
    

    【讨论】:

      猜你喜欢
      • 2021-03-01
      • 1970-01-01
      • 2021-01-13
      • 2020-02-13
      • 2019-06-15
      • 2018-12-08
      • 2019-02-05
      • 2021-03-28
      • 2020-08-25
      相关资源
      最近更新 更多