Skip to content

Commit 93e5f70

Browse files
Merge pull request #1666 from bejewel-kyoungmin/test-priority-env-load-configuration
test(@nestjs/config) add more tests for priority
2 parents 15dcdfa + f95c1f9 commit 93e5f70

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

tests/e2e/load-priority.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,47 @@ describe('Environment variables and .env files', () => {
6666
});
6767
});
6868

69+
describe('with conflicts of .env file and loaded configuration', () => {
70+
beforeAll(async () => {
71+
const moduleRef = await Test.createTestingModule({
72+
imports: [
73+
AppModule.withEnvVarsAndLoadedConfigurations([
74+
() => ({ PORT: '8000' }),
75+
]),
76+
],
77+
}).compile();
78+
79+
app = moduleRef.createNestApplication();
80+
await app.init();
81+
});
82+
83+
it('should choose .env file vars over load configuration', () => {
84+
const configService = app.get(ConfigService);
85+
expect(configService.get('PORT')).toEqual('4000');
86+
});
87+
});
88+
89+
describe('with conflicts of multiple loaded configurations', () => {
90+
beforeAll(async () => {
91+
const moduleRef = await Test.createTestingModule({
92+
imports: [
93+
AppModule.withDynamicLoadedConfigurations([
94+
() => ({ PORT: '8000' }),
95+
() => ({ PORT: '9000' }),
96+
]),
97+
],
98+
}).compile();
99+
100+
app = moduleRef.createNestApplication();
101+
await app.init();
102+
});
103+
104+
it('should choose last load configuration', () => {
105+
const configService = app.get(ConfigService);
106+
expect(configService.get('PORT')).toEqual('9000');
107+
});
108+
});
109+
69110
afterEach(async () => {
70111
process.env = {
71112
...envBackup,

tests/src/app.module.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DynamicModule, Inject, Module, Optional } from '@nestjs/common';
22
import Joi from 'joi';
33
import { join } from 'path';
4-
import { ConfigType } from '../../lib';
4+
import { ConfigFactory, ConfigType } from '../../lib';
55
import { ConfigModule } from '../../lib/config.module';
66
import { ConfigService } from '../../lib/config.service';
77
import databaseConfig from './database.config';
@@ -98,7 +98,21 @@ export class AppModule {
9898
imports: [
9999
ConfigModule.forRoot({
100100
envFilePath: join(__dirname, '.env.expanded'),
101-
expandVariables: { ignoreProcessEnv: true }
101+
expandVariables: { ignoreProcessEnv: true },
102+
}),
103+
],
104+
};
105+
}
106+
107+
static withEnvVarsAndLoadedConfigurations(
108+
configFactory: ConfigFactory[],
109+
): DynamicModule {
110+
return {
111+
module: AppModule,
112+
imports: [
113+
ConfigModule.forRoot({
114+
envFilePath: join(__dirname, '.env'),
115+
load: configFactory,
102116
}),
103117
],
104118
};
@@ -137,6 +151,19 @@ export class AppModule {
137151
};
138152
}
139153

154+
static withDynamicLoadedConfigurations(
155+
configFactory: ConfigFactory[],
156+
): DynamicModule {
157+
return {
158+
module: AppModule,
159+
imports: [
160+
ConfigModule.forRoot({
161+
load: configFactory,
162+
}),
163+
],
164+
};
165+
}
166+
140167
static withSchemaValidation(
141168
envFilePath?: string,
142169
ignoreEnvFile?: boolean,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy