How to avoid timeouts in mocha tests?

Here I attach my code, I pass the callback and using supertest for the request. Since I use assert / expect in my test file inside the request.end block, why do I need to worry about a timeout? What am I doing here.

it('should get battle results ', function(done) {
    request(url)
      .post('/compare?vf_id='+vf_id)
      .set('access_token',access_token)
      .send(battleInstance)
      .end(function(err, res){  // why need timeout
        if (err) return done(err);
        console.log(JSON.stringify(res.body));
        expect(res.body.status).to.deep.equal('SUCCESS');
        done();
      });
 });

Test results after the answer: Error: Exceeded 2000 ms. Verify that the done () callback is called in this test.

If I run my test files using the mocha command, then it shows this error, and if I run the test mocha --timeout 15000, then the test test passes correctly. But I want to avoid a timeout, how can I do this?

+4
source share
3

mocha, , mocha --timeout 15000, . -, ?

-, , . - , .

Mocha -, - 0, , , , , .

request ( superagent), HTTP-/, , (, ), .

+4

mocha - 2 (2000ms).

- () , --timeout xxxx.

- , this.timeout( xxxx ) - , arrow functions - ( xxxx - , 20000, ).

it('My test', function(){
  this.timeout(5000);
  //... rest of your code
});

- ( describe):

describe("My suite", function(){
  // this will apply for both "it" tests
  this.timeout(5000);

  it( "Test 1", function(){
     ...
  });

  it( "Test 2", function(){
     ...
  });

});

before, beforeEach, after, afterEach.

: https://mochajs.org/#timeouts

, 2 , , - , . , , , , , .

+2

Here is what you need

mocha timeouts

describe('a suite of tests', function() {
  this.timeout(500);

  it('should take less than 500ms', function(done){
    setTimeout(done, 300);
  });

  it('should take less than 500ms as well', function(done){
    setTimeout(done, 250);
  });
})
0
source

All Articles