mongodb - multiple tasks in nodeunit with mongo fail -


i've taken how asynchronous result node unit , mongoose? , modified simpler show failure.

var mongoose = require('mongoose');

var db;  module.exports = {     setup: function(callback) {         try {             //db.connection.on('open', function() {             mongoose.connection.on('open', function() {                 console.log('opened connection');                 callback();             });              db = mongoose.connect('mongodb://localhost/test_1');             console.log('started connection, waiting open');         } catch (err) {             console.log('setting failed:', err.message);             test.done();             callback(err);         }     },      teardown: function(callback) {         console.log('in teardown');         try {             console.log('closing connection');             db.disconnect();             callback();         } catch (err) {             console.log('tearing down failed:', err.message);             test.done();             callback(err);         }     },      test1: function(test) {         test.iferror(null);         test.done();     },     test2: function(test) {         test.iferror(null);         test.done();     } }; 

when running nodeunit following:

stam2_test.js started connection, waiting open opened connection in teardown closing connection ✔ test1 started connection, waiting open opened connection  failures: undone tests (or setups/teardowns):  - test2  fix this, make sure tests call test.done() 

some more info: if in setup/teardown don't user mongo test code, increasing counter, works. if have 1 test, works. adding test , having mongo in setup consistently fails guess i'm doing wrong in setup.

thank in advance.

the reason failure seems event subscription in mongoose.connection.on('open',...) remains bound callback test1 after disconnect , connect test2. call previous callback 1 causing trouble.

you should make sure remove subscription somehow when done it. since mongoose connection based on nodejs eventemitter simple solution might replace call mongoose.connection.on('open'...) mongoose.connection.once('open'...) use general add/removelistener() needed.

on different note, seems unneeded connect , disconnect in each test of unit test. connect once requiring module connects test database in require('db_connect_test'), module db_connect_test should call mongoose.connect(...) , tests run same connection (or pool mongoose creates).

have one!


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -