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

Unit testing process not exiting due to mangoose ? #5715

Closed
ghost opened this issue Oct 15, 2017 · 3 comments
Closed

Unit testing process not exiting due to mangoose ? #5715

ghost opened this issue Oct 15, 2017 · 3 comments

Comments

@ghost
Copy link

ghost commented Oct 15, 2017

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;
}
@binarytracer
Copy link

binarytracer commented Oct 16, 2017

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

@ghost
Copy link
Author

ghost commented Oct 16, 2017

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.

@ghost ghost closed this as completed Oct 16, 2017
@andreyrd
Copy link

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.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants