diff --git a/src/client/Api.ts b/src/client/Api.ts index a5c3ab00d..5f6177b96 100644 --- a/src/client/Api.ts +++ b/src/client/Api.ts @@ -679,7 +679,7 @@ export function getApiBase() { const domainname = getAudience(); if (domainname === "localhost") { - const apiDomain = process?.env?.API_DOMAIN; + const apiDomain = process.env.API_DOMAIN; if (apiDomain) { return `https://${apiDomain}`; } diff --git a/tests/Api.test.ts b/tests/Api.test.ts new file mode 100644 index 000000000..b5bf87ebf --- /dev/null +++ b/tests/Api.test.ts @@ -0,0 +1,15 @@ +import { beforeEach, describe, expect, it } from "vitest"; +import { getApiBase } from "../src/client/Api"; + +describe("getApiBase", () => { + beforeEach(() => { + localStorage.clear(); + }); + + // API_DOMAIN is forced empty under vitest via the vite.config `define`, so this + // regression test exercises the fallback branch deterministically regardless of + // any API_DOMAIN in the host shell / CI. + it("falls back to http://localhost:8787 on localhost when apiHost is not set and API_DOMAIN is empty", () => { + expect(getApiBase()).toBe("http://localhost:8787"); + }); +}); diff --git a/vite.config.ts b/vite.config.ts index 393dc2ca7..95067afe4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -242,7 +242,12 @@ export default defineConfig(({ mode }) => { "process.env.STRIPE_PUBLISHABLE_KEY": JSON.stringify( env.STRIPE_PUBLISHABLE_KEY, ), - "process.env.API_DOMAIN": JSON.stringify(env.API_DOMAIN), + // Force empty under vitest (mode "test") so the getApiBase localhost- + // fallback test is deterministic regardless of any API_DOMAIN in the + // host shell / CI environment. + "process.env.API_DOMAIN": JSON.stringify( + mode === "test" ? "" : (env.API_DOMAIN ?? ""), + ), // Add other process.env variables if needed, OR migrate code to import.meta.env },