FactuGES_ERP/tests/e2e/support/e2e-lifecycle.ts

38 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

export interface E2EEnvironment {
readonly environment: Record<string, string>;
destroy(): Promise<void>;
}
export async function runE2ELifecycle<TEnvironment extends E2EEnvironment>(params: {
provision: () => Promise<TEnvironment>;
prepare: (environment: TEnvironment) => Promise<void>;
bootstrap: (environment: TEnvironment) => Promise<void>;
runChild: (environment: Record<string, string>) => Promise<number>;
}): Promise<number> {
const environment = await params.provision();
let mainFailure: Error | undefined;
let exitCode: number | undefined;
try {
await params.prepare(environment);
await params.bootstrap(environment);
exitCode = await params.runChild(environment.environment);
} catch (error) {
mainFailure = error instanceof Error ? error : new Error(String(error));
}
try {
await environment.destroy();
} catch (cleanupError) {
if (!mainFailure) {
throw cleanupError;
}
throw new AggregateError([mainFailure, cleanupError], "E2E execution and cleanup failed.");
}
if (mainFailure) {
throw mainFailure;
}
return exitCode ?? 1;
}