Question

I am using Mocha and should for testing simple database queries, I am trying to run Async tests for simple Moongose schema functions, but I am getting timeout exceeded error everytime.

  Error: timeout of 15000ms exceeded
  at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14)
  at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

I have even used this.timeout(15000) and also tried --timeout 15000 in the mocha command but of no success, what ever timeout time I give I am getting this error. Only synchronous tests are passing. Below are my tests and function which I want to test.

My Mocha Test:-

   describe('#getFacility()', function () {
    this.timeout(15000);
    it('should get facility successfully', function (done) {
        db.getFacilites(Data.testFacility, function (err, facilities) {
            if (err) throw err;
            facilities.Name.should.equal(Data.testFacility.body.Name);
            done();
        })
    });
});

My Data:-

testFacility : {
    params:  { clientId:"51c85c3267b6fc1553000001" }
},

My Get Function

getFacilites: function (req, res) {
    Facility.find({Client: req.params.clientId}, function (err, facilities) {
        if(err) {
            res.send(500,err);
        } else if (!facilities) {
            res.send(404, "facilities not found");

        } else if (req.query.format && req.query.format === 'select') {
            var result = facilities.map (function (f) {
                return { value: f._id.toString(), text: f.Name }
            });
            res.json(result);

        } else {
            console.log("Cannot Retrieve Facilities");
        }
    });
}

I even created a new separate function for query too, but it is still not working. any Ideas that function looks like this.

describe('#getFacility() direct from DB', function () {
    it('should get facility successfully from db', function (done) {
        Client_data.Facility.find({Client: Data.testFacility.params.clientId}, function(err, facilities) {
            if (err) throw (err);
            if (facilities) {
                facilities.forEach(function (f) {
                    console.log(f);
                });
                done();
            }
        });
    });
});

If I try to call done() callback after the query, test passes, but that's also not look good to me.

describe('#addFacility()', function () {
    it('should add facility successfully', function (done) {
        API_Calls.addFacility(Data.testFacility, function (doc) {
            doc.Name.should.equal(Data.testFacility.body.Name);
        });
        done();
    });
});
Was it helpful?

Solution

Your getFacilities is taking a req, res and next and you're passing it something totally different in your test (a testFacility object and a callback).

I think your getFacilities method definition should not be taking req, res and next, maybe a clientId and next only and then depending on the callback of next you can create the appropriate response.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top