histoire
Version:
A fast story book powered by Vite
91 lines (86 loc) • 4.37 kB
JavaScript
import fs from 'fs-extra';
import { join } from 'pathe';
import { assertNoDuplicateTempPaths, toTempPathSegment } from '../util/temp-paths.js';
import { TEST_HARNESS_ID } from '../virtual/index.js';
import { TEST_COLLECTION_ENDPOINT } from './collection-channel.js';
/**
* Writes one Vitest spec file per collected variant.
* @param storyFiles Collected story files that registered tests.
* @param root Directory owned by the current run.
* @param mode Collection stores definitions in inert task metadata; run executes assertions.
*/
export async function generateSpecFiles(storyFiles, root, mode = 'run', collectionToken) {
await fs.emptyDir(root);
const results = [];
for (const storyFile of storyFiles) {
if (!storyFile.story)
continue;
for (const variant of storyFile.story.variants) {
// The segment carries a hash of the RAW variant id: two ids that sanitize
// to the same name would otherwise overwrite each other on disk and one
// variant's results would be silently dropped while the run reports `ok`.
const specPath = join(root, storyFile.relativePath.replace(/\.[^.]+$/, ''), `${toTempPathSegment(variant.id)}.histoire.spec.ts`);
await fs.ensureDir(join(specPath, '..'));
await fs.writeFile(specPath, mode === 'collect' ? generateCollectionSpecCode(storyFile.story.id, variant.id, collectionToken) : generateSpecCode(storyFile.story.id, variant.id, storyFile.story.title, variant.title), 'utf8');
results.push({
path: specPath,
storyId: storyFile.story.id,
variantId: variant.id,
});
}
}
// Defense in depth: a duplicate generated path means two variants collided and
// one would clobber the other (both on disk and in the path-keyed summary map).
assertNoDuplicateTempPaths(results.map(spec => ({ path: spec.path, label: `variant "${spec.storyId}:${spec.variantId}"` })), 'spec');
return results;
}
/** Inert task metadata carries exact definitions through Vitest's collection reporting. */
function generateCollectionSpecCode(storyId, variantId, token) {
return `import { test } from 'vitest'
import { serializeTestErrors } from '@histoire/shared'
import { collectVariantTests } from ${JSON.stringify(TEST_HARNESS_ID)}
const collectionStarted = performance.now()
let collection
try {
collection = { definitions: await collectVariantTests(${JSON.stringify(storyId)}, ${JSON.stringify(variantId)}) }
}
catch (error) {
collection = { definitions: [], error: serializeTestErrors([error])[0] }
}
${token
? `await fetch(${JSON.stringify(TEST_COLLECTION_ENDPOINT)}, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token: ${JSON.stringify(token)}, duration: performance.now() - collectionStarted, entry: { target: ${JSON.stringify({ storyId, variantId })}, collection } }),
}).then(response => { if (!response.ok) throw new Error('Test collection publication failed') })`
: ''}
// Runtime collection reads metadata; this callback never executes.
test('Histoire test collection', { meta: { histoireCollection: collection } }, () => {})
`;
}
/**
* Generates the spec running every test a variant collected.
*
* Vitest owns the outer cases. Story imports use Histoire's harness, which
* provides the definitions (including their
* `.only`/`.skip`/`.todo` mode) come from the collection.
*/
function generateSpecCode(storyId, variantId, storyTitle, variantTitle) {
return `import { describe, test } from 'vitest'
import { prepareVariantTestRun } from ${JSON.stringify(TEST_HARNESS_ID)}
const { definitions, run } = await prepareVariantTestRun(${JSON.stringify(storyId)}, ${JSON.stringify(variantId)})
describe(${JSON.stringify(`${storyTitle} > ${variantTitle}`)}, () => {
for (const definition of definitions) {
if (definition.mode === 'todo') {
test.todo(definition.fullName)
continue
}
const testFn = definition.mode === 'only' ? test.only : definition.mode === 'skip' ? test.skip : test
testFn(definition.fullName, async () => {
await run(definition)
// Embedded runtime owns body and hook deadlines. A second outer deadline
// races it, duplicates failures, and can preempt a valid longer test.
}, 0)
}
})
`;
}