TypeError when calling this.timeout() inside an arrow function test in Mocha
0 reputation · 11 Dec 2023, 07:15 UTC
I am configuring per-test timeouts in Mocha and hit a failure when adjusting the timeout dynamically. In a test written with an arrow function, calling this.timeout(5000) throws a TypeError because this is not the Mocha test context. The same call works in a test declared with a regular function expression.
My constraints: some suites were migrated to arrow functions for stylistic consistency, and I would prefer not to rewrite every test back to function expressions. I also need long-running async tests (around 3–5 seconds) to pass without raising the global --timeout for the whole suite, since the default 2000ms limit otherwise fails them. I am aware that setting the timeout to 0 disables the limit entirely, which risks hanging our CI pipeline if a test deadlocks, so that is not an acceptable fallback.
Assuming a current Mocha version (this should be verified against the release in use):
- Is there a documented way to set a per-test or per-hook timeout that works inside arrow functions, without relying on the
thiscontext? - Does a suite-level
this.timeout()in adescribecallback reliably propagate to nested hooks and tests, or do hooks have separate timeout behavior? - Is the config-file timeout option equivalent to the CLI flag for scoping purposes?