histoire
Version:
A fast story book powered by Vite
165 lines (164 loc) • 7.29 kB
JavaScript
import { createHistoireTestSummary, serializeTestError } from '@histoire/shared';
import { openAroundHooks } from './around-hooks.js';
import { resetSharedExpectState } from './expect-state.js';
import { runHooks } from './hooks.js';
import { runDefinitionWithHooks } from './run-definition.js';
import { finishHistoireTestTask } from './task-callbacks.js';
import { createPreviewTestTask } from './test-task.js';
import { getVitestWorkerState } from './worker-state.js';
/** Decides whether a definition must be reported as skipped. */
function isSkipped(definition, hasFocusedTests) {
return definition.mode === 'skip'
|| definition.mode === 'todo'
|| (hasFocusedTests && definition.mode !== 'only');
}
/** Builds one result entry reported to Histoire. */
function toResult(definition, state, errors) {
return {
id: definition.id,
name: definition.name,
fullName: definition.fullName,
state,
errors,
};
}
/** Returns count of identical leading suite scopes. */
function commonScopeCount(left, right) {
let count = 0;
while (left[count] && left[count] === right[count]) {
count++;
}
return count;
}
/** Marks an already-created result failed because suite cleanup failed. */
function appendResultError(result, error) {
if (!result || !error) {
return;
}
result.state = 'failed';
result.errors.push(serializeTestError(error));
}
/** Executes all collected tests of one mounted variant session. */
export async function runSessionTests(session, storyId, variantId) {
const workerState = getVitestWorkerState();
const hasFocusedTests = session.definitions.some(definition => definition.mode === 'only');
const filepath = session.file.story.file?.filePath ?? session.file.filePath;
const results = [];
const scopeErrors = new Map();
const unopenedScopeErrors = new Map();
const suiteCleanups = new Map();
const aroundControllers = new Map();
let activeScopes = [];
let lastRunnableResult;
/** Unwinds a completed suite, including setup disposers and its aroundAll wrapper. */
async function closeScope(scope, error) {
error = await runHooks([...scope.afterAll].reverse(), undefined, scope.suite, error, true);
error = await runHooks((suiteCleanups.get(scope) ?? []).reverse(), undefined, undefined, error, true);
const aroundError = await aroundControllers.get(scope)?.close();
suiteCleanups.delete(scope);
aroundControllers.delete(scope);
scopeErrors.delete(scope);
return error ?? aroundError;
}
for (const [index, definition] of session.definitions.entries()) {
if (isSkipped(definition, hasFocusedTests)) {
results.push(toResult(definition, 'skipped', []));
continue;
}
const nextScopes = definition.hookScopes ?? [];
const sharedCount = commonScopeCount(activeScopes, nextScopes);
let transitionError;
for (const scope of activeScopes.slice(sharedCount).reverse()) {
transitionError = await closeScope(scope, transitionError);
}
appendResultError(lastRunnableResult, transitionError);
activeScopes = activeScopes.slice(0, sharedCount);
// A failed ancestor prevents Vitest from entering descendant suites. Keep
// `activeScopes` to scopes that actually opened so their teardown runs,
// while never calling child beforeAll/afterAll hooks that were skipped.
let blockedByAncestor = activeScopes.some(scope => scopeErrors.has(scope));
for (const scope of nextScopes.slice(sharedCount)) {
if (blockedByAncestor || unopenedScopeErrors.has(scope)) {
blockedByAncestor = true;
break;
}
let openingError;
if (scope.aroundAll.length) {
const opened = await openAroundHooks(scope.aroundAll, scope.suite);
if ('controller' in opened) {
aroundControllers.set(scope, opened.controller);
}
else {
openingError = opened.error;
}
}
if (openingError) {
// Vitest never enters a suite whose aroundAll setup failed, so neither
// afterAll nor beforeAll disposers may run for this scope.
unopenedScopeErrors.set(scope, openingError);
blockedByAncestor = true;
break;
}
const cleanups = [];
suiteCleanups.set(scope, cleanups);
const error = await runHooks(scope.beforeAll, undefined, scope.suite, undefined, false, cleanups);
activeScopes.push(scope);
if (error) {
scopeErrors.set(scope, error);
blockedByAncestor = true;
}
}
const previousCurrent = workerState?.current;
const previousFilepath = workerState?.filepath;
const task = createPreviewTestTask(definition, `${storyId}:${variantId}:${index}`, filepath);
if (workerState) {
workerState.filepath = filepath;
workerState.current = task;
}
try {
resetSharedExpectState();
let taskFinalized = false;
let testError = nextScopes.map(scope => scopeErrors.get(scope) ?? unopenedScopeErrors.get(scope)).find(Boolean);
if (!testError) {
try {
await runDefinitionWithHooks(definition, storyId, variantId, async (error) => {
taskFinalized = true;
return await finishHistoireTestTask(task, error);
});
}
catch (error) {
testError = error;
}
}
// A failed aroundEach setup may never call the inner lifecycle, so its
// callbacks have not finalized the task yet. Finalize it here exactly
// once; otherwise callbacks have already run before wrapper teardown.
if (!taskFinalized) {
await finishHistoireTestTask(task, testError);
}
else if (testError) {
const errors = testError instanceof AggregateError ? testError.errors : [testError];
for (const error of errors) {
if (!task.result.errors.includes(error))
task.result.errors.push(error);
}
task.result.state = 'fail';
}
const errors = task.result.errors;
lastRunnableResult = toResult(definition, errors.length ? 'failed' : 'passed', errors.map(serializeTestError));
results.push(lastRunnableResult);
}
finally {
if (workerState) {
workerState.current = previousCurrent;
workerState.filepath = previousFilepath;
}
}
}
let finalError;
for (const scope of [...activeScopes].reverse()) {
finalError = await closeScope(scope, finalError);
}
appendResultError(lastRunnableResult, finalError);
return createHistoireTestSummary(storyId, variantId, results);
}