Ashan77 Ashan77 - 11 days ago 4
Javascript Question

Testing Promises with Mocha

I've been trying to test the following code using Mocha, but I always get the error.

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test


The code I want to test is as follows.

'use strict'
const Promise = require('bluebird');

const successResponse = {status: 'OK'};
const failResponse = {status: 'FAIL'};

function dbStatusSuccess () {
return new Promise(function(resolve, reject) {
setTimeout(() => {
resolve(successResponse);
}, 2010);

});
}

function dbStatusFail () {
return new Promise(function(resolve, reject) {
setTimeout(() => {
reject(failResponse);
}, 2000);

});
}

module.exports = {
dbStatusSuccess,
dbStatusFail
}


and here are my tests.

'use strict'
const Promise = require('bluebird');
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();


const healthyCheck = require('./healthyCheck');

const resp = {status:'OK'};
const resp2 ={status: 'FAIL'};

describe('healthy-check end point', () => {

it('should return successful response when connected to database', () => {

return healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
return expect(res).to.equal(resp);
}).catch( (err) => {
console.log(err);
return expect(err).to.deep.equal(resp2);
});


});
});


I also get an error { AssertionError: expected { status: 'OK' } to equal { status: 'OK' } in the console. Which I believe is from loggin the err from the .catch function.

EDIT 1.
Removed the reject function from dbStatusSuccess function.

The issue is with the promises taking 2 seconds to complete/fail. If the time set in setTimeout is less than 2 seconds, the tests will pass.

Answer

The default timeout in your test seem to be 2000ms. Your code obviously takes longer to complete. Therefor you have to up the timeout limit. As noted here you shouldn't use arrow functions so you can safely access this.

Then you can increase your timeout like so:

'use strict'
const Promise = require('bluebird');
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();


const healthyCheck = require('./healthyCheck');

const resp = {status:'OK'};
const resp2 ={status: 'FAIL'};

 describe('healthy-check end point', () => {

    it('should return successful response when connected to database', function() {
        this.timeout(3000);
        return healthyCheck.dbStatusSuccess()
                            .then((res) => {
                                console.log(JSON.stringify(res, undefined, 2));
                                return expect(res).to.equal(resp);
                            }).catch( (err) => {
                                console.log(err);
                                return expect(err).to.deep.equal(resp2);
                            });


    });
 });

Then your test should run as expected.

Comments