From 6ddde089482bd8ec6834290e89cf3aa5ddd67c21 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 15:07:53 +0800 Subject: [PATCH] Import scripts/test-scenarios.mjs --- scripts/test-scenarios.mjs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 scripts/test-scenarios.mjs diff --git a/scripts/test-scenarios.mjs b/scripts/test-scenarios.mjs new file mode 100644 index 0000000..65d0a69 --- /dev/null +++ b/scripts/test-scenarios.mjs @@ -0,0 +1,31 @@ +// Run related variants of one contract without registering one test per row. +// Vitest still owns the first setup and final teardown. Restore the same fixture +// between rows so timers, mocks, credentials and connection state stay isolated. +export async function runScenarios(scenarios, between) { + const failures = [] + for (const [index, scenario] of scenarios.entries()) { + if (index > 0) await between?.() + try { + await scenario.run() + } catch (error) { + failures.push({ name: scenario.name, error }) + } + } + if (failures.length === 0) return + + // Keep the first assertion's actual/expected values and stack for Vitest's + // diff, and include every failed row instead of stopping at the first row. + const first = failures[0].error + const error = first instanceof Error ? first : new Error(String(first)) + error.message = failures.map(failure => + `${failure.name}: ${failure.error instanceof Error ? failure.error.message : String(failure.error)}`, + ).join('\n\n') + throw error +} + +export function scenarioName(template, row, index) { + const values = Array.isArray(row) ? [...row] : [row] + const title = template.replace(/%[sidjf]/g, () => String(values.shift())) + .replace(/\$([\w]+)/g, (match, key) => row?.[key] === undefined ? match : String(row[key])) + return `${title} [row ${index + 1}]` +}