Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

victor-yanev
Copy link
Contributor

Description:

Related issue(s):

Fixes #

Notes for reviewer:

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

@victor-yanev victor-yanev added enhancement New feature or request Technical Debt Issue which resolves technical debt labels Sep 20, 2024
@victor-yanev victor-yanev self-assigned this Sep 20, 2024
Copy link

github-actions bot commented Sep 20, 2024

Acceptance Tests

0 tests   0 ✔️  0s ⏱️
0 suites  0 💤
0 files    0

Results for commit 7e74328.

♻️ This comment has been updated with latest results.

Signed-off-by: Victor Yanev <[email protected]>
Copy link

sonarcloud bot commented Sep 20, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
9.1% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

Copy link

codecov bot commented Sep 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 19.98%. Comparing base (4d727db) to head (7e74328).

❗ There is a different number of reports uploaded between BASE (4d727db) and HEAD (7e74328). Click for more details.

HEAD has 3 uploads less than BASE
Flag BASE (4d727db) HEAD (7e74328)
server 1 0
ws-server 1 0
1 0
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     
Flag Coverage Δ
relay 19.98% <ø> (-70.17%) ⬇️
server ?
ws-server ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../lib/services/ethService/ethFilterService/index.ts 16.25% <ø> (-68.75%) ⬇️

... and 55 files with indirect coverage changes

Comment on lines +953 to +962
before(() => {
for (const key in envs) {
envsToReset[key] = process.env[key];
overrideEnv(process.env, key, envs[key]);
}
});

tests();

after(() => {
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Comment on lines +924 to +968
/**
* 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]);
}
});
});
};
Copy link
Contributor

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)

Suggested change
/**
* 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;
}
});
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Technical Debt Issue which resolves technical debt
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

2 participants