Skip to content

Commit 0ad638f

Browse files
committed
getConfig: wip update to remove superfluous fail in test env option
1 parent 6de4d6d commit 0ad638f

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

client/utils/getConfig.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import { getConfig } from './getConfig';
22

3+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
4+
35
describe('utils/getConfig()', () => {
46
beforeEach(() => {
57
delete global.process.env.CONFIG_TEST_KEY_NAME;
68
delete window.process.env.CONFIG_TEST_KEY_NAME;
9+
10+
consoleWarnSpy.mockClear();
11+
});
12+
13+
afterAll(() => {
14+
consoleWarnSpy.mockRestore();
715
});
816

917
// check for key
@@ -33,13 +41,14 @@ describe('utils/getConfig()', () => {
3341
});
3442

3543
it('throws an error if throwErrorIfNotFound is true', () => {
36-
process.env.NODE_ENV = 'not_test';
3744
expect(() =>
3845
getConfig('CONFIG_TEST_KEY_NAME', {
39-
throwErrorIfNotFound: true,
40-
throwErrorInTestEnv: true
46+
throwErrorIfNotFound: true
4147
})
4248
).toThrow();
49+
expect(consoleWarnSpy).toHaveBeenCalledWith(
50+
'getConfig("CONFIG_TEST_KEY_NAME") returned null or undefined'
51+
);
4352
});
4453

4554
it('returns undefined by default', () => {
@@ -67,8 +76,7 @@ describe('utils/getConfig()', () => {
6776
it('throws an error if throwErrorIfNotFound is true', () => {
6877
expect(() =>
6978
getConfig('CONFIG_TEST_KEY_NAME', {
70-
throwErrorIfNotFound: true,
71-
throwErrorInTestEnv: true
79+
throwErrorIfNotFound: true
7280
})
7381
).toThrow();
7482
});

client/utils/getConfig.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ interface GetConfigOptions {
1515
warn?: boolean;
1616
nullishString?: boolean;
1717
throwErrorIfNotFound?: boolean;
18-
throwErrorInTestEnv?: boolean; // this is only to test getConfig and should never be set to override defaults
1918
}
2019

2120
const defaultGetConfigOptions: GetConfigOptions = {
2221
warn: !isTestEnvironment,
2322
nullishString: false,
24-
throwErrorIfNotFound: false,
25-
throwErrorInTestEnv: false
23+
throwErrorIfNotFound: false
2624
};
2725

2826
/**
@@ -34,7 +32,6 @@ const defaultGetConfigOptions: GetConfigOptions = {
3432
* - `throwErrorIfNotFound`: whether to throw an error if the value is missing (default to `false`).
3533
* - `warn`: whether to warn if the value is missing (default `true` unless in test env).
3634
* - `nullishString`: if true, returns `''` instead of `undefined` when missing.
37-
* - `throwErrorInTestEnv`: this is to test getConfig and should not be overridden (default to `false`).
3835
* @returns String value of the env var, or `''` or `undefined` if missing.
3936
*/
4037
export function getConfig(
@@ -46,7 +43,7 @@ export function getConfig(
4643
}
4744

4845
// override default options with param options
49-
const { warn, nullishString, throwErrorIfNotFound, throwErrorInTestEnv } = {
46+
const { warn, nullishString, throwErrorIfNotFound } = {
5047
...defaultGetConfigOptions,
5148
...options
5249
};
@@ -59,10 +56,7 @@ export function getConfig(
5956
const notFoundMessage = `getConfig("${key}") returned null or undefined`;
6057

6158
// error, warn or continue if no value found:
62-
if (
63-
(throwErrorIfNotFound && !isTestEnvironment) ||
64-
(throwErrorIfNotFound && isTestEnvironment && throwErrorInTestEnv) // this is just to enable us to test getConfig's error throwing
65-
) {
59+
if (throwErrorIfNotFound && !isTestEnvironment) {
6660
throw new Error(notFoundMessage);
6761
}
6862
if (warn) {

0 commit comments

Comments
 (0)