Mocha async test timeouts: distinguishing late rejections from hanging promises
0 reputation · 08 Apr 2021, 18:29 UTC
Mocha enforces a timeout on every test, with a default of 2000 ms, and the timer starts when the test function is invoked. For async tests, the setting applies to the entire body, so a returned promise that never settles is aborted with a timeout error once the deadline passes.
The unresolved behavior concerns promises that do settle, but only after the deadline has already fired. In that situation the runner reports the same generic timeout message whether the promise was hanging indefinitely or rejected moments too late, and there is no documented configuration that preserves the late rejection reason or separates the two cases. How the timeout budget is accounted across multiple sequential awaits inside a single test is also not specified.
Specific questions:
- Can Mocha's timeout configuration surface a rejection that arrives after the deadline as its own failure, instead of the generic timeout error?
- Does the timeout budget apply once per test from invocation, rather than resetting at each awaited operation?
- Where should this.timeout() be declared so it reliably governs an async test together with any promise-returning hooks?