Skip to content

Commit

Permalink
better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saeta-eth committed Sep 10, 2024
1 parent 8c4a5b1 commit 69123a5
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions packages/cli/src/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,37 +157,78 @@ describe('settings.ts', () => {
});

describe('resolveCliSettings()', () => {
// eslint-disable-next-line no-undef
let originalEnv: NodeJS.ProcessEnv;

beforeEach(() => {
originalEnv = { ...process.env };
jest.resetModules();
});

afterEach(() => {
process.env = originalEnv;
});

it('should cache the result after the first call', () => {
const settings1 = resolveCliSettings();
const settings2 = resolveCliSettings();

expect(settings1).toBe(settings2);
});

it('should not cache the result when resolveCliSettingsNoCache() is called', () => {
it('should return different results when overrides are provided', () => {
const settings1 = resolveCliSettings();
const uncachedSettings = resolveCliSettingsNoCache();
const settings2 = resolveCliSettings();

expect(settings1).toBe(settings2);
const overrides = {
rpcUrl: 'https://override.rpc.url',
ipfsRetries: 10,
};
const settings2 = resolveCliSettings(overrides);

expect(uncachedSettings).not.toBe(settings1);
expect(uncachedSettings).not.toBe(settings2);
expect(settings1).not.toBe(settings2);
expect(settings1.rpcUrl).not.toBe(settings2.rpcUrl);
expect(settings1.ipfsRetries).not.toBe(settings2.ipfsRetries);
expect(settings2.rpcUrl).toBe('https://override.rpc.url');
expect(settings2.ipfsRetries).toBe(10);
});

it('should reset the cache when new overrides are provided', () => {
const settings1 = resolveCliSettings();
it('should prioritize overrides over environment variables', () => {
process.env.CANNON_RPC_URL = 'https://env.rpc.url';

const overrides = {
rpcUrl: 'https://override.rpc.url',
};
const settings = resolveCliSettings(overrides);

const settingsWithOverrides = resolveCliSettings(overrides);
const settings2 = resolveCliSettings();
expect(settings.rpcUrl).toBe('https://override.rpc.url');
});

it('should use the same cached result when called with the same overrides', () => {
const overrides = {
rpcUrl: 'https://override.rpc.url',
};

const settings1 = resolveCliSettings(overrides);
const settings2 = resolveCliSettings(overrides);

expect(settings1).toBe(settings2);
});

it('should use different cached results for different overrides', () => {
const overrides1 = {
rpcUrl: 'https://override1.rpc.url',
};

const overrides2 = {
rpcUrl: 'https://override2.rpc.url',
};

expect(settings1).not.toBe(settingsWithOverrides);
const settings1 = resolveCliSettings(overrides1);
const settings2 = resolveCliSettings(overrides2);

expect(settings1).not.toBe(settings2);
expect(settings1.rpcUrl).toBe('https://override1.rpc.url');
expect(settings2.rpcUrl).toBe('https://override2.rpc.url');
});
});
});

0 comments on commit 69123a5

Please sign in to comment.