From 760e97eee5463caa32024a2c89df004623e37674 Mon Sep 17 00:00:00 2001 From: simeng-li Date: Thu, 10 Aug 2023 14:08:13 +0800 Subject: [PATCH] refactor(cloud): clarify the isTest scope (#4310) clarify the isTest scope --- packages/core/src/env-set/create-pool.ts | 8 ++++++-- packages/core/src/env-set/index.ts | 12 ++++++------ packages/shared/src/node/env/GlobalValues.ts | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/core/src/env-set/create-pool.ts b/packages/core/src/env-set/create-pool.ts index a8e60283e..f0863109f 100644 --- a/packages/core/src/env-set/create-pool.ts +++ b/packages/core/src/env-set/create-pool.ts @@ -2,9 +2,13 @@ import { assert } from '@silverhand/essentials'; import { createMockPool, createMockQueryResult, createPool, parseDsn } from 'slonik'; import { createInterceptors } from 'slonik-interceptor-preset'; -const createPoolByEnv = async (databaseDsn: string, isTest: boolean, poolSize?: number) => { +const createPoolByEnv = async ( + databaseDsn: string, + mockDatabaseConnection: boolean, + poolSize?: number +) => { // Database connection is disabled in unit test environment - if (isTest) { + if (mockDatabaseConnection) { return createMockPool({ query: async () => createMockQueryResult([]) }); } diff --git a/packages/core/src/env-set/index.ts b/packages/core/src/env-set/index.ts index 5bab9b686..7fb599dac 100644 --- a/packages/core/src/env-set/index.ts +++ b/packages/core/src/env-set/index.ts @@ -29,15 +29,15 @@ export class EnvSet { /** The value set for global configurations. */ static values = new GlobalValues(); - static get isTest() { - return this.values.isTest; - } - static get dbUrl() { return this.values.dbUrl; } - static sharedPool = createPoolByEnv(this.dbUrl, this.isTest, this.values.databasePoolSize); + static sharedPool = createPoolByEnv( + this.dbUrl, + EnvSet.values.isUnitTest, + this.values.databasePoolSize + ); #pool: Optional; #oidc: Optional>>; @@ -66,7 +66,7 @@ export class EnvSet { async load() { const pool = await createPoolByEnv( this.databaseUrl, - EnvSet.isTest, + EnvSet.values.isUnitTest, EnvSet.values.databasePoolSize ); diff --git a/packages/shared/src/node/env/GlobalValues.ts b/packages/shared/src/node/env/GlobalValues.ts index fb56f585a..d6dcab756 100644 --- a/packages/shared/src/node/env/GlobalValues.ts +++ b/packages/shared/src/node/env/GlobalValues.ts @@ -5,8 +5,8 @@ import { throwErrorWithDsnMessage } from './throw-errors.js'; export default class GlobalValues { public readonly isProduction = getEnv('NODE_ENV') === 'production'; - public readonly isTest = getEnv('NODE_ENV') === 'test'; public readonly isIntegrationTest = yes(getEnv('INTEGRATION_TEST')); + public readonly isUnitTest = getEnv('NODE_ENV') === 'test'; public readonly httpsCert = process.env.HTTPS_CERT_PATH; public readonly httpsKey = process.env.HTTPS_KEY_PATH;