Unit testing mongodb connection with mocha

I am new to NodeJS + mocha testing. I'm going to test the database connection.

test.js

var assert = require('chai').assert;
var db=require("../model/mongoDBhandler");

describe('Database', function(){
       it('connecting the database', function(done){
        db.connect();
        done();
    })
})

      

../mode/mongoDBHandler.js

var MongoClient = require('mongodb').MongoClient;

exports.connect=function(done){
    var url = 'mongodb://localhost:27017/testdb';
    // Use connect method to connect to the Server
    MongoClient.connect(url, function(err, db) {
        console.log("Connected correctly to server");
        db.close();
    });
    done();
    console.log("finished");
}

      

But when I run mocha it doesn't print "Connected correctly to the server" or "completed". I have tried various codes about async methods. But they didn't work. Maybe I'm wrong. Could you help me figure this out?

+3


source to share





All Articles