Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface DotenvParseOutput {
}

export interface DotenvPopulateOutput {
[name: string]: string;
[name: string]: string | undefined;
}

export interface DotenvParseOptions {
Expand Down
12 changes: 12 additions & 0 deletions tests/test-populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ t.test('takes processEnv and check if all keys applied to processEnv', ct => {
ct.same(parsed, processEnv)
})

t.test('preserves undefined input values in the target and returned object', ct => {
for (const processEnv of [{}, { OPTIONAL: 'existing' }]) {
const populated = dotenv.populate(processEnv, { OPTIONAL: undefined }, { override: true })

ct.same(processEnv, { OPTIONAL: undefined })
ct.same(populated, { OPTIONAL: undefined })
ct.ok(Object.prototype.hasOwnProperty.call(processEnv, 'OPTIONAL'))
ct.ok(Object.prototype.hasOwnProperty.call(populated, 'OPTIONAL'))
}
ct.end()
})

t.test('does not write over keys already in processEnv', ct => {
ct.plan(1)

Expand Down
9 changes: 9 additions & 0 deletions tests/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ populate(target, { DB_HOST: "localhost" }, { debug: true });
populate(target, { DB_HOST: "localhost" }, { override: true });
populate(target, { DB_HOST: "localhost" }, { debug: true, override: false });

// populate() preserves undefined values from DotenvPopulateInput in its result.
const populated = populate({}, { OPTIONAL: undefined });
const optionalValue: string | undefined = populated["OPTIONAL"];
// @ts-expect-error A populated value is not guaranteed to be a string.
const requiredValue: string = populated["OPTIONAL"];
if (optionalValue !== undefined) {
optionalValue.trim();
}

// config() reports a missing .env file by returning the ENOENT that fs threw,
// so comparing error.code against it must type-check
if (env.error?.code === "ENOENT") {
Expand Down
Loading