Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing Connection after used #128

Closed
binarytracer opened this issue Aug 20, 2019 · 4 comments
Closed

Closing Connection after used #128

binarytracer opened this issue Aug 20, 2019 · 4 comments

Comments

@binarytracer
Copy link

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

  • test runner cannot exit gracefully,

file: src/app/app.controller.spec.ts

import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AuthModule } from '../auth/auth.module';
import { UsersModule } from '../users/users.module';
import { MongooseModule} from '@nestjs/mongoose';
import {dbConn, dbUser, dbPass } from './app.constants';

describe('AppController', () => {
  let appController: AppController;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [AppController],
      providers: [],
      imports: [
        AuthModule,
        UsersModule,
        MongooseModule.forRoot(dbConn, {
            useNewUrlParser: true,
            user: dbUser,
            pass: dbPass,
          })
        ]
    }).compile();

    appController = app.get<AppController>(AppController);
  });

  describe('root', () => {
    it('should return "Hello World!"', () => {
      expect(true).toBe(true);
    });
  });

});

result:
image

Expected behavior

  • It should terminate gracefully.

Minimal reproduction of the problem with instructions

running npm script again,

npm test 

with adjustments

import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AuthModule } from '../auth/auth.module';
import { UsersModule } from '../users/users.module';
import { MongooseModule} from '@nestjs/mongoose';

import {connections} from 'mongoose'; // added
import {dbConn, dbUser, dbPass } from './app.constants';

describe('AppController', () => {
  let appController: AppController;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [AppController],
      providers: [],
      imports: [
        AuthModule,
        UsersModule,
        MongooseModule.forRoot(dbConn, {
            useNewUrlParser: true,
            user: dbUser,
            pass: dbPass,
          })
        ]
    }).compile();

    appController = app.get<AppController>(AppController);
  });

  describe('root', () => {
    it('should return "Hello World!"', () => {
      expect(true).toBe(true);
    });
  });

 // added
  afterEach( (done) => {
    connections[1].close(() => {
      done();
    });
  })
});

result
image

What is the motivation / use case for changing the behavior?

  • to be able to close the db connection gracefully.
  • Maybe exposing this method this will be help or the module itself.

Environment


For Tooling issues:
[System Information]
- OS Version     : Linux 4.15
- NodeJS Version : v12.8.1
- NPM Version    : 6.10.3

[Nest Information]
- platform-express version : 6.0.0
- mongoose version         : 6.1.2
- passport version         : 6.1.0
- common version           : 6.0.0
- core version             : 6.0.0
- jwt version              : 6.1.1

@kamilmysliwiec
Copy link
Member

You should close your Nest application in either after or afterEach hook (app.close())

@binarytracer
Copy link
Author

@kamilmysliwiec its working and more simple way than i thought.

@Kronhyx
Copy link

Kronhyx commented Sep 23, 2022

@binarytracer How did you do it? can you share your piece of code solving this problem?

@lunkums
Copy link

lunkums commented Jul 28, 2023

@Kronhyx

You should close your Nest application in either after or afterEach hook (app.close())

Closing the application in afterAll did not work for me, and VSCode could not find after. I had to use afterEach:

 afterEach(async () => {
    // If you don't call this, then the tests will hang
    await app.close();
  });

@nestjs nestjs locked and limited conversation to collaborators Jul 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants