1
votes

I am using setTimeout in mocha test suite to insert a 20 seconds delay before making the last post call of the it() in the describe block. Although, I am using done() , still I am getting below error on terminal :

error: timeout of 2000ms exceeded. for async tests and hooks, ensure "done()" is called; if returning a promise, ensure it resolves error: timeout of 2000ms exceeded. for async tests and hooks, ensure "done()" is called; if returning a promise, ensure it resolves

What am I doing wrong?

Below is my code :

describe('Testing get and post APIs', ()=> {

            it('Series of get and post', (done) => {
                chai.request(server)
                .post('/thisis/1st_post')
                .send()
                .end((err, res) => {
                 expect(res.statusCode).to.equal(200);                                 

                 chai.request(server)
                .get('/thisis/1st_get')
                .send()
                .end((err, res) => {
                 expect(res.statusCode).to.equal(200);
                 setTimeout(function() {
                       chai.request(server)
                      .post('/thisis/last_post')
                      .send()
                      .end((err, res) => {
                      expect(res.statusCode).to.equal(200); 
                      done();
                 })
                 },20000);  
               }); 
             });     
        });
});

Thanks.

2

2 Answers

0
votes

If you want the test runner to wait longer than the default time you'll need to change the timeout value. For example, try adding this at the beginning of your describe block:

this.timeout(30 * 1000); // wait up to 30 seconds before failing from timeout

See also: Change default timeout for mocha & https://mochajs.org/api/test#timeout

However, depending on the reason for your desired 20 second delay, extending the test timeout may be the wrong solution. If you're using the delay just to handle cases where the request promise never resolves then a better approach would be to use Promise.race. Without understanding your situation more it's hard for me to judge though.

0
votes

The timeout is set to 20000 (20 seconds) but the test timeout based on error is 2000 (2 seconds). It means that we need to set larger timeout for the test itself.

describe('Testing get and post APIs', function() { // don't use () because we want to use `this` in the next line
  this.timeout(40000); // set timeout here

  it('Series of get and post', function(done) {
    chai.request(server)
      .post('/thisis/1st_post')
      .send()
      .end((err, res) => {
        expect(res.statusCode).to.equal(200);

        chai.request(server)
          .get('/thisis/1st_get')
          .send()
          .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            setTimeout(function () {
              chai.request(server)
                .post('/thisis/last_post')
                .send()
                .end((err, res) => {
                  expect(res.statusCode).to.equal(200);
                  done();
                })
            }, 20000);
          });
      });
  });
});

I wonder whether we can do the test like below. It is cleaner and more maintainable.

 describe('Testing get and post APIs', function () { // don't use () because we want to use `this` in the next line
  this.timeout(40000); // set timeout here

  it('Series post', function () { // no need done() because we can just return promise
    return chai.request(server)
      .post('/thisis/1st_post')
      .send()
      .end((err, res) => {
        expect(res.statusCode).to.equal(200);
      })
  });

  it('Series get', function () {
    return chai.request(server)
      .get('/thisis/1st_get')
      .send()
      .end((err, res) => {
        expect(res.statusCode).to.equal(200);
      });
  });

  it('Series last post', function(done) {
    setTimeout(function () {
      chai.request(server)
        .post('/thisis/last_post')
        .send()
        .end((err, res) => {
          expect(res.statusCode).to.equal(200);
          done();
        });
    }, 20000);
  });
});

Hope it helps.