Files
suaveplan/packages/persistence/database/surrealdb/tests/integration/integration-setup.ts
2026-05-27 23:47:53 +01:00

59 lines
2.1 KiB
TypeScript

/**
* Shared integration-setup module — exposes the typed `inject` helper and a
* `surrealUnavailable` boolean for `describe.skipIf(...)` skip-when-down
* behaviour. Consumed by every integration test file in this package.
*
* Fixture registration lives in the sibling `global-setup.ts` file (which
* vitest's `globalSetup` array points at) — that is the only place where
* `createIntegrationSetup` is called, because vitest's globalSetup process
* runs in a separate module scope from the test workers and would otherwise
* not see the fixture registry populated here.
*
* @module
*/
import { inject as vitestInject } from "@suaveplan/testing/runner";
import type { SurrealDBConfig } from "@suaveplan/testing-integration-surrealdb";
export type FixtureName = "surrealdb";
interface IntegrationConfigs {
surrealdb: SurrealDBConfig;
}
/**
* Typed accessor for the published fixture configs. Wraps Vitest's untyped
* `inject("__integration_configs__")` with the package's known fixture map.
*/
export function inject<K extends keyof IntegrationConfigs>(
key: K
): IntegrationConfigs[K] {
const all = (vitestInject as (k: string) => IntegrationConfigs)(
"__integration_configs__"
);
return all[key];
}
/**
* `true` when the surrealdb fixture failed to start (no Docker, env-bridge
* incomplete, etc.) AND when the global-setup did not run at all (e.g. the
* test file is loaded outside the integration runner). Test files use this
* with `describe.skipIf(...)` so the suite is SKIPPED rather than failed.
*/
export const surrealUnavailable: boolean = (() => {
try {
const failed = (vitestInject as (k: string) => string[] | undefined)(
"__integration_failed__"
);
if (failed === undefined) return true;
if (failed.includes("surrealdb")) return true;
const configs = (
vitestInject as (
k: string
) => Record<string, Record<string, unknown>> | undefined
)("__integration_configs__");
return configs === undefined || configs.surrealdb === undefined;
} catch {
return true;
}
})();