Skip to content

Commit

Permalink
fix: add ignoreEnvironmentVariableSubstitution for directoryLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikaple committed Mar 28, 2022
1 parent 631b2ab commit 60abf39
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ export interface DirectoryLoaderOptions extends OptionsSync {
* File regex to include.
*/
include?: RegExp;
/**
* If "true", ignore environment variable substitution.
* Default: true
*/
ignoreEnvironmentVariableSubstitution?: boolean;
}
```

Expand Down
8 changes: 8 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"testEnvironment": "node",
"testRegex": ".spec.ts$",
"collectCoverageFrom": ["./lib/**/*.ts"],
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
Expand Down
5 changes: 5 additions & 0 deletions lib/loader/directory-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export interface DirectoryLoaderOptions extends OptionsSync {
* File regex to include.
*/
include?: RegExp;
/**
* If "true", ignore environment variable substitution.
* Default: true
*/
ignoreEnvironmentVariableSubstitution?: boolean;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/e2e/directory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,23 @@ describe('Directory loader', () => {
expect(databaseConfig.port).toBe(3000);
});

it(`should be able to substitute env variables`, async () => {
process.env['TABLE_NAME'] = 'table2';
const module = await Test.createTestingModule({
imports: [AppModule.withDirectorySubstitution()],
}).compile();

app = module.createNestApplication();
await app.init();
const config = app.get(DirectoryConfig);
expect(config.table.name).toEqual('table2');

const databaseConfig = app.get(DatabaseConfig);
expect(databaseConfig.port).toBe(3000);
});

afterEach(async () => {
process.env['TABLE_NAME'] = '';
await app?.close();
});
});
15 changes: 15 additions & 0 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ export class AppModule {
};
}

static withDirectorySubstitution(): DynamicModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRoot({
schema: DirectoryConfig,
load: directoryLoader({
directory: join(__dirname, 'dir_sub'),
ignoreEnvironmentVariableSubstitution: false,
}),
}),
],
};
}

static withDotenvNoOption(): DynamicModule {
return {
module: AppModule,
Expand Down
5 changes: 5 additions & 0 deletions tests/src/dir_sub/database.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
host = '127.0.0.1'
port = 3000

[table]
name = 'test'
1 change: 1 addition & 0 deletions tests/src/dir_sub/table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "${TABLE_NAME}"

0 comments on commit 60abf39

Please sign in to comment.