Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/vitest.setup.ts
T

79 lines
2.4 KiB
TypeScript

process.env.APP_NAME ??= "HiveGrid Test";
process.env.DATABASE_URL ??= "postgres://hivegrid:test@example.invalid:5432/hivegrid_test";
process.env.SESSION_SECRET ??= "test-session-secret";
process.env.JWT_SECRET ??= "test-jwt-secret";
process.env.API_ACCESS_TOKEN ??= "test-api-access-token";
process.env.CRYPTO_SECRET ??= "test-crypto-secret";
import i18next from "i18next";
import enTranslations from "./apps/web/public/locales/en/translations.json";
// Node keeps its native undici Request in the jsdom environment while jsdom
// installs a realm-local URLSearchParams constructor. Undici performs a strict
// WebIDL brand check that differs between Node releases. Serialize form bodies
// at the Request boundary instead of replacing the global constructor so both
// browser-facing URLSearchParams behavior and native Request parsing stay stable.
const NativeRequest = globalThis.Request;
function isUrlSearchParamsBody(body: BodyInit | null | undefined): body is URLSearchParams {
return body !== null && typeof body === "object" && Object.prototype.toString.call(body) === "[object URLSearchParams]";
}
function acceptsNativeRequestSignal(signal: AbortSignal | null | undefined): boolean {
if (!signal) {
return true;
}
try {
new NativeRequest("https://app.example.test/__signal_compatibility__", { signal });
return true;
} catch {
return false;
}
}
class RealmSafeRequest extends NativeRequest {
constructor(input: RequestInfo | URL, init?: RequestInit) {
if (!init) {
super(input);
return;
}
const safeInit = acceptsNativeRequestSignal(init.signal) ? init : { ...init, signal: undefined };
if (!isUrlSearchParamsBody(safeInit.body)) {
super(input, safeInit);
return;
}
const headers = new Headers(safeInit.headers);
if (!headers.has("content-type")) {
headers.set("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
}
super(input, { ...safeInit, body: safeInit.body.toString(), headers });
}
}
Object.defineProperty(globalThis, "Request", {
configurable: true,
writable: true,
value: RealmSafeRequest,
});
if (!i18next.isInitialized) {
await i18next.init({
lng: "en",
fallbackLng: "en",
ns: ["translations", "translation"],
defaultNS: "translations",
resources: {
en: {
translation: enTranslations,
translations: enTranslations,
},
},
interpolation: { escapeValue: false },
});
}