From 32afc950cc3d90f726a7433b4758a16a9d7d054f Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 22 Oct 2024 11:37:20 +0200 Subject: [PATCH] refactor: mock contexts constants to be able to set different values on tests (#9529) refactor: mock contexts constants to be able to set different values o tests Signed-off-by: Philippe Martin --- .../kubernetes/contexts-manager.spec.ts | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/main/src/plugin/kubernetes/contexts-manager.spec.ts b/packages/main/src/plugin/kubernetes/contexts-manager.spec.ts index e2d2ecdd5b5af..ce3c7f379beb1 100644 --- a/packages/main/src/plugin/kubernetes/contexts-manager.spec.ts +++ b/packages/main/src/plugin/kubernetes/contexts-manager.spec.ts @@ -95,14 +95,40 @@ vi.mock('@kubernetes/client-node', async importOriginal => { }; }); -// Needs to mock these values to make the backoff much longer than other timeouts, so connection are never retried during the tests +const connectTimeoutMock = vi.fn(); +const backoffInitialValueMock = vi.fn(); +const backoffMultiplierMock = vi.fn(); +const backoffMultiplierCurrentContextMock = vi.fn(); +const backoffLimitMock = vi.fn(); +const backoffLimitCurrentContextMock = vi.fn(); +const backoffJitterMock = vi.fn(); +const dispatchTimeoutMock = vi.fn(); vi.mock('./contexts-constants.js', () => { return { - connectTimeout: 1, - backoffInitialValue: 10000, - backoffLimit: 1000, - backoffJitter: 0, - dispatchTimeout: 1, + get connectTimeout(): number { + return connectTimeoutMock(); + }, + get backoffInitialValue(): number { + return backoffInitialValueMock(); + }, + get backoffMultiplier(): number { + return backoffMultiplierMock(); + }, + get backoffMultiplierCurrentContext(): number { + return backoffMultiplierCurrentContextMock(); + }, + get backoffLimit(): number { + return backoffLimitMock(); + }, + get backoffLimitCurrentContext(): number { + return backoffLimitCurrentContextMock(); + }, + get backoffJitter(): number { + return backoffJitterMock(); + }, + get dispatchTimeout(): number { + return dispatchTimeoutMock(); + }, }; }); @@ -112,11 +138,20 @@ const consoleDebugMock = vi.fn(); beforeEach(() => { console.debug = consoleDebugMock; vi.useFakeTimers(); + // Needs to mock these values to make the backoff much longer than other timeouts, so connection are never retried during the tests + connectTimeoutMock.mockReturnValue(1); + backoffInitialValueMock.mockReturnValue(10000); + backoffMultiplierMock.mockReturnValue(2.0); + backoffMultiplierCurrentContextMock.mockReturnValue(1.2); + backoffLimitMock.mockReturnValue(1000); + backoffLimitCurrentContextMock.mockReturnValue(1000); + backoffJitterMock.mockReturnValue(0); + dispatchTimeoutMock.mockReturnValue(1); }); afterEach(() => { console.debug = originalConsoleDebug; - vi.resetAllMocks(); + vi.clearAllMocks(); }); describe('update', async () => {