histoire
Version:
A fast story book powered by Vite
64 lines (63 loc) • 3.08 kB
JavaScript
import { runAroundHooks } from './around-hooks.js';
import { createMissingHandlerError } from './definitions.js';
import { assertAndClearSharedExpectState } from './expect-state.js';
import { runHooks } from './hooks.js';
import { discardTestPromises, settleTestPromises } from './task-promises.js';
import { isHistoireTestTask } from './test-task.js';
import { createTestTimeoutError, getTestTimeout, withTimeout } from './timeouts.js';
import { getVitestWorkerState } from './worker-state.js';
/** Executes a test body, retaining a setup error instead of running the handler. */
async function runHandler(definition, storyId, variantId, context, task, callerPromises, error) {
if (error)
return error;
try {
if (!definition.handler)
throw createMissingHandlerError(definition.fullName, storyId, variantId);
const timeout = getTestTimeout(definition.timeout);
await withTimeout(Promise.resolve().then(async () => {
await definition.handler(context);
const pendingErrors = await settleTestPromises(task, callerPromises);
if (pendingErrors.length) {
throw pendingErrors.length === 1
? pendingErrors[0]
: new AggregateError(pendingErrors, 'Pending assertions failed');
}
}), timeout, () => createTestTimeoutError(timeout));
}
catch (handlerError) {
discardTestPromises(task, callerPromises);
return handlerError;
}
}
/** Runs the shared per-test lifecycle, including disposers from successful setup hooks. */
export async function runDefinitionWithHooks(definition, storyId, variantId, finish) {
const scopes = definition.hookScopes ?? [];
const task = getVitestWorkerState()?.current;
const context = task?.context;
const callerPromises = new Set(task?.promises ?? []);
await runAroundHooks(scopes.flatMap(scope => scope.aroundEach.map(hook => ({ hook, suite: scope.suite }))), async () => {
const cleanups = [];
let error;
for (const scope of scopes) {
error = await runHooks(scope.beforeEach, context, scope.suite, error, false, cleanups);
}
error = await runHandler(definition, storyId, variantId, context, task, callerPromises, error);
if (!error && isHistoireTestTask(task)) {
try {
assertAndClearSharedExpectState(context);
}
catch (assertionError) {
error = assertionError;
}
}
for (const scope of [...scopes].reverse()) {
error = await runHooks([...scope.afterEach].reverse(), context, scope.suite, error, true);
}
// All explicit afterEach hooks precede returned disposers; acquired resources
// unwind in reverse order even if a later setup, test, or teardown failed.
error = await runHooks(cleanups.reverse(), context, undefined, error, true);
error = (await finish?.(error)) ?? error;
if (error)
throw error;
}, context);
}