UNPKG

histoire

Version:

A fast story book powered by Vite

54 lines (53 loc) • 2.06 kB
import { HistoireSdkError } from '@histoire/protocol'; /** Creates the progress signal a run's reporter touches and its stall timeout listens to. */ export function createVitestRunActivity() { let listener; return { touch: () => listener?.(), listen: (next) => { listener = next; }, }; } /** * Runs a Vitest browser run under a safety timeout. * * `vitest.start()` can hang forever when the browser pool never finishes * initializing (missing Playwright browsers, a page that never loads), which * would wedge the CLI or a dev-server request with no output at all. * * The timer is unref'd so it never keeps the process alive on its own, and it is * cleared on every path (success, rejection, retry) so it can neither leak nor * fire on an already-closed Vitest instance. * @param run Starts the Vitest run. * @param options Timeout configuration (see {@link VitestStartTimeoutOptions}). */ export async function runWithVitestStartTimeout(run, options) { let timer; try { await Promise.race([ run(), new Promise((_, reject) => { /** (Re)starts the countdown; with an activity signal every progress report calls it again. */ const arm = () => { clearTimeout(timer); timer = setTimeout(() => { try { options.onTimeout(); } catch { // Vitest may already be torn down when a late timer fires — its // diagnostics must never replace the timeout error. } reject(new HistoireSdkError('TIMEOUT', options.message)); }, options.timeoutMs); timer?.unref?.(); }; options.activity?.listen(arm); arm(); }), ]); } finally { options.activity?.listen(undefined); clearTimeout(timer); } }