forked from nestjs/config
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set env dynamically (nestjs#1476)
- Loading branch information
1 parent
0838552
commit 761ef89
Showing
3 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ConfigModule } from '../../lib'; | ||
import { AppModule } from '../src/app.module'; | ||
import { ConfigService } from '../../lib'; | ||
|
||
describe('Setting environment variables', () => { | ||
let app: INestApplication; | ||
let module: TestingModule; | ||
let originalEnv: NodeJS.ProcessEnv; | ||
|
||
beforeEach(async () => { | ||
originalEnv = { ...process.env }; | ||
|
||
module = await Test.createTestingModule({ | ||
imports: [AppModule.withExpandedEnvVars()], | ||
}).compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
it('should return updated value after set', async () => { | ||
const prevUrl = module.get(ConfigService).get('URL'); | ||
|
||
module.get(ConfigService).set('URL', 'yourapp.test'); | ||
|
||
const updatedUrl = module.get(ConfigService).get('URL'); | ||
|
||
expect(prevUrl).toEqual('myapp.test'); | ||
expect(updatedUrl).toEqual('yourapp.test'); | ||
}); | ||
|
||
it('should return value after set', async () => { | ||
const undefinedEnv = module.get(ConfigService).get('UNDEFINED_ENV'); | ||
|
||
module.get(ConfigService).set('UNDEFINED_ENV', 'defined'); | ||
|
||
const definedEnv = module.get(ConfigService).get('UNDEFINED_ENV'); | ||
|
||
expect(undefinedEnv).toEqual(undefined); | ||
expect(definedEnv).toEqual('defined'); | ||
}); | ||
|
||
it('should return updated value with interpolation after set', async () => { | ||
const prevUrl = module.get(ConfigService).get('URL'); | ||
const prevEmail = module.get(ConfigService).get('EMAIL'); | ||
|
||
module.get(ConfigService).set('URL', 'yourapp.test'); | ||
|
||
const updatedUrl = module.get(ConfigService).get('URL'); | ||
const updatedEmail = module.get(ConfigService).get('EMAIL'); | ||
|
||
expect(prevUrl).toEqual('myapp.test'); | ||
expect(prevEmail).toEqual('[email protected]'); | ||
expect(updatedUrl).toEqual('yourapp.test'); | ||
expect(updatedEmail).toEqual('[email protected]'); | ||
}); | ||
|
||
it(`should return updated process.env property after set`, async () => { | ||
await ConfigModule.envVariablesLoaded; | ||
|
||
module.get(ConfigService).set('URL', 'yourapp.test'); | ||
|
||
const envVars = app.get(AppModule).getEnvVariables(); | ||
|
||
expect(envVars.URL).toEqual('yourapp.test'); | ||
expect(envVars.EMAIL).toEqual('[email protected]'); | ||
}); | ||
|
||
afterEach(async () => { | ||
process.env = originalEnv; | ||
await app.close(); | ||
}); | ||
}); |