Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gustiando opened this issue Dec 26, 2015 · 24 comments

Comments

@gustiando
Copy link

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 #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!

@boneskull
Copy link
Contributor

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

@boneskull boneskull added the status: waiting for author waiting on response from OP - more information needed label Dec 26, 2015
@gustiando
Copy link
Author

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?

@gustiando
Copy link
Author

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.

@gustiando
Copy link
Author

Closing this issue. It turned out to be a memory leak issue described here #2030

@elvinaze
Copy link

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

@spike321
Copy link

spike321 commented Dec 13, 2016

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

@vishnu2prasadh
Copy link

vishnu2prasadh commented Sep 7, 2017

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

@ScottFreeCode
Copy link
Contributor

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.

@vishnu2prasadh
Copy link

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

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

@ianrussel
Copy link

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

@CryptoKiddies
Copy link

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

greatwillow added a commit to greatwillow/react-datasheet that referenced this issue Jun 2, 2018
This change fixes the timeout error that was occuring upon testing which was causing tests to fail.  For further reference, see: mochajs/mocha#2025
@mochajs mochajs deleted a comment from selvakumar1994 Aug 29, 2018
@chunkingz
Copy link

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 👍

@ranjanpandey11111
Copy link

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

@ashish101184
Copy link

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

@plroebuck
Copy link
Contributor

plroebuck commented Apr 9, 2019

@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');
    });
});

@vijaysimh
Copy link

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

@sean-olson
Copy link

sean-olson commented May 12, 2019

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

@plroebuck plroebuck removed the status: waiting for author waiting on response from OP - more information needed label May 13, 2019
@scibert
Copy link

scibert commented Jun 3, 2020

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.

@VishSinha
Copy link

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!

@jeff-r-koyaltech
Copy link

I know this is old, but I have encountered tests that run fine on one version of Node, but hang with the "timeout of 2000ms exceeded. Ensure the done() callback" message on other versions.

My node code runs on Node v 14.x as per my Dockerfile, and my tests pass fine on that version. But on a build agent in Azure, I was running on Node v 12.x, and I was getting this error. Once I specified v 14.x, it also worked on Azure's pipeline without any other code changes. 👍

So it's worth trying a few different versions of Node, if you're otherwise stuck!

Thanks to all who have previously commented on this issue. Good info to know.

Confidenceman02 pushed a commit to Confidenceman02/elm-select that referenced this issue Jul 17, 2021
It seems the first test is having issues with a timeout. Added timeout
as recommended by mochajs/mocha#2025
Confidenceman02 pushed a commit to Confidenceman02/elm-select that referenced this issue Jul 17, 2021
It seems the first test is having issues with a timeout. Added timeout
as recommended by mochajs/mocha#2025
Confidenceman02 pushed a commit to Confidenceman02/elm-select that referenced this issue Jul 17, 2021
It seems the first test is having issues with a timeout. Added timeout
as recommended by mochajs/mocha#2025
Confidenceman02 pushed a commit to Confidenceman02/elm-select that referenced this issue Jul 17, 2021
It seems the first test is having issues with a timeout. Added timeout
to test script as recommended by mochajs/mocha#2025
@Iroh-Omolola
Copy link

i solved mine by adding "done()" after the .end closing bracket .end((err, res) => {
if (err) done(err);
expect(res.status).to.equal(200);
});
done()

@SLRonzoni
Copy link

Gracias Iroh-Omolola !!! a mi me funcionó perfecto con tu resolución

@MuhammadWaqar320
Copy link

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 #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!

no

@MuhammadWaqar320
Copy link

no

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests