diff --git a/README.md b/README.md index a924fb56..825342d1 100644 --- a/README.md +++ b/README.md @@ -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; } ``` diff --git a/jest.config.json b/jest.config.json index 63479692..a0230b10 100644 --- a/jest.config.json +++ b/jest.config.json @@ -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" }, diff --git a/lib/loader/directory-loader.ts b/lib/loader/directory-loader.ts index 60bc2577..3df6826c 100644 --- a/lib/loader/directory-loader.ts +++ b/lib/loader/directory-loader.ts @@ -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; } /** diff --git a/tests/e2e/directory.spec.ts b/tests/e2e/directory.spec.ts index 60598400..4921a73b 100644 --- a/tests/e2e/directory.spec.ts +++ b/tests/e2e/directory.spec.ts @@ -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(); }); }); diff --git a/tests/src/app.module.ts b/tests/src/app.module.ts index 7b9e6f6e..45ab7f01 100644 --- a/tests/src/app.module.ts +++ b/tests/src/app.module.ts @@ -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, diff --git a/tests/src/dir_sub/database.toml b/tests/src/dir_sub/database.toml new file mode 100644 index 00000000..2b3dc320 --- /dev/null +++ b/tests/src/dir_sub/database.toml @@ -0,0 +1,5 @@ +host = '127.0.0.1' +port = 3000 + +[table] +name = 'test' \ No newline at end of file diff --git a/tests/src/dir_sub/table.toml b/tests/src/dir_sub/table.toml new file mode 100644 index 00000000..25a3e4d7 --- /dev/null +++ b/tests/src/dir_sub/table.toml @@ -0,0 +1 @@ +name = "${TABLE_NAME}" \ No newline at end of file