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
.
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.
My run/debug configuration on Webstorm 11 is like below.
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();
});
});
});
});