0

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.

Community
  • 1
  • 1
silverlight513
  • 5,268
  • 4
  • 26
  • 38

1 Answers1

2

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.

jesusgn90
  • 563
  • 2
  • 12
  • You write a timeout of 2300 it's normal that you exceed the 2000 default time! – jesusgn90 Feb 06 '17 at 14:03
  • Sorry, it just clicked that the mocha timeout is the final curtain close. I was thinking that if you return a promise, the timeout gets cancelled and you're allowed as much time as you want. And now I think about that, that would be very dangerous – silverlight513 Feb 06 '17 at 14:06
  • Hahahah nevermind friend! – jesusgn90 Feb 06 '17 at 14:07