UNPKG

histoire

Version:

A fast story book powered by Vite

42 lines (41 loc) • 1.64 kB
import { createHookTimeoutError, getHookTimeout, withTimeout } from './timeouts.js'; /** Deadlines inherited by disposers returned from setup hooks. */ const cleanupTimeouts = new WeakMap(); /** Remembers source-hook deadline for a cleanup Vitest runs later. */ function addCleanup(cleanups, cleanup, timeout) { if (!cleanups) return; const timedCleanup = async () => await cleanup(); cleanupTimeouts.set(timedCleanup, timeout); cleanups.push(timedCleanup); } /** Runs hooks while preserving first error and optionally continuing cleanup. */ export async function runHooks(hooks, context, suite, currentError, always = false, cleanups) { let error = currentError; for (const hook of hooks) { if (error && !always) { break; } try { if (typeof hook === 'function') { const timeout = cleanupTimeouts.get(hook); if (timeout === undefined) { await hook(); } else { await withTimeout(Promise.resolve().then(hook), timeout, () => createHookTimeoutError(timeout)); } continue; } const timeout = getHookTimeout(hook.timeout); const cleanup = await withTimeout(Promise.resolve().then(() => hook.handler(context ?? {}, suite)), timeout, () => createHookTimeoutError(timeout)); if (typeof cleanup === 'function') { addCleanup(cleanups, cleanup, timeout); } } catch (hookError) { error ??= hookError; } } return error; }