Mongoose: Unit testing process not exiting due to mangoose ?

Created on 16 Oct 2017  路  3Comments  路  Source: Automattic/mongoose

Hi
4.12.0

I added some Mocha/Chai test to my working app (Express+mangoose) and I noticed something that was not visible running the app.

Issue
When running my unit tests, the process do never exit and is stuck idle due to mangoose (apparently).

Nothing amazing in the code snippet below... I tried some mongoose#disconnect at the very end of my test but does not change anything..

Could you please advice on what is blocking the process exit ?

const mongoose = require('mongoose');
import { databaseConfig } from './config/database';

export function setupDatabase() {
  mongoose.Promise = require('bluebird');
  //mongoose.set("debug", true)
  var connection = mongoose.connect(databaseConfig.database, { useMongoClient: true }, (err) => { console.log("Connect error: " + err) });

  mongoose.connection.on('connected', () => {
    console.log('Connected to database ' + databaseConfig.database);
  });

  mongoose.connection.on('error', (err) => {
    console.log('Database error: ' + err);
  });

  return mongoose;
}

Most helpful comment

Forcing the process to exit is just a workaround. The issue is that it seems like if you have multiple connections open and use mongoose.disconnect() it still leaves a socket open.

All 3 comments

everytime i used the mongoose.connect, i always get these warning

DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection(). See http://mongoosejs.com/docs/connections.html#use-mongo-client

im on node: 8.7, mongoose: 4.12.2

try this,

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
import { databaseConfig } from './config/database';


async function setupDatabase() {
  // mongoose.set('debug', true);
  return await mongoose.connection.openUri(databaseConfig.database);
}

(async function () {
  const conn = await setupDatabase();
  // closing instance
  await conn.close();
}());



Well, my bad this was not mangoose fault.

I am using mocha and had to add --exit parameter to force the call to process.exit.

I guess without this parameter mocha just... does nothing at the end of the test and waits.

Forcing the process to exit is just a workaround. The issue is that it seems like if you have multiple connections open and use mongoose.disconnect() it still leaves a socket open.

Was this page helpful?
0 / 5 - 0 ratings