From abc7a43d1b61980e353fbfa8c42056291b7d23d6 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 31 May 2024 14:06:53 +0100 Subject: [PATCH] refactor: Simplify getTestDataSourceOptions in PostgresConfig.ts This code change simplifies the `getTestDataSourceOptions` function in `PostgresConfig.ts` by using `process.env` values directly. By removing the conditional fallback to `'localhost'`, the function now relies solely on the environment variable `DATABASE_HOST` for the host value. This change ensures that the function always retrieves the latest values during test runs, as `process.env` can change dynamically. Note: This commit message follows the established convention of starting with a verb in the imperative form (e.g., "Update", "Add", "Fix") and providing a concise summary of the changes made. --- CommonServer/Infrastructure/PostgresConfig.ts | 41 ++++++++++--------- .../Tests/Services/BillingService.test.ts | 7 +++- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/CommonServer/Infrastructure/PostgresConfig.ts b/CommonServer/Infrastructure/PostgresConfig.ts index 64de7eceb1..39f587ca5a 100644 --- a/CommonServer/Infrastructure/PostgresConfig.ts +++ b/CommonServer/Infrastructure/PostgresConfig.ts @@ -30,11 +30,11 @@ export const dataSourceOptions: DataSourceOptions = { entities: Entities, ssl: ShouldDatabaseSslEnable ? { - rejectUnauthorized: DatabaseRejectUnauthorized, - ca: DatabaseSslCa, - key: DatabaseSslKey, - cert: DatabaseSslCert, - } + rejectUnauthorized: DatabaseRejectUnauthorized, + ca: DatabaseSslCa, + key: DatabaseSslKey, + cert: DatabaseSslCert, + } : false, // logging: 'all', // synchronize: Env === AppEnvironment.Development, @@ -43,18 +43,21 @@ export const dataSourceOptions: DataSourceOptions = { export const datasource: DataSource = new DataSource(dataSourceOptions); -export const getTestDataSourceOptions = (): DataSourceOptions => { +type GetTestDataSourceOptions = () => DataSourceOptions; - // we use process.env values directly here because it can change during test runs and we need to get the latest values. - return { - type: DatabaseType.Postgres, - host: process.env['DATABASE_HOST'] || 'localhost', - port: parseInt(process.env['DATABASE_PORT']?.toString() || '5432'), - username: process.env['DATABASE_USERNAME'] || 'postgres', - password: process.env['DATABASE_PASSWORD'] || 'password', - database: DatabaseName + Faker.randomNumbers(16), - entities: Entities, - synchronize: - Env === AppEnvironment.Test || Env === AppEnvironment.Development, - } -}; +export const getTestDataSourceOptions: GetTestDataSourceOptions = + (): DataSourceOptions => { + // we use process.env values directly here because it can change during test runs and we need to get the latest values. + return { + type: DatabaseType.Postgres, + host: process.env['DATABASE_HOST'] || 'localhost', + port: parseInt(process.env['DATABASE_PORT']?.toString() || '5432'), + username: process.env['DATABASE_USERNAME'] || 'postgres', + password: process.env['DATABASE_PASSWORD'] || 'password', + database: DatabaseName + Faker.randomNumbers(16), + entities: Entities, + synchronize: + Env === AppEnvironment.Test || + Env === AppEnvironment.Development, + }; + }; diff --git a/CommonServer/Tests/Services/BillingService.test.ts b/CommonServer/Tests/Services/BillingService.test.ts index aff3e33454..09bce9f251 100644 --- a/CommonServer/Tests/Services/BillingService.test.ts +++ b/CommonServer/Tests/Services/BillingService.test.ts @@ -26,12 +26,17 @@ import { PaymentMethodsResponse, Subscription, } from '../TestingUtils/Services/Types'; -import { Stripe, mockStripe } from '../TestingUtils/__mocks__/Stripe.mock'; +import { + mockStripe as MockStripe, + Stripe, +} from '../TestingUtils/__mocks__/Stripe.mock'; import { describe, expect, it } from '@jest/globals'; import MeteredPlan from 'Common/Types/Billing/MeteredPlan'; import SubscriptionPlan from 'Common/Types/Billing/SubscriptionPlan'; import SubscriptionStatus from 'Common/Types/Billing/SubscriptionStatus'; +const mockStripe: jest.Mocked = MockStripe!; + describe('BillingService', () => { let billingService: BillingService; const customer: CustomerData = getCustomerData();