nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
529 stars 118 forks source link

Closing Connection after used #128

Closed binarytracer closed 5 years ago

binarytracer commented 5 years ago

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

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

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?

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 commented 5 years ago

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

binarytracer commented 5 years ago

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

Kronhyx commented 2 years ago

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

lunkums commented 1 year ago

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