From 301f7d553da2e666349574f8ec8b42fb81a08104 Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Thu, 28 Nov 2024 13:13:31 +0100 Subject: [PATCH] feat: export metabase client with delayed instantiation for test --- .../modules/analytics/metabase/shared/client.test.ts | 4 ++++ api.planx.uk/modules/analytics/metabase/shared/client.ts | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/api.planx.uk/modules/analytics/metabase/shared/client.test.ts b/api.planx.uk/modules/analytics/metabase/shared/client.test.ts index 6746ab159c..f16d2bd196 100644 --- a/api.planx.uk/modules/analytics/metabase/shared/client.test.ts +++ b/api.planx.uk/modules/analytics/metabase/shared/client.test.ts @@ -3,6 +3,7 @@ import { validateConfig, createMetabaseClient, MetabaseError, + initializeMetabaseClient, } from "./client.js"; import nock from "nock"; @@ -12,6 +13,9 @@ describe("Metabase client", () => { beforeEach(() => { vi.clearAllMocks(); vi.resetModules(); + process.env.METABASE_URL_EXT = "https://test-metabase-url.com"; + process.env.METABASE_API_KEY = "test-api-key"; + initializeMetabaseClient(); }); afterEach(() => { diff --git a/api.planx.uk/modules/analytics/metabase/shared/client.ts b/api.planx.uk/modules/analytics/metabase/shared/client.ts index cd0048a25a..5691d2083b 100644 --- a/api.planx.uk/modules/analytics/metabase/shared/client.ts +++ b/api.planx.uk/modules/analytics/metabase/shared/client.ts @@ -120,7 +120,10 @@ export const createMetabaseClient = (): AxiosInstance => { return client; }; -// Export singleton instance -export const metabaseClient = createMetabaseClient(); +// Export both client and instance with delayed instantiation for test purposes +export let metabaseClient: AxiosInstance; -export default metabaseClient; +export const initializeMetabaseClient = () => { + metabaseClient = createMetabaseClient(); + return metabaseClient; +};