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

Viewed 1070

I want to save some data out of a body and use it with the next request in my tests. I'm doing a post request and get back an id. This id I want to use to fetch data in other test.

My code looks like this:

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

The token is getting set and the code runs, however my jest test won't stop with and I'll get the error:

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.

I know that I can do forceExit but I want to do it clean and understand the problem.

When I do the jest return method, it's working.. However there Idk how to store the body somewhere..

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

I resolved this problem by closing the database connection when the application is shutting down

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();
  }
}

```
Related