Reputation: 75686
I get the following error in my mocha tests when mongoose tries to connect to mongodb:
Error: Trying to open unclosed connection.
Here is my test:
var cfg = require('../config')
, mongoose = require('mongoose')
, db = mongoose.connect(cfg.mongo.uri, cfg.mongo.db)
, User = require('../models/user')
, Item = require('../models/item')
, should = require('should')
, fakeUser
, fakeItem;
mongoose.connection.on('error', function(err){
console.log(err);
});
describe('User', function(){
beforeEach(function(done){
//clear out db
User.remove(done);
});
after(function(done){
//clear out db
User.remove(function(err){
Item.remove(done);
});
});
});
Upvotes: 4
Views: 3799
Reputation: 13200
Close the connection when done:
after(function(done){
//clear out db
User.remove(function(err){
Item.remove(function() {
mongoose.connection.close();
done();
});
});
});
Upvotes: 7