Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid timeouts in mocha testcases?

Here I am attaching my code, I am passing done callback and using supertest for request. Since I am using assert/expect in my testcase inside request.end block why I need to worry about timeout? What is mistake I am making 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();
      });
 });

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

If I am running my testcases with mocha command then its show this error while If I am running test mocha --timeout 15000 then testcase is passing correctly. But I want to avoid timeout, How can I do that?

like image 689
Anupam Jakhar Avatar asked Apr 28 '16 06:04

Anupam Jakhar


People also ask

How do I change the timeout on my Mocha?

Whenever you run Mocha at the command line, it will read this file and set a timeout of 5 seconds by default. Another way which may be better depending on your situation is to set it like this in a top level describe call in your test file: describe("something", function () { this. timeout(5000); // tests... });

How do you ignore a test in Mocha?

Find out how you can ignore some tests in Mocha You can use the skip() method (on describe() or it() ) to ignore some tests: In case of describe , it will ignore all tests in the describe block (which includes all nested describe and it functions); In case of it , it will ignore the individual test.

Do Mocha tests run sequentially?

Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.


1 Answers

If I am running my testcases with mocha command then its show this error while If I am running test mocha --timeout 15000 then testcase is passing correctly. But I want to avoid timeout, How can I do that?

You can't avoid timeouts, since it looks like you're testing a remote service. If, for whatever reason, the request to that service takes a long time, you will run into timeouts.

You can tell Mocha to disable for timeout checking by setting the timeout to 0, but that's probably also not ideal because it may cause each test case to take an excessive amount of time.

As an alternative, you can mock request (which I assume is superagent) so you can control the entire HTTP request/response flow, but since it looks like you're testing a remote service (one which you have no control over) that would make this particular test case moot.

like image 76
robertklep Avatar answered Sep 19 '22 15:09

robertklep