2

Based on Alexey B.'s comment, I revised my test codes and found the situation that causes a same error from my test codes. When I try to test a single test file, it works well. However, if I try to test several test files simultaneously, it is broken. The common error message is Error: Trying to open unclosed connection.. It seems that my code for DB connection has some problems.

Here is my revised code.

utils.js:

var mongoose = require('mongoose');
module.exports = function(models) {
    return function(done) {
        for(var i in models) {
            models[i].remove({}, function() {});
        }
        done();
    };
};

user.server.model.tests.js:

var should = require('should'),
    mongoose = require('mongoose'),
    utils = require('./utils');

require('../config/mongoose')();
var User = mongoose.model('User'),
    user;

describe('User Model Tests:', function() {
    afterEach(utils([User]));

    describe('#create()', function() {
        beforeEach(function(done) {
            user = new User({
                email:'test1@test.com',
                username: 'test1',
                password: '1234'
            });
            done();
        });

        it('create a new user', function(done) {
            user.save(function(err, user) {
                should.not.exist(err);
                user.email.should.equal('test1@test.com');
                user.username.should.equal('test1');
                done();
            });
        });

        it('create a new user with an existing email', function(done) {
            user.save(function(err) {
                should.not.exist(err);
            });

            var userDUP = new User({
                email:'test1@test.com',
                username:'test2',
                password: '1234'
            });

            userDUP.save(function(err) {
                should.exist(err);
                done();
            });
        });
    });
});

product.server.model.tests.js:

var should = require('should'),
    mongoose = require('mongoose'),
    utils = require('./utils');

require('../config/mongoose')();

var Product = mongoose.model('Product'),
    User = mongoose.model('User');

describe('Product Model Tests:', function(){
    afterEach(utils([User, Product]));
    describe('#create()', function(){
        it('create a new product', function(done) {
            var user = new User({
                email:'test@test.com',
                username: 'test',
                password: '1234'
            });

            user.save(function(err) {
                should.not.exist(err);
            });

            var product = new Product({
                name: 'Product1',
                user: user
            });

            product.save(function(err, product) {
                should.not.exist(err);
                User.findOne({'_id':product.user}, function(err, user) {
                    should.not.exist(err);
                    user.username.should.equal('test');
                });
                product.name.should.equal('Product1');
                product.ordered.should.equal(0);
                product.stock.should.equal(0);

                done();
            });
        });

        it('create a new product without a user', function(done) {
            var product = new Product({
                name: 'Product'
            });

            product.save(function(err){
                should.exist(err);
                done();
            });
        });
    });
});

I have two more test files, but their structures are same.

Also, my conection to DB is defined in ../config/mongoose.js. Here is the code.

var config = require('./config'),
    mongoose = require('mongoose');

module.exports = function() {
    var db = mongoose.connect(config.db);
    console.log('MongoDB is successfully connected.');

    require('../models/user.server.model');
    require('../models/product.server.model');
    require('../models/sale.server.model');
    require('../models/dcompany.server.model');
    require('../models/customer.server.model');

    return db;
};

I tried to connect the DB using createConnect rather than connect, but it raised another error called timeout.

Below are the old version of this question.

I have two test files ('product.server.model.tests.js' and 'user.server.model.tests.js') and both calls 'utils.js' that contains beforeEach and afterEach where connecting/disconnecting a DB has been done.

When I make a run/debug configuration of Mocha testing and try to test them on Webstorm 11, the test is broken with an error (Error: Trying to open unclosed connection.) like below. It happens when Mocha tries to test the user.server.model.js.

enter image description here

However, when I run these tests on the terminal, it passes all tests! (see below) Also, it has no problem if I make separate run/debug configurations for each test file.

enter image description here

My run/debug configuration on Webstorm 11 is like below.

enter image description here

Is it kinda bug of Webstorm 11? Or do I something wrong on setting run/debug configuration or my test codes? I attached my test codes below.


utils.js:

var mongoose = require('mongoose');

beforeEach(function(done) {
    require('../config/mongoose')();

    for(var i in mongoose.connection.collections) {
        mongoose.connection.collections[i].remove(function() {});
    }

    done();
});

afterEach(function(done) {
    mongoose.disconnect();
    done();
});

user.server.model.test.js:

require('./utils');
var should = require('should'),
    mongoose = require('mongoose');

describe('User Model Tests:', function() {
    describe('#create()', function() {
        it('create a new user', function(done) {
            var User = mongoose.model('User');

            var user = new User({
                email:'test1@test.com',
                username: 'test1',
                password: '1234'
            });

            user.save(function(err, user) {
                should.not.exist(err);
                user.email.should.equal('test1@test.com');
                user.username.should.equal('test1');
                done();
            });
        });

        it('duplication: email', function(done) {
            var User = mongoose.model('User');

            var user = new User({
                email:'test1@test.com',
                username: 'test1',
                password: '1234'
            });

            user.save(function(err) {
                should.not.exist(err);
            });

            var userDUP = new User({
                email:'test1@test.com',
                username:'test2',
                password: '1234'
            });

            userDUP.save(function(err) {
                should.exist(err);
                done();
            });
        });
    });
});

product.server.model.tests.js:

require('./utils');
var should = require('should'),
    mongoose = require('mongoose');

describe('Product Model Tests:', function(){
    describe('#create()', function(){
        it('create a new product', function(done) {
            var Product = mongoose.model('Product');
            var User = mongoose.model('User');

            var user = new User({
                email:'test@test.com',
                username: 'test',
                password: '1234'
            });

            user.save(function(err) {
                should.not.exist(err);
            });

            var product = new Product({
                name: 'Product1',
                user: user
            });

            product.save(function(err, product) {
                should.not.exist(err);
                User.findOne({'_id':product.user}, function(err, user) {
                    should.not.exist(err);
                    user.username.should.equal('test');
                });
                product.name.should.equal('Product1');
                product.ordered.should.equal(0);
                product.stock.should.equal(0);

                done();
            });
        });

        it('create a new product without a user', function(done) {
            var Product = mongoose.model('Product');

            var product = new Product({
                name: 'Product'
            });

            product.save(function(err){
                should.exist(err);
                done();
            });
        });
    });
});
byron1st
  • 969
  • 1
  • 13
  • 35

3 Answers3

1

I found that catching error resolves this issue for me. It's probably a mocha bug? Anyways this works and it is not worth my time to investigate further. I hope this unblocks someone else.

Notice how I pass a callback into the connect function:

var mongoose = require('mongoose');

...

before(function (done) {
    mongoose.connect('mongodb://localhost/test', function(err) {
        done();
    });
});

after(function (done) {
    mongoose.connection.close();
    done();
});

describe('some tests', function() {
   it('can do something', function (done) {
   });
})
LLL
  • 1,085
  • 8
  • 16
0

Cant tell exactly that is the problem here, but is a bad practice to implement mocha hooks in separate file. For example, then you add more tests to your project, you cant disable that hooks for some tests which doesnt require a database connection.

Right way here is to make some helper module

module.exports = {
    connect: function (done) {
        require('../config/mongoose')();

        for(var i in mongoose.connection.collections) {
            mongoose.connection.collections[i].remove(function() {});
        }

        done();
    },

    disconnect: function (done) {
        mongoose.disconnect();
        done();
    }
};

And applying it in tests

var should = require('should'),
    mongoose = require('mongoose'),
    helper = require('./utils');

describe('User Model Tests:', function() {
    beforeEach(helper.connect);
    afterEach(helper.disconnect);
    describe('#create()', function() {
        ...
    });
});
Alexey B.
  • 11,965
  • 2
  • 49
  • 73
0

I refactored my test code referring to the question "joining tests from multiple files with mocha.js". I built a test.js to join all different test files and, at the beginning of test.js, connecting to the DB has been tried. Here is my working test codes.

test/test.js:

var mongoose = require('mongoose');

var importTest = function(name, path) {
    describe(name, function() {
        require(path);
    });
};

//Connecting to the DB
require('../../app/config/mongoose')();

describe("Model Testing:", function() {
    before(function() {
        console.log('Start a model testing!\n');
    });

    importTest('User Model Testing:', './models/user.server.model.tests');
    importTest('Sale Model Testing:', './models/sale.server.model.tests');
    importTest('Product Model Testing:', './models/product.server.model.tests');
    importTest('DCompany Model Testing:', './models/dcompany.server.model.tests');

    after(function() {
        mongoose.disconnect();
        console.log("Disconnect to the DB.\n");
    })
});

/test/models/user.server.model.tests.js:

var should = require('should'),
    mongoose = require('mongoose'),
    utils = require('./utils');

var User = mongoose.model('User'),
    user;

afterEach(utils([User]));
describe('#create()', function() {
    beforeEach(function(done) {
        user = new User({
            email:'test1@test.com',
            username: 'test1',
            password: '1234'
        });
        done();
    });

    it('create a new user', function(done) {
        user.save(function(err, user) {
            should.not.exist(err);
            user.email.should.equal('test1@test.com');
            user.username.should.equal('test1');
            done();
        });
    });

    it('create a new user with an existing email', function(done) {
        user.save(function(err) {
            should.not.exist(err);
        });

        var userDUP = new User({
            email:'test1@test.com',
            username:'test2',
            password: '1234'
        });

        userDUP.save(function(err) {
            should.exist(err);
            done();
        });
    });
});

Other test files have the structure similar with user.server.model.tests.js.

Community
  • 1
  • 1
byron1st
  • 969
  • 1
  • 13
  • 35