Skip to content

Commit 2e2fc07

Browse files
authored
test: add helper method for overriding env variables (#3022)
* test: add helper method for overriding env variables Signed-off-by: Victor Yanev <[email protected]> * chore: improve readability Signed-off-by: Victor Yanev <[email protected]> * fix: usages Signed-off-by: Victor Yanev <[email protected]> * fix: cacheService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: utils.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: relay.spec.ts and sdkClient.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: eth_call.spec.ts, eth_estimateGas.spec.ts, ethGetBlockBy.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: use helper method to override envs in acceptance test specs Signed-off-by: Victor Yanev <[email protected]> * fix: hapiService.spec.ts and precheck.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: sdkClient.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: cacheService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: metricService.spec.ts and subscriptionController.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: rateLimiter.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: failing tests Signed-off-by: Victor Yanev <[email protected]> * chore: revert some changes in rpc_batch3.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: subscribe.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: add docs with examples to `overrideEnvs` and `withOverriddenEnvs` Signed-off-by: Victor Yanev <[email protected]> * chore: address comments Signed-off-by: Victor Yanev <[email protected]> * fix: build image test Signed-off-by: Victor Yanev <[email protected]> * Merge branch 'main' into add-helper-method-for-overriding-env-variables Signed-off-by: Victor Yanev <[email protected]> # Conflicts: # packages/relay/tests/lib/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository.spec.ts # packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts * chore: address comments + fix conflicts after merge Signed-off-by: Victor Yanev <[email protected]> * chore: optimize TTL tests in localLRUCache.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: failing unit test after merge from main Signed-off-by: Victor Yanev <[email protected]> * chore: optimize imports after merge from main Signed-off-by: Victor Yanev <[email protected]> --------- Signed-off-by: Victor Yanev <[email protected]>
1 parent 6bc6010 commit 2e2fc07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1935
-1951
lines changed

packages/relay/src/lib/services/ethService/ethFilterService/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class FilterService implements IFilterService {
187187
return predefined.UNSUPPORTED_METHOD;
188188
}
189189

190-
public async getFilterLogs(filterId: string, requestDetails: RequestDetails): Promise<any> {
190+
public async getFilterLogs(filterId: string, requestDetails: RequestDetails): Promise<Log[]> {
191191
this.logger.trace(`${requestDetails.formattedRequestId} getFilterLogs(${filterId})`);
192192
FilterService.requireFiltersEnabled();
193193

packages/relay/tests/helpers.ts

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -885,40 +885,98 @@ export const calculateTxRecordChargeAmount = (exchangeRateIncents: number) => {
885885
};
886886

887887
export const useInMemoryRedisServer = (logger: Logger, port: number) => {
888-
let envsToReset: { TEST?: string; REDIS_ENABLED?: string; REDIS_URL?: string };
888+
overrideEnvsInMochaDescribe({ TEST: 'false', REDIS_ENABLED: 'true', REDIS_URL: `redis://127.0.0.1:${port}` });
889+
889890
let redisInMemoryServer: RedisInMemoryServer;
890891

891892
before(async () => {
892-
({ envsToReset, redisInMemoryServer } = await startRedisInMemoryServer(logger, port));
893+
redisInMemoryServer = await startRedisInMemoryServer(logger, port);
893894
});
894895

895896
after(async () => {
896-
await stopRedisInMemoryServer(redisInMemoryServer, envsToReset);
897+
await stopRedisInMemoryServer(redisInMemoryServer);
897898
});
898899
};
899900

900901
export const startRedisInMemoryServer = async (logger: Logger, port: number) => {
901902
const redisInMemoryServer = new RedisInMemoryServer(logger.child({ name: 'RedisInMemoryServer' }), port);
902903
await redisInMemoryServer.start();
903-
const envsToReset = {
904-
TEST: process.env.TEST,
905-
REDIS_ENABLED: process.env.REDIS_ENABLED,
906-
REDIS_URL: process.env.REDIS_URL,
907-
};
908-
process.env.TEST = 'false';
909-
process.env.REDIS_ENABLED = 'true';
910-
process.env.REDIS_URL = `redis://127.0.0.1:${port}`;
911-
return { redisInMemoryServer, envsToReset };
904+
return redisInMemoryServer;
912905
};
913906

914-
export const stopRedisInMemoryServer = async (
915-
redisInMemoryServer: RedisInMemoryServer,
916-
envsToReset: { TEST?: string; REDIS_ENABLED?: string; REDIS_URL?: string },
917-
): Promise<void> => {
907+
export const stopRedisInMemoryServer = async (redisInMemoryServer: RedisInMemoryServer): Promise<void> => {
918908
await redisInMemoryServer.stop();
919-
process.env.TEST = envsToReset.TEST;
920-
process.env.REDIS_ENABLED = envsToReset.REDIS_ENABLED;
921-
process.env.REDIS_URL = envsToReset.REDIS_URL;
909+
};
910+
911+
/**
912+
* Temporarily overrides environment variables for the duration of the encapsulating describe block.
913+
* @param envs - An object containing key-value pairs of environment variables to set.
914+
*
915+
* @example
916+
* describe('given TEST is set to false', () => {
917+
* overrideEnvsInMochaDescribe({ TEST: 'false' });
918+
*
919+
* it('should return false', () => {
920+
* expect(process.env.TEST).to.equal('false');
921+
* });
922+
* });
923+
*
924+
* it('should return true', () => {
925+
* expect(process.env.TEST).to.equal('true');
926+
* });
927+
*/
928+
export const overrideEnvsInMochaDescribe = (envs: NodeJS.Dict<string>) => {
929+
let envsToReset: NodeJS.Dict<string> = {};
930+
931+
const overrideEnv = (object: NodeJS.Dict<string>, key: string, value: string | undefined) => {
932+
if (value === undefined) {
933+
delete object[key];
934+
} else {
935+
object[key] = value;
936+
}
937+
};
938+
939+
before(() => {
940+
for (const key in envs) {
941+
envsToReset[key] = process.env[key];
942+
overrideEnv(process.env, key, envs[key]);
943+
}
944+
});
945+
946+
after(() => {
947+
for (const key in envs) {
948+
overrideEnv(process.env, key, envsToReset[key]);
949+
}
950+
});
951+
};
952+
953+
/**
954+
* Overrides environment variables for the duration of the provided tests.
955+
*
956+
* @param {NodeJS.Dict<string>} envs - An object containing key-value pairs of environment variables to set.
957+
* @param {Function} tests - A function containing the tests to run with the overridden environment variables.
958+
*
959+
* @example
960+
* withOverriddenEnvsInMochaTest({ TEST: 'false' }, () => {
961+
* it('should return false', () => {
962+
* expect(process.env.TEST).to.equal('false');
963+
* });
964+
* });
965+
*
966+
* it('should return true', () => {
967+
* expect(process.env.TEST).to.equal('true');
968+
* });
969+
*/
970+
export const withOverriddenEnvsInMochaTest = (envs: NodeJS.Dict<string>, tests: () => void) => {
971+
const overriddenEnvs = Object.entries(envs)
972+
.map(([key, value]) => `${key}=${value}`)
973+
.join(', ');
974+
975+
describe(`given ${overriddenEnvs} are set`, () => {
976+
overrideEnvsInMochaDescribe(envs);
977+
978+
tests();
979+
});
922980
};
923981

924982
export const estimateFileTransactionsFee = (

packages/relay/tests/lib/clients/localLRUCache.spec.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import chaiAsPromised from 'chai-as-promised';
2323
import { Registry } from 'prom-client';
2424
import pino from 'pino';
2525
import { LocalLRUCache } from '../../../src/lib/clients';
26-
import constants from '../../../src/lib/constants';
26+
import { overrideEnvsInMochaDescribe } from '../../helpers';
2727
import { RequestDetails } from '../../../src/lib/types';
2828

2929
const logger = pino();
@@ -107,13 +107,9 @@ describe('LocalLRUCache Test Suite', async function () {
107107
});
108108

109109
describe('verify cache management', async function () {
110-
beforeEach(() => {
111-
process.env.CACHE_MAX = constants.CACHE_MAX.toString();
112-
});
110+
overrideEnvsInMochaDescribe({ CACHE_MAX: '2' });
113111

114112
it('verify cache size', async function () {
115-
const cacheMaxSize = 2;
116-
process.env.CACHE_MAX = `${cacheMaxSize}`;
117113
const customLocalLRUCache = new LocalLRUCache(logger.child({ name: `cache` }), registry);
118114
const keyValuePairs = {
119115
key1: 'value1',
@@ -149,8 +145,9 @@ describe('LocalLRUCache Test Suite', async function () {
149145
it('verify cache ttl nature', async function () {
150146
const customLocalLRUCache = new LocalLRUCache(logger.child({ name: `cache` }), registry);
151147
const key = 'key';
152-
await customLocalLRUCache.set(key, 'value', callingMethod, requestDetails, 100); // set ttl to 1 ms
153-
await new Promise((r) => setTimeout(r, 500)); // wait for ttl to expire
148+
const ttl = 100; // set ttl to 100ms
149+
await customLocalLRUCache.set(key, 'value', callingMethod, requestDetails, ttl);
150+
await new Promise((r) => setTimeout(r, ttl + 100)); // wait for ttl to expire
154151
const cacheValue = await customLocalLRUCache.get(key, callingMethod, requestDetails);
155152
expect(cacheValue).to.be.null;
156153
});

packages/relay/tests/lib/clients/redisCache.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('RedisCache Test Suite', async function () {
106106
it('should be able to set cache with TTL less than 1000 milliseconds', async () => {
107107
const key = 'int';
108108
const value = 1;
109-
const ttl = 500;
109+
const ttl = 100;
110110

111111
await redisCache.set(key, value, callingMethod, requestDetails, ttl);
112112

@@ -122,7 +122,7 @@ describe('RedisCache Test Suite', async function () {
122122
it('should be able to set cache with TTL greater than 1000 milliseconds', async () => {
123123
const key = 'int';
124124
const value = 1;
125-
const ttl = 1500;
125+
const ttl = 1100;
126126

127127
await redisCache.set(key, value, callingMethod, requestDetails, ttl);
128128

0 commit comments

Comments
 (0)