mocha timeout 2000ms exceeded in before hook: how to fix
Explains how to fix Mocha hook timeouts by using function-style hooks with this.timeout. Use when Mocha 10 or 11 reports a 2000ms timeout naming a before or beforeEach hook. Not for test-body timeouts, not for hooks that hang forever, and not when you want faster suites.
TL;DR
Raise the timeout on the hook itself with a regular function, not an arrow function, so this.timeout works. If the hook is genuinely slow, fix the setup cost instead of inflating the timeout forever. Start with this.timeout(10000) inside before() and re-run.
Error
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.Steps
- Change the hook from an arrow function to function () so Mocha binds this. Expected: this.timeout is available inside the hook.
- Call this.timeout(10000) as the first line of the hook. Expected: the 2000ms error disappears.
- If the hook does async work, return the promise or await it; never mix done callbacks with returned promises. Expected: Mocha waits for the real completion signal.
- Time the hook; if it takes most of the new budget, speed up the setup (reuse servers, seed once). Expected: the hook finishes well under the timeout.
- As a global fallback, pass --timeout 10000 on the CLI or set it in .mocharc. Expected: all tests and hooks get the higher budget.
When to use
- Mocha 10 or 11 reports timeout exceeded naming a before or beforeEach hook.
- The hook does DB, server, or browser startup that legitimately takes seconds.
- The default 2000ms is too tight for integration-style setup.
When not to use
- A test (not a hook) times out; raise it on the test or fix the test.
- The hook hangs forever; a bigger timeout just waits longer, find the missing resolve.
- You want faster suites; timeouts are a ceiling, not a speedup.
Tool compatibility
- Mocha 10.x, 11.x: this.timeout(ms) in function-style hooks; --timeout flag; .mocharc timeout setting.
- Arrow functions never get Mocha's this context in any version.
Variant phrasings
mocha hook timed out after 2000ms
Same fix; the hook form of the classic Mocha timeout error.
this.timeout is not a function in mocha hook
You used an arrow function; switch to function () so Mocha can bind this.
Why it happens
Mocha gives hooks 2000ms by default and binds the timeout control to this, which only exists in regular functions. Arrow functions capture the outer this, so the call silently fails and the default stands.
Edge cases
- Root-level hooks in a separate file need the same treatment; they share the default timeout.
- done() called twice throws a different error; do not add done to a promise-returning hook.
- Parallel mode runs hooks per file; a slow hook multiplies across workers, so fixing setup cost beats raising timeouts.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_7odB-LB3tvYc57UQCp5c1Q