From b2205097dabff23904e235e3ece4f5fb43efa4d7 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Wed, 18 Oct 2023 15:45:54 +0200 Subject: [PATCH] test: Implement the structure of mocking appConfig options --- packages/backend/test/setup/global-hooks.ts | 11 +++++++++++ packages/backend/test/setup/set-mock-config.ts | 13 +++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 packages/backend/test/setup/set-mock-config.ts diff --git a/packages/backend/test/setup/global-hooks.ts b/packages/backend/test/setup/global-hooks.ts index e83f6204..80d72630 100644 --- a/packages/backend/test/setup/global-hooks.ts +++ b/packages/backend/test/setup/global-hooks.ts @@ -1,6 +1,16 @@ import { Model } from 'objection'; import { client as knex } from '../../src/config/database'; import logger from '../../src/helpers/logger'; +import { mockConfigState, resetMockConfig } from './set-mock-config'; + +jest.mock('../../src/config/app', () => ({ + ...jest.requireActual('../../src/config/app').default, + get isCloud() { + return mockConfigState.isCloud !== undefined + ? mockConfigState.isCloud + : jest.requireActual('../../src/config/app').default.isCloud; + }, +})); global.beforeAll(async () => { global.knex = null; @@ -23,6 +33,7 @@ global.afterEach(async () => { Model.knex(knex); jest.clearAllMocks(); + resetMockConfig(); }); global.afterAll(async () => { diff --git a/packages/backend/test/setup/set-mock-config.ts b/packages/backend/test/setup/set-mock-config.ts new file mode 100644 index 00000000..954a93e3 --- /dev/null +++ b/packages/backend/test/setup/set-mock-config.ts @@ -0,0 +1,13 @@ +export const mockConfigState = { + isCloud: false, +}; + +export const resetMockConfig = () => { + for (const key in mockConfigState) { + delete (mockConfigState as any)[key]; + } +}; + +export const setMockConfig = (config: Partial) => { + Object.assign(mockConfigState, config); +};