Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error " Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called;"

I am seeing following using mocha and chai library for test case. Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

here is code for testing handler for amazon lambda function.(for now , I am not using super-test npm module)

const expect = require('chai').expect;
const mongoose = require('mongoose');
const CONFIG_DEV_MONGODB = require('../../config/config.dev').DB_CONNECTION_URL;


describe('get the foo', () => {

    let handler;
    before(() => {

        process.env.DEPLOYMENT_STAGE = 'test';
        process.env.DB_CONNECTION_URL = CONFIG_DEV_MONGODB;
        handler = require('./get');
    });

    after(() => {

        if (mongoose.connection) {
            mongoose.connection.close();
        }
    });

    it('validate getFeaturedProducts get request with storeid',function(done){
        //request has the sample data for event
        let request = require('../../test/fixtures/featureProductJSON');
        handler.getFeaturedProducts(request, {} , (error, result) => {

             expect(error).to.be.null;
             expect(result).to.be.not.null;
             done()
         })

    })
});

Here is the handler

module.exports.getFeaturedProducts = function (event, context, callback) {
    ..............
    .......
    mongoConnection.then( function() {
         return callback(null, 'test');
     }, function (error) {

        return return callback(true, null);;
    })
 }

can any one explain what is happening wne

like image 613
made_in_india Avatar asked Nov 28 '22 06:11

made_in_india


1 Answers

Your tests are taking longer than Mocha expects them to take and timing out. By default, all callback functions timeout after 2000ms. You'll need to adjust the timeout of your test suite using this.timeout().

You can specify this at the suite level in your describe():

describe('get the foo', function () {
  this.timeout(10000) // all tests in this suite get 10 seconds before timeout

  // hooks & tests
})

You can specify this in a hook like before():

describe('get the foo', function() {
    before(function() {
      this.timeout(10000) // 10 second timeout for setup
    })

    // tests
})

You can also do this at the test level in your it()

describe('get the foo', function () {
  it('validate getFeaturedProducts get request with storeid', function (done) {
    this.timeout(10000) // 10 second timeout only for this test

    // assertions
  })
})
like image 117
peteb Avatar answered Dec 06 '22 11:12

peteb