Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

nodeunit fail to exit from my asynchronous tests

nodeunit fail to exit from my asynchronous tests

Problem

Whenever I run my nodeunit test in IDE or console, it run well but fail to exit. Help me please with it!

var store = require('../lib/db');
var list = require('../source/models/mock_deals');
var logger = require('../lib/logging').logger;

exports.setUp = function(done){
    logger.info('start test...');
    done();
};
exports.tearDown = function(done){
    logger.info('end test...');
    done();
};

exports.testInsertDeal = function(test){
    var length = list.length;
    test.equals(length, 2);
    store.mongodb.open(function(err,db){
        if(err){
            logger.error(err);
            return;
        }
        logger.info("mongodb is connected!");

        db.collection('deals',function(err,collection){
            for(var i=0; i
Problem courtesy of: Henry Leu

Solution

I changed to use mongoose instead of mongodb. test still could not exit automatically.

But when I disconnected mongoose in test.tearDown method in my nodeunit test. the test existed correctly.

Add below in you test:

exports.tearDown = function(done){
    mongoose.disconnect(function(err){
        if(err) {
            logger.error(err);
            return;
        }
        logger.info('mongoose is disconnected');
    });
    done();
};

And more, If I use log4js for logging in my test and configure log4js with reloadSecs: 500 , test will not exist either. After I set reloadSecs to 0, then test exists well. So we need to configure logging.json with option reloadSecs: 0

To summarize: we need to make sure there are no working parts there after all test methods are done. then test will exist correctly.

Solution courtesy of: Henry Leu

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

nodeunit fail to exit from my asynchronous tests

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×