From 0d12dc0878fc4f64e1017ad20a7418e83db561f8 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 26 Oct 2023 18:22:13 +0100 Subject: [PATCH] feat: Increase robustness of `FUEL_NETWORK_URL` config (#1379) * test: add testcases for wallet config network url export * chore: changeset * feat: increase robustness of fuel network url config --- .changeset/strange-lions-warn.md | 2 ++ packages/wallet/src/configs.test.ts | 52 +++++++++++++++++++++++++++++ packages/wallet/src/configs.ts | 5 ++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 .changeset/strange-lions-warn.md create mode 100644 packages/wallet/src/configs.test.ts diff --git a/.changeset/strange-lions-warn.md b/.changeset/strange-lions-warn.md new file mode 100644 index 00000000000..a49ba48448f --- /dev/null +++ b/.changeset/strange-lions-warn.md @@ -0,0 +1,2 @@ +--- +--- \ No newline at end of file diff --git a/packages/wallet/src/configs.test.ts b/packages/wallet/src/configs.test.ts new file mode 100644 index 00000000000..1e71827fa93 --- /dev/null +++ b/packages/wallet/src/configs.test.ts @@ -0,0 +1,52 @@ +describe('Configs', () => { + it('exports FUEL_NETWORK_URL', async () => { + const configs = await import('./configs'); + expect(configs.FUEL_NETWORK_URL).toBe('http://127.0.0.1:4000/graphql'); + }); +}); + +describe('Configs - undefined process', () => { + const originalProcess = process; + + beforeEach(() => { + jest.resetModules(); + + // @ts-expect-error - test to assert undefined process + // eslint-disable-next-line no-global-assign + process = undefined; + }); + + afterEach(() => { + // eslint-disable-next-line no-global-assign + process = originalProcess; + }); + + it('exports FUEL_NETWORK_URL with undefined process', async () => { + expect(typeof process).toBe('undefined'); + expect(process).toBeUndefined(); + + const configs = await import('./configs'); + + expect(configs.FUEL_NETWORK_URL).toBe('http://127.0.0.1:4000/graphql'); + }); +}); + +describe('Configs - overridden env', () => { + const originalEnv = process.env; + + beforeEach(() => { + jest.resetModules(); + + process.env = { ...originalEnv, FUEL_NETWORK_URL: 'some-other-network-url' }; + }); + + afterEach(() => { + process.env = originalEnv; + }); + + it('exports FUEL_NETWORK_URL with overridden env', async () => { + const configs = await import('./configs'); + + expect(configs.FUEL_NETWORK_URL).toBe('some-other-network-url'); + }); +}); diff --git a/packages/wallet/src/configs.ts b/packages/wallet/src/configs.ts index b613aad39b8..182406d1cde 100644 --- a/packages/wallet/src/configs.ts +++ b/packages/wallet/src/configs.ts @@ -1 +1,4 @@ -export const FUEL_NETWORK_URL = process?.env?.FUEL_NETWORK_URL || 'http://127.0.0.1:4000/graphql'; +export const FUEL_NETWORK_URL = + typeof process !== 'undefined' + ? process?.env?.FUEL_NETWORK_URL || 'http://127.0.0.1:4000/graphql' + : 'http://127.0.0.1:4000/graphql';