-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add helper method for overriding env variables #3022
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Victor Yanev <[email protected]>
Signed-off-by: Victor Yanev <[email protected]>
Quality Gate failedFailed conditions |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3022 +/- ##
===========================================
- Coverage 88.37% 19.98% -68.40%
===========================================
Files 61 41 -20
Lines 4053 3068 -985
Branches 815 621 -194
===========================================
- Hits 3582 613 -2969
- Misses 430 2446 +2016
+ Partials 41 9 -32
Flags with carried forward coverage won't be shown. Click here to find out more.
|
before(() => { | ||
for (const key in envs) { | ||
envsToReset[key] = process.env[key]; | ||
overrideEnv(process.env, key, envs[key]); | ||
} | ||
}); | ||
|
||
tests(); | ||
|
||
after(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe mocha allows the user to have multiple before
and after
. Maybe we could use a smaller wrapper that only invokes before
and after
to set only 1 env variable. I guess this way might be more composable. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true and the tests()
method that is passed can also include before
, beforeEach
, afterEach
, and after
in addition to it
and describe
declarations, I don't see a problem with there being other before
and after
called inside.
I am not sure what you mean, could you maybe give some examples?
/** | ||
* Overrides an environment variable in the provided {@link NodeJS.Dict} object. | ||
* | ||
* @param {NodeJS.Dict<string>} object - The object containing the environment variables. | ||
* @param {string} key - The key of the environment variable to override. | ||
* @param {string | undefined} value - The value to set the environment variable to. | ||
*/ | ||
export const overrideEnv = (object: NodeJS.Dict<string>, key: string, value: string | undefined) => { | ||
if (value === undefined) { | ||
delete object[key]; | ||
} else { | ||
object[key] = value; | ||
} | ||
}; | ||
|
||
/** | ||
* Overrides environment variables for the duration of the provided tests. | ||
* | ||
* @param {NodeJS.Dict<string>} envs - An object containing key-value pairs of environment variables to set. | ||
* @param {Function} tests - A function containing the tests to run with the overridden environment variables. | ||
*/ | ||
export const withOverriddenEnvs = (envs: NodeJS.Dict<string>, tests: () => void) => { | ||
const overriddenEnvs = Object.entries(envs) | ||
.map(([key, value]) => `${key}=${value}`) | ||
.join(', '); | ||
|
||
describe(`given ${overriddenEnvs} are set`, () => { | ||
let envsToReset: NodeJS.Dict<string> = {}; | ||
|
||
before(() => { | ||
for (const key in envs) { | ||
envsToReset[key] = process.env[key]; | ||
overrideEnv(process.env, key, envs[key]); | ||
} | ||
}); | ||
|
||
tests(); | ||
|
||
after(() => { | ||
for (const key in envsToReset) { | ||
overrideEnv(process.env, key, envsToReset[key]); | ||
} | ||
}); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking something like this (and for each env var you want to modify you need to call this method in the test)
/** | |
* Overrides an environment variable in the provided {@link NodeJS.Dict} object. | |
* | |
* @param {NodeJS.Dict<string>} object - The object containing the environment variables. | |
* @param {string} key - The key of the environment variable to override. | |
* @param {string | undefined} value - The value to set the environment variable to. | |
*/ | |
export const overrideEnv = (object: NodeJS.Dict<string>, key: string, value: string | undefined) => { | |
if (value === undefined) { | |
delete object[key]; | |
} else { | |
object[key] = value; | |
} | |
}; | |
/** | |
* Overrides environment variables for the duration of the provided tests. | |
* | |
* @param {NodeJS.Dict<string>} envs - An object containing key-value pairs of environment variables to set. | |
* @param {Function} tests - A function containing the tests to run with the overridden environment variables. | |
*/ | |
export const withOverriddenEnvs = (envs: NodeJS.Dict<string>, tests: () => void) => { | |
const overriddenEnvs = Object.entries(envs) | |
.map(([key, value]) => `${key}=${value}`) | |
.join(', '); | |
describe(`given ${overriddenEnvs} are set`, () => { | |
let envsToReset: NodeJS.Dict<string> = {}; | |
before(() => { | |
for (const key in envs) { | |
envsToReset[key] = process.env[key]; | |
overrideEnv(process.env, key, envs[key]); | |
} | |
}); | |
tests(); | |
after(() => { | |
for (const key in envsToReset) { | |
overrideEnv(process.env, key, envsToReset[key]); | |
} | |
}); | |
}); | |
}; | |
export const overriddeEnv = (key: string, value: string | undefined) => { | |
let prevValue: string | undefined; | |
before(() => { | |
prevValue = process.env[key]; | |
process.env[key] = value; | |
}); | |
after(() => { | |
if (prevValue === undefined) { | |
delete process.env[key]; | |
} else { | |
process.env[key] = prevValue; | |
} | |
}); | |
}; |
Description:
Related issue(s):
Fixes #
Notes for reviewer:
Checklist