Skip to content

Commit bbeac90

Browse files
committed
getConfig: update to have failOnNotFound option to fail on compiler
1 parent 15cabf8 commit bbeac90

File tree

2 files changed

+77
-19
lines changed

2 files changed

+77
-19
lines changed

client/utils/getConfig.test.ts

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,55 @@ describe('utils/getConfig()', () => {
2727
});
2828

2929
// check returns unhappy path
30-
it('warns but does not throw if no value found', () => {
31-
expect(() => getConfig('CONFIG_TEST_KEY_NAME')).not.toThrow();
32-
});
30+
describe('and when the key does not exist in the env file', () => {
31+
it('warns but does not throw if failOnNotFound is false (default)', () => {
32+
expect(() => getConfig('CONFIG_TEST_KEY_NAME')).not.toThrow();
33+
});
34+
35+
it('throws an error if failOnNotFound is true', () => {
36+
expect(() =>
37+
getConfig('CONFIG_TEST_KEY_NAME', { failOnNotFound: true })
38+
).toThrow();
39+
});
3340

34-
it('returns the expected nullish value when no value is found', () => {
35-
const result = getConfig('CONFIG_TEST_KEY_NAME');
36-
expect(result).toBe(undefined);
37-
expect(!result).toBe(true);
38-
expect(`${result}`).toBe('undefined');
41+
it('returns undefined by default', () => {
42+
const result = getConfig('CONFIG_TEST_KEY_NAME');
43+
expect(result).toBe(undefined);
44+
expect(!result).toBe(true);
45+
expect(`${result}`).toBe('undefined');
46+
});
47+
48+
it('can be set to return an empty string as the nullish value', () => {
49+
const result = getConfig('CONFIG_TEST_KEY_NAME', { nullishString: true });
50+
expect(`${result}`).toBe('');
51+
});
3952
});
40-
it('can be set to return an empty string as the nullish value', () => {
41-
const result = getConfig('CONFIG_TEST_KEY_NAME', { nullishString: true });
42-
expect(`${result}`).toBe('');
53+
54+
describe('and when the key exists in the env file but the value is empty', () => {
55+
beforeEach(() => {
56+
global.process.env.CONFIG_TEST_KEY_NAME = '';
57+
});
58+
59+
it('warns but does not throw if failOnNotFound is false (default)', () => {
60+
expect(() => getConfig('CONFIG_TEST_KEY_NAME')).not.toThrow();
61+
});
62+
63+
it('throws an error if failOnNotFound is true', () => {
64+
expect(() =>
65+
getConfig('CONFIG_TEST_KEY_NAME', { failOnNotFound: true })
66+
).toThrow();
67+
});
68+
69+
it('returns undefined by default', () => {
70+
const result = getConfig('CONFIG_TEST_KEY_NAME');
71+
expect(result).toBe(undefined);
72+
expect(!result).toBe(true);
73+
expect(`${result}`).toBe('undefined');
74+
});
75+
76+
it('can be set to return an empty string as the nullish value', () => {
77+
const result = getConfig('CONFIG_TEST_KEY_NAME', { nullishString: true });
78+
expect(`${result}`).toBe('');
79+
});
4380
});
4481
});

client/utils/getConfig.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@
33
* @returns String value of env variable or undefined if not found.
44
*/
55
function getEnvVar(key: string): string | undefined {
6-
const env: Record<string, string | undefined> =
7-
(typeof global !== 'undefined' ? global : window)?.process?.env || {};
6+
const configSource = global ?? window;
7+
const env = configSource?.process?.env ?? {};
88

99
return env[key];
1010
}
1111

12-
type GetConfigOptions = {
12+
interface GetConfigOptions {
1313
warn?: boolean;
1414
nullishString?: boolean;
15+
failOnNotFound?: boolean;
16+
}
17+
18+
const isTestEnvironment = getEnvVar('NODE_ENV') === 'test';
19+
20+
const defaultGetConfigOptions: GetConfigOptions = {
21+
warn: !isTestEnvironment,
22+
nullishString: false,
23+
failOnNotFound: false
1524
};
1625

1726
/**
1827
* Returns a string config value from environment variables.
19-
* Logs a warning if the value is missing and `warn` is not explicitly disabled.
28+
* Logs a warning or throws an error if the value is missing, if `warn` and `failOnNotFound` are true in options
2029
*
2130
* @param key - The environment variable key to fetch.
2231
* @param options - Optional settings:
32+
* - `failOnNotFound`: whether to throw an error if the value is missing (default to `false`).
2333
* - `warn`: whether to warn if the value is missing (default `true` unless in test env).
2434
* - `nullishString`: if true, returns `''` instead of `undefined` when missing.
2535
* @returns String value of the env var, or `''` or `undefined` if missing.
@@ -31,15 +41,26 @@ export function getConfig(
3141
if (!key) {
3242
throw new Error('"key" must be provided to getConfig()');
3343
}
34-
const isTestEnvironment = getEnvVar('NODE_ENV') === 'test';
3544

36-
const { warn = !isTestEnvironment, nullishString = false } = options;
45+
// override default options with param options
46+
const { warn, nullishString, failOnNotFound } = {
47+
...defaultGetConfigOptions,
48+
...options
49+
};
3750

3851
const value = getEnvVar(key);
3952

40-
if (value == null) {
53+
// value == null when the key is not present in the env file
54+
// value === '' when the key is present but is empty (eg. TEST_CONFIG_VALUE=)
55+
if (value == null || value === '') {
56+
const notFoundMessage = `getConfig("${key}") returned null or undefined`;
57+
58+
// error, warn or continue if no value found:
59+
if (failOnNotFound) {
60+
throw new Error(notFoundMessage);
61+
}
4162
if (warn) {
42-
console.warn(`getConfig("${key}") returned null or undefined`);
63+
console.warn(notFoundMessage);
4364
}
4465
return nullishString ? '' : undefined;
4566
}

0 commit comments

Comments
 (0)