Vitest testTimeout: uniform behavior across sync and async tests
0 reputation · 23 Dec 2021, 19:23 UTC
0 reputation · 23 Dec 2021, 19:23 UTC
Determine whether Vitest’s testTimeout setting applies consistently to both synchronous and asynchronous tests, and how a timeout influences pending microtasks and cleanup.
The documentation specifies a global testTimeout and per‑test test.timeout overrides, but it does not state if the timeout is enforced for tests that finish synchronously. Additionally, when a timeout occurs, it is unclear whether Vitest forcefully aborts the test’s async callbacks or simply marks the test as failed, which affects whether queued microtasks are still processed.
testTimeout trigger a timeout error for a test that completes synchronously but exceeds the specified duration?afterEach hooks still executed?34775 reputation · 24 Dec 2021, 02:06 UTC
Vitest’s testTimeout does not fire for a test that finishes synchronously and returns immediately, because the test’s completion promise resolves instantly. The timeout only becomes observable after the test yields control to the event loop (e.g., after a blocking synchronous operation or an asynchronous await).
Promise.race).done() callback) before the timeout promise rejects, the test passes; otherwise it fails.afterEach hooks are executed for every test outcome—pass, fail, or timeout—ensuring teardown logic runs.testTimeout: 10 (ms). The test will pass regardless of the timeout value.while (Date.now() - start < 150) {}). The test will fail after the loop finishes, because the test’s promise only resolves after the blocking work.testTimeout. The test fails, but any .then handlers attached to that promise still run as microtasks before the failure is reported.afterEach – add an afterEach that increments a counter or logs a message. Run a passing test, a timed‑out sync test, and a timed‑out async test; the hook will run in all three cases.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.