Skip to content

Commit

Permalink
Merge pull request #1666 from bejewel-kyoungmin/test-priority-env-loa…
Browse files Browse the repository at this point in the history
…d-configuration

test(@nestjs/config) add more tests for priority
  • Loading branch information
kamilmysliwiec authored Mar 25, 2024
2 parents 15dcdfa + f95c1f9 commit 93e5f70
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
41 changes: 41 additions & 0 deletions tests/e2e/load-priority.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 29 additions & 2 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
}),
],
};
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 93e5f70

Please sign in to comment.