Question

I've taken How do I get an asynchronous result back with node unit and mongoose? and VERY slightly modified it to be simpler to show my 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 for it to open');
        } catch (err) {
            console.log('Setting up 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 this with nodeunit I get the following:

stam2_test.js
Started connection, waiting for it to open
Opened connection
In tearDown
Closing connection
✔ test1
Started connection, waiting for it to open
Opened connection

FAILURES: Undone tests (or their setups/teardowns): 
- test2

To fix this, make sure all tests call test.done()

Some more info: If in the setUp/tearDown i don't user mongo but just a test code, like increasing a counter, it all works. If I have only one test, everything works. Adding another test AND having mongo in the setup consistently fails it so I guess I'm doing something wrong in the setup.

Thank you in advance.

Était-ce utile?

La solution

The reason for the failure seems that the event subscription in mongoose.connection.on('open',...) remains bound to the callback from test1 even after disconnect and connect for test2. The extra call to the previous callback is the one causing trouble.

You should make sure to remove the subscription somehow when you are done with it. Since mongoose connection is based on nodejs EventEmitter then a simple solution might be to replace the call mongoose.connection.on('open'...) with mongoose.connection.once('open'...) but you could also use the general add/removeListener() as needed.

On a different note, it seems unneeded to connect and disconnect in each test of a unit test. You could just connect once by something like requiring a module that connects to your test database as in require('db_connect_test'), the module db_connect_test should just call mongoose.connect(...) and all the tests would run with the same connection (or pool as mongoose creates).

Have a good one!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top