Mocha exceeding 2000ms timeout when returning a pr

2019-08-27 02:24发布

问题:

This question already has an answer here:

  • Change default timeout for mocha 4 answers

I'm trying to test my Express.js controllers but every so often I run into a problem with the mocha timeout telling me off.

The docs (https://mochajs.org/#working-with-promises) and the answer here: https://stackoverflow.com/a/26572442/1646372 state that I can just return the promise I'm using.

I've wrapped my express controllers with Promises so that I can then return them in the tests.

I have a basic test that I can run to consistently get the error message:

it('should return', () => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('hello');
    }, 2300);
  });
});

The error I'm getting is:

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

I don't understand why the error is present as I'm returning a promise that does resolve.

回答1:

You can set timeout in the command line with this flag --timeout 5000 or you can add this at first line of a test this.timeout(5000);, under describe statement, inside the function.