Files
flights_web/src/env/env.test.ts
T
gnezim 1facfd8050
Deploy / build-and-deploy (push) Failing after 5s
Fix runtime rendering: Outlet, process guards, env defaults
Three root causes of blank page:
1. Modern.js layouts use <Outlet /> not {children} for nested routes
2. process.env not available in browser — guard with typeof checks
3. getEnv() schema required all fields — add defaults for browser context

Also: add source.entriesDir, runtime.router to modern.config.ts,
disable SSR temporarily until the SSR server build alias issue is
resolved (framework-level @_modern_js_src resolution).
2026-04-15 15:24:32 +03:00

94 lines
3.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "vitest";
describe("getEnv", () => {
const originalEnv = { ...process.env };
beforeEach(() => {
// Clear env to a clean slate, then set a minimum valid shape.
for (const key of Object.keys(process.env)) {
if (key.startsWith("VITEST_")) continue;
delete process.env[key];
}
process.env["NODE_ENV"] = "testing";
process.env["BUILD_TARGET"] = "standalone";
process.env["PROD_ORIGIN"] = "https://flights.aeroflot.ru";
process.env["API_BASE_URL"] = "https://platform.aeroflot.ru";
process.env["SIGNALR_HUB_URL"] = "wss://platform.aeroflot.ru/hub";
process.env["ANALYTICS_METRICA"] = "true";
process.env["ANALYTICS_CTM"] = "false";
process.env["ANALYTICS_VARIOCUBE"] = "false";
process.env["ANALYTICS_DYNATRACE"] = "true";
process.env["VERSION"] = "abc1234";
});
afterEach(async () => {
process.env = { ...originalEnv };
// Reset the module cache so getEnv re-reads the updated env.
const mod = await import("./index.js");
mod.__resetEnvCacheForTests();
});
it("returns a typed Env object for valid input", async () => {
const { getEnv } = await import("./index.js");
const env = getEnv();
expect(env.NODE_ENV).toBe("testing");
expect(env.BUILD_TARGET).toBe("standalone");
expect(env.PROD_ORIGIN).toBe("https://flights.aeroflot.ru");
expect(env.API_BASE_URL).toBe("https://platform.aeroflot.ru");
expect(env.SIGNALR_HUB_URL).toBe("wss://platform.aeroflot.ru/hub");
expect(env.ANALYTICS_ENABLED).toEqual({
metrica: true,
ctm: false,
variocube: false,
dynatrace: true,
});
expect(env.VERSION).toBe("abc1234");
expect(env.OTEL_EXPORTER_OTLP_ENDPOINT).toBeUndefined();
});
it("caches the result across calls (same object identity)", async () => {
const { getEnv } = await import("./index.js");
const a = getEnv();
const b = getEnv();
expect(a).toBe(b);
});
it("uses default value when a field is missing (browser-safe defaults)", async () => {
delete process.env["API_BASE_URL"];
const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
__resetEnvCacheForTests();
const env = getEnv();
// API_BASE_URL defaults to http://localhost:8080/api when not set
expect(env.API_BASE_URL).toBe("http://localhost:8080/api");
});
it("throws when NODE_ENV is not one of the allowed values", async () => {
process.env["NODE_ENV"] = "banana";
const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
__resetEnvCacheForTests();
expect(() => getEnv()).toThrow(/NODE_ENV/);
});
it("throws when BUILD_TARGET is neither standalone nor remote", async () => {
process.env["BUILD_TARGET"] = "hybrid";
const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
__resetEnvCacheForTests();
expect(() => getEnv()).toThrow(/BUILD_TARGET/);
});
it("accepts optional OTEL_EXPORTER_OTLP_ENDPOINT when provided", async () => {
process.env["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://otel.example/v1/traces";
const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
__resetEnvCacheForTests();
const env = getEnv();
expect(env.OTEL_EXPORTER_OTLP_ENDPOINT).toBe("https://otel.example/v1/traces");
});
it("rejects OTEL_EXPORTER_OTLP_ENDPOINT that is not a URL", async () => {
process.env["OTEL_EXPORTER_OTLP_ENDPOINT"] = "not-a-url";
const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
__resetEnvCacheForTests();
expect(() => getEnv()).toThrow(/OTEL_EXPORTER_OTLP_ENDPOINT/);
});
});