diff --git a/packages/backend-core/tests/jestSetup.ts b/packages/backend-core/tests/jestSetup.ts index 3e9c9f8bdc..f7887ec824 100644 --- a/packages/backend-core/tests/jestSetup.ts +++ b/packages/backend-core/tests/jestSetup.ts @@ -1,18 +1,4 @@ import env from "../src/environment" +import { testContainerUtils } from "./utilities" -const globalSafe = global as any - -console.error(global) - -env._set( - "COUCH_DB_PORT", - globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_PORT_5984__"] -) -env._set( - "COUCH_DB_URL", - `http://${globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_IP__"]}:${globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_PORT_5984__"]}` -) -env._set( - "MINIO_URL", - `http://${globalSafe["__TESTCONTAINERS_MINIO-SERVICE-1_IP__"]}:${globalSafe["__TESTCONTAINERS_MINIO-SERVICE-1_PORT_9000__"]}` -) +testContainerUtils.setupEnv(env) diff --git a/packages/backend-core/tests/utilities/index.ts b/packages/backend-core/tests/utilities/index.ts index ee96a94152..468d980a7f 100644 --- a/packages/backend-core/tests/utilities/index.ts +++ b/packages/backend-core/tests/utilities/index.ts @@ -2,6 +2,7 @@ export * as mocks from "./mocks" export * as structures from "./structures" export { generator } from "./structures" export * as testEnv from "./testEnv" +export * as testContainerUtils from "./testContainerUtils" import * as dbConfig from "./db" dbConfig.init() diff --git a/packages/backend-core/tests/utilities/testContainerUtils.ts b/packages/backend-core/tests/utilities/testContainerUtils.ts new file mode 100644 index 0000000000..e36d02d464 --- /dev/null +++ b/packages/backend-core/tests/utilities/testContainerUtils.ts @@ -0,0 +1,39 @@ +function getTestContainerSettings(serverName: string, key: string) { + const [_, value] = Object.entries(global).find( + ([k]) => + k.includes(`_${serverName.toUpperCase()}`) && + k.includes(`_${key.toUpperCase()}__`) + )! + return value +} + +function getCouchConfig() { + const port = getTestContainerSettings("COUCHDB-SERVICE", "PORT_5984") + return { + port, + url: `http://${getTestContainerSettings("COUCHDB-SERVICE", "IP")}:${port}`, + } +} + +function getMinioConfig() { + const port = getTestContainerSettings("MINIO-SERVICE", "PORT_9000") + return { + port, + url: `http://${getTestContainerSettings("MINIO-SERVICE", "IP")}:${port}`, + } +} + +export function setupEnv(...envs: any[]) { + const configs = [ + { key: "COUCH_DB_PORT", value: getCouchConfig().port }, + { key: "COUCH_DB_URL", value: getCouchConfig().url }, + { key: "MINIO_PORT", value: getMinioConfig().port }, + { key: "MINIO_URL", value: getMinioConfig().url }, + ] + + for (const config of configs) { + for (const env of envs) { + env._set(config.key, config.value) + } + } +} diff --git a/packages/server/src/tests/jestSetup.ts b/packages/server/src/tests/jestSetup.ts index 4c0e45743d..9b6036467d 100644 --- a/packages/server/src/tests/jestSetup.ts +++ b/packages/server/src/tests/jestSetup.ts @@ -1,5 +1,6 @@ import env from "../environment" import { env as coreEnv } from "@budibase/backend-core" +import { testContainerUtils } from "@budibase/backend-core/tests" if (!process.env.DEBUG) { global.console.log = jest.fn() // console.log are ignored in tests @@ -12,25 +13,4 @@ if (!process.env.CI) { jest.setTimeout(100000) } -function overrideConfigValue(key: string, value: string) { - env._set(key, value) - coreEnv._set(key, value) -} - -const globalSafe = global as any - -console.error(global) - -overrideConfigValue( - "COUCH_DB_PORT", - globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_PORT_5984__"] -) -overrideConfigValue( - "COUCH_DB_URL", - `http://${globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_IP__"]}:${globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_PORT_5984__"]}` -) - -overrideConfigValue( - "MINIO_URL", - `http://${globalSafe["__TESTCONTAINERS_MINIO-SERVICE-1_IP__"]}:${globalSafe["__TESTCONTAINERS_MINIO-SERVICE-1_PORT_9000__"]}` -) +testContainerUtils.setupEnv(env, coreEnv) diff --git a/packages/worker/src/tests/jestSetup.ts b/packages/worker/src/tests/jestSetup.ts index 95b3157f99..790f942a30 100644 --- a/packages/worker/src/tests/jestSetup.ts +++ b/packages/worker/src/tests/jestSetup.ts @@ -1,4 +1,4 @@ -import { mocks } from "@budibase/backend-core/tests" +import { mocks, testContainerUtils } from "@budibase/backend-core/tests" import env from "../environment" import { env as coreEnv } from "@budibase/backend-core" @@ -21,25 +21,4 @@ if (!process.env.CI) { jest.setTimeout(100000) } -function overrideConfigValue(key: string, value: string) { - env._set(key, value) - coreEnv._set(key, value) -} - -const globalSafe = global as any - -console.error(global) - -overrideConfigValue( - "COUCH_DB_PORT", - globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_PORT_5984__"] -) -overrideConfigValue( - "COUCH_DB_URL", - `http://${globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_IP__"]}:${globalSafe["__TESTCONTAINERS_COUCHDB-SERVICE-1_PORT_5984__"]}` -) - -overrideConfigValue( - "MINIO_URL", - `http://${globalSafe["__TESTCONTAINERS_MINIO-SERVICE-1_IP__"]}:${globalSafe["__TESTCONTAINERS_MINIO-SERVICE-1_PORT_9000__"]}` -) +testContainerUtils.setupEnv(env, coreEnv)