From f95c1f96de5ebcdb651ad0588938c06db9b21e10 Mon Sep 17 00:00:00 2001 From: kyoungminkim Date: Fri, 22 Mar 2024 17:37:12 +0900 Subject: [PATCH] test(@nestjs/config) add more tests for priority These answer following questions. both .env file and load configuration, what's the priority? both multiple load configurations, what's the priority? --- tests/e2e/load-priority.spec.ts | 41 +++++++++++++++++++++++++++++++++ tests/src/app.module.ts | 31 +++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/tests/e2e/load-priority.spec.ts b/tests/e2e/load-priority.spec.ts index 1a56a0c4..50b59f56 100644 --- a/tests/e2e/load-priority.spec.ts +++ b/tests/e2e/load-priority.spec.ts @@ -66,6 +66,47 @@ describe('Environment variables and .env files', () => { }); }); + describe('with conflicts of .env file and loaded configuration', () => { + beforeAll(async () => { + const moduleRef = await Test.createTestingModule({ + imports: [ + AppModule.withEnvVarsAndLoadedConfigurations([ + () => ({ PORT: '8000' }), + ]), + ], + }).compile(); + + app = moduleRef.createNestApplication(); + await app.init(); + }); + + it('should choose .env file vars over load configuration', () => { + const configService = app.get(ConfigService); + expect(configService.get('PORT')).toEqual('4000'); + }); + }); + + describe('with conflicts of multiple loaded configurations', () => { + beforeAll(async () => { + const moduleRef = await Test.createTestingModule({ + imports: [ + AppModule.withDynamicLoadedConfigurations([ + () => ({ PORT: '8000' }), + () => ({ PORT: '9000' }), + ]), + ], + }).compile(); + + app = moduleRef.createNestApplication(); + await app.init(); + }); + + it('should choose last load configuration', () => { + const configService = app.get(ConfigService); + expect(configService.get('PORT')).toEqual('9000'); + }); + }); + afterEach(async () => { process.env = { ...envBackup, diff --git a/tests/src/app.module.ts b/tests/src/app.module.ts index 82df44c4..a7c4cb47 100644 --- a/tests/src/app.module.ts +++ b/tests/src/app.module.ts @@ -1,7 +1,7 @@ import { DynamicModule, Inject, Module, Optional } from '@nestjs/common'; import Joi from 'joi'; import { join } from 'path'; -import { ConfigType } from '../../lib'; +import { ConfigFactory, ConfigType } from '../../lib'; import { ConfigModule } from '../../lib/config.module'; import { ConfigService } from '../../lib/config.service'; import databaseConfig from './database.config'; @@ -98,7 +98,21 @@ export class AppModule { imports: [ ConfigModule.forRoot({ envFilePath: join(__dirname, '.env.expanded'), - expandVariables: { ignoreProcessEnv: true } + expandVariables: { ignoreProcessEnv: true }, + }), + ], + }; + } + + static withEnvVarsAndLoadedConfigurations( + configFactory: ConfigFactory[], + ): DynamicModule { + return { + module: AppModule, + imports: [ + ConfigModule.forRoot({ + envFilePath: join(__dirname, '.env'), + load: configFactory, }), ], }; @@ -137,6 +151,19 @@ export class AppModule { }; } + static withDynamicLoadedConfigurations( + configFactory: ConfigFactory[], + ): DynamicModule { + return { + module: AppModule, + imports: [ + ConfigModule.forRoot({ + load: configFactory, + }), + ], + }; + } + static withSchemaValidation( envFilePath?: string, ignoreEnvFile?: boolean,