mocha: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

I’m getting the following error when running the entire suite of tests:

timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

I found this super useful response on StackOverflow http://stackoverflow.com/questions/16607039/in-mocha-testing-while-calling-asynchronous-function-how-to-avoid-the-timeout-er# and here https://github.com/mochajs/mocha/pull/278

However, the problem still persists even after deleting every occurrence in my tests that deal with HTTP and promises. All I’ve got now are Angular directive and controller specs which doesn’t seem to do much other than checking template data, directive and controller logic.

Does anyone know why this is still happening and if there’s a better way to know exactly what the issue is? thanks!

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 46
  • Comments: 24 (3 by maintainers)

Commits related to this issue

Most upvoted comments

just add in your package.json under scripts

       "scripts": {
            "start": "SET NODE_ENV=dev && node ./bin/www",
            "devstart": "SET NODE_ENV=dev && nodemon ./bin/www",
            "test": "mocha --timeout 10000"  <= increase this from 1000 to 10000
        },

Then you can just run

      npm test

Test-specific timeouts may also be applied, or the use of this.timeout(0) to disable timeouts all together:

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

source: https://mochajs.org/#timeouts

@ScottFreeCode thank you. Error resolved by adding this.timeout(10000); inside

it("Test Post Request", function(done) {
     this.timeout(10000);
});

Am getting Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. How to fix this? My code is:

describe("Test Search", function() {
	it("Test Post Request", function(done) {
            chai.request(postReqURL)
            .post(postReqURL)
            .send(postReqObject)
            .end(function (err, res) {
                if (err) done(err);
                expect(res.status).to.equal(200);
                done()
            })
       });
});

I was also getting that error, and after several hours of researching and debugging, I found the root cause.

Consider this test:

const delay = require('delay')

describe('Test', function() {
    it('should resolve', async function(done) {
      await delay(1000)
    })
})

When I run the test, I get this error:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

Now consider this slightly different test, where the done argument is removed:

const delay = require('delay')

describe('Test', function() {
    it('should resolve', async function() {
      await delay(1000)
    })
})

When I run this test, it passes.

Somehow the presence of the done argument in the async function breaks the test, even if it’s not used, and even if done() is called at the end of the test.

Closing this issue. It turned out to be a memory leak issue described here https://github.com/mochajs/mocha/issues/2030

Hi @vishnu2prasadh, have you tried increasing the timeout to longer than the HTTP API is expected to take to respond? Your test’s usage of done appears to be correct.

just add in your package.json under scripts

       "scripts": {
            "start": "SET NODE_ENV=dev && node ./bin/www",
            "devstart": "SET NODE_ENV=dev && nodemon ./bin/www",
            "test": "mocha --timeout 10000"  <= increase this from 1000 to 10000
        },

Then you can just run

      npm test

Adding --timeout 10000 to the the “test” “scripts” , worked like charm to me. Thank you!

I resolved this my creating a global timeout rather than setting it for individual tests or adding the configuration to my package.json file. Under my test folder I created a file mocha.opts. In the file, I set a reasonable timeout span for my tests to run, e.g.,

--timeout 10000

just add in your package.json under scripts

       "scripts": {
            "start": "SET NODE_ENV=dev && node ./bin/www",
            "devstart": "SET NODE_ENV=dev && nodemon ./bin/www",
            "test": "mocha --timeout 10000"  <= increase this from 1000 to 10000
        },

Then you can just run

      npm test

the most useful answer so far 👍