hashgraph / hedera-json-rpc-relay

Implementation of Ethereum JSON-RPC APIs for Hedera
Apache License 2.0
48 stars 62 forks source link

Add Helper Methods for Overriding Environment Variables in Tests #3029

Open victor-yanev opened 1 week ago

victor-yanev commented 1 week ago

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 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.

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', () => {
});

Alternatives

No response