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

Created on 26 Dec 2015  ·  19Comments  ·  Source: mochajs/mocha

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!

Most helpful comment

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

All 19 comments

@gumatias to be much more help we'd need to set your tests

thanks for the feedback @boneskull !

There's around 700+ specs at the moment and they're all proprietary code. I can provide one of two similar samples, would that be any helpful?

Below is what I currently have in package.json. Upgrading libraries such as mocha and karma-mocha didn't seem to help.

"devDependencies": {
  "karma": "~0.12.30",
  "karma-chai-jquery": "~1.0.0",
  "karma-chrome-launcher": "~0.1",
  "karma-coffee-preprocessor": "~0.1.3",
  "karma-firefox-launcher": "~0.1",
  "karma-jquery": "~0.1.0",
  "karma-mocha": "0.2.0",
  "karma-sinon-chai": "~0.1.1",
  "karma-spec-reporter": "~0.0.10",
  "mocha": "^2.2.5"
}

Below are some of my attempts on getting to a state where the timeouts would stop occurring (without success).

1. Called done() within and after each promise and async calls in tests

2. Removed all specs that deals with async calls, promises, httpBackend and timeout (just in case)

3. Upgraded most libraries in package.json:

"devDependencies": {
  "karma": "~0.12.30",
  "karma-chai-jquery": "1.0.0",
  "karma-chrome-launcher": "0.2.2",
  "karma-coffee-preprocessor": "0.3.0",
  "karma-firefox-launcher": "~0.1",
  "karma-jquery": "0.1.0",
  "karma-mocha": "0.2.1",
  "karma-sinon-chai": "1.1.0",
  "karma-spec-reporter": "0.0.23",
  "mocha": "2.3.4"
}

4. Removed specs that karma spec reporter was complaining to be slow:
e.g. Chrome 39.0.2171 (Mac OS X 10.11.2) SLOW 2.457 secs: My Spec Name "before each" hook for "should display " Add My Spec" when doing something"

Turned out other new specs were complaining to be slow. This could probably lead to deleting specs without really finding the root cause.

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

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

If you're using Ember, try wrapping async calls in Ember.run => read this

I had the same problem, and Ember.run => fixed it.

Also, remember the arrow has to be a fat arrow, (i made that mistake once of using -> instead of =>)it's because of javascript scope, read up on it if you are interested.

stackoverflow link

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()
            })
       });
});

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.

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

it("Test Post Request", function(done) {
     this.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

@elvinaze is there a way to increase timeout limits in the beforeEach block?

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 👍

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

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

it('should access user module', function(done){
any_asynchfunction().then(function(check){
try{
assert.strictEqual(check,true,'admin user have access user module');
done();
}catch(err){
done(err);
}
});
});

var any_asynchfunction = function (){
var deferred = q.defer();
// Call Async function like : API call (http://api.domain.com)
deferred.resolve(res);
return deferred.promise;
}

Testing async code with Mocha using callbacks and promises

@ashish101184, don't add comments to unrelated closed issues!

Your problem was explained in the documentation.

it('should access user module', function() {
  return any_asynchfunction()
    .then(function(check) {
      assert.strictEqual(check, true, 'admin user should have access to user module');
    });
});

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
Hello have installed botium directline3 facing error 'Timeout of 60000ms exceeded' as per your guidelines in which file should we have add these lines

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

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.

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!

Was this page helpful?
0 / 5 - 0 ratings