Search code examples
node.jschaiasync.jschai-http

chai-http and async.each, throwing "Timeout of 2000ms exceeded..."


I have a simple test with chai-http, in which I try to test several URLs using async.each, but when the request takes more than 2 seconds, then I got the error.

it("it should GET the required images", (done) => {
    async.each(get_data, function(item, cb){
      chai
        .request(item.server_url.S)
        .get('/'+ item.endpoint.S + '?' + item.incoming.S)
        .end(function(err, res) {
          if(err) console.error(err);
          expect(err).to.be.null;
          expect(res).to.have.status(200);
          cb();
        });
    }, function(err){
      if(err) console.log(err);
      done();
    });
  });

I'm calling "done" as I think is correct, but I keep getting the error, what am I doing wrong? The error is showing even with no async, just the simple chai request, with only one request... so pretty sure is not an async issue but me using chaiHttp bad.

I also tried with "then/catch" instead of "end", but the result is the same.

I have a similar issue, in the same testing script but with the DB, if it takes more than 2 seconds with the query, it breaks... same error, also using "done":

before((done) => {
  // runs before all tests in this block
  const params = {
    TableName: "mytable"
  };

  mydb.scan(params, (err, records) => {
    if(err) console.log(err);
    for(let i = 0; i < records.Items.length; i++){
      //...some ifs, nothing async
    }
    done();
  });
});

Solution

  • If your test takes longer than 2000ms, consider to extend the timeout for your test might solve your problem

    it("it should GET the required images", (done) => {
       this.timeout(5000);
       //...