File tree Expand file tree Collapse file tree 2 files changed +70
-2
lines changed Expand file tree Collapse file tree 2 files changed +70
-2
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,47 @@ describe('Environment variables and .env files', () => {
66
66
} ) ;
67
67
} ) ;
68
68
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
+
69
110
afterEach ( async ( ) => {
70
111
process . env = {
71
112
...envBackup ,
Original file line number Diff line number Diff line change 1
1
import { DynamicModule , Inject , Module , Optional } from '@nestjs/common' ;
2
2
import Joi from 'joi' ;
3
3
import { join } from 'path' ;
4
- import { ConfigType } from '../../lib' ;
4
+ import { ConfigFactory , ConfigType } from '../../lib' ;
5
5
import { ConfigModule } from '../../lib/config.module' ;
6
6
import { ConfigService } from '../../lib/config.service' ;
7
7
import databaseConfig from './database.config' ;
@@ -98,7 +98,21 @@ export class AppModule {
98
98
imports : [
99
99
ConfigModule . forRoot ( {
100
100
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 ,
102
116
} ) ,
103
117
] ,
104
118
} ;
@@ -137,6 +151,19 @@ export class AppModule {
137
151
} ;
138
152
}
139
153
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
+
140
167
static withSchemaValidation (
141
168
envFilePath ?: string ,
142
169
ignoreEnvFile ?: boolean ,
You can’t perform that action at this time.
0 commit comments