In the current setup, there is no straightforward way to temporarily override environment variables for the duration of a test suite. This can lead to issues where tests inadvertently affect each other due to shared environment settings, making it difficult to isolate and reproduce test failures.
Additionally, in many tests environment variables are overridden without being reverted, propagating this behavior to other tests and making the tests dependent on their order and leading to chain-failures in cases where only one of the tests actually fails.
Solution
Add new helper methods overrideEnvs and withOverriddenEnvs provide a mechanism to temporarily set environment variables for the duration of a test or a group of tests. These methods ensure that the original environment settings are restored after the tests have run, maintaining the integrity of the global environment.
overrideEnvs: Temporarily sets environment variables before a test and restores them afterward.
withOverriddenEnvs: A higher-level function that uses overrideEnvs to set environment variables for a group of tests defined within a callback function.
Examples:
// Example usage of overrideEnvs
describe('test suite', () => {
overrideEnvs({ TEST: 'true', API_URL: 'http://localhost' });
it('should run with overridden env variables', () => {
});
it('should run with overridden env variables', () => {
});
});
it('should run with original env variables', () => {
});
// Example usage of withOverriddenEnvs
withOverriddenEnvs({ TEST: 'false', API_URL: 'http://testnet' }, () => {
it('should run with overridden env variables', () => {
});
it('should run with overridden env variables', () => {
});
});
it('should run with original env variables', () => {
});
Problem
In the current setup, there is no straightforward way to temporarily override environment variables for the duration of a test suite. This can lead to issues where tests inadvertently affect each other due to shared environment settings, making it difficult to isolate and reproduce test failures.
Additionally, in many tests environment variables are overridden without being reverted, propagating this behavior to other tests and making the tests dependent on their order and leading to chain-failures in cases where only one of the tests actually fails.
Solution
Add new helper methods
overrideEnvs
andwithOverriddenEnvs
provide a mechanism to temporarily set environment variables for the duration of a test or a group of tests. These methods ensure that the original environment settings are restored after the tests have run, maintaining the integrity of the global environment.overrideEnvs
: Temporarily sets environment variables before a test and restores them afterward.withOverriddenEnvs
: A higher-level function that usesoverrideEnvs
to set environment variables for a group of tests defined within a callback function.Examples:
Alternatives
No response