Ad

Can't Figure Out Why Mocha Test Is Passing

- 1 answer

I'm learning to do testing in javascript and I have this mocha test that I'm running

describe("Fetches Coordinates", function() {
        it("searches the database for coordinates", function() {
            var boundary = routes.setBoundries(20, 80, 20, 80)
            routes.searchCoords(boundary, function(err,data) {
                expect(data.length).to.equal(100)
            });
        });
    });

And this is the method that it is using

exports.searchCoords = function searchCoords(boundary, callback){
     models.sequelize.query('SELECT "data".longitude, "data".latitude, "data".ipscount FROM ('
                + ' SELECT * FROM "DataPoints" as "data"'
                + ' WHERE "data".longitude BETWEEN '
                + boundary.xlowerbound + ' and ' + boundary.xupperbound + ') data'
                + ' WHERE "data".latitude BETWEEN '
                + boundary.ylowerbound + ' and '
                + boundary.yupperbound + ';', { type: models.sequelize.QueryTypes.SELECT}).then(function(data) {
                    callback(data);
                });                 
}

When I run the test it seems like Mocha just skips over the callback and passes. I can't seem to get this right. What's the correct syntax?

Ad

Answer

Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is complete. By adding a callback (usually named done) to it() Mocha will know that it should wait for completion.

   it("searches the database for coordinates", function(done) {
        var boundary = routes.setBoundries(20, 80, 20, 80)
        routes.searchCoords(boundary, function(err,data) {
            expect(data.length).to.equal(100)
            done();
        });
    });
Ad
source: stackoverflow.com
Ad