Skip to content

Commit 6de4d6d

Browse files
committed
move isTestEnvironemnt to its own module for better testability
1 parent 8b3fee5 commit 6de4d6d

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

client/modules/IDE/actions/uploader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TEXT_FILE_REGEX } from '../../../../server/utils/fileUtils';
22
import { apiClient } from '../../../utils/apiClient';
3-
import { getConfig, isTestEnvironment } from '../../../utils/getConfig';
3+
import { getConfig } from '../../../utils/getConfig';
4+
import { isTestEnvironment } from '../../../utils/checkTestEnv';
45
import { handleCreateFile } from './files';
56

67
const s3BucketUrlBase = getConfig('S3_BUCKET_URL_BASE');

client/utils/checkTestEnv.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { getEnvVar } from './getConfig';
2+
3+
export const isTestEnvironment = getEnvVar('NODE_ENV') === 'test';

client/utils/getConfig.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { isTestEnvironment } from './checkTestEnv';
2+
13
/**
2-
* Internal function to retrieve env vars, with no error handling.
4+
* Function to retrieve env vars, with no error handling.
35
* @returns String value of env variable or undefined if not found.
46
*/
5-
function getEnvVar(key: string): string | undefined {
7+
export function getEnvVar(key: string): string | undefined {
68
const configSource = global ?? window;
79
const env = configSource?.process?.env ?? {};
810

@@ -16,8 +18,6 @@ interface GetConfigOptions {
1618
throwErrorInTestEnv?: boolean; // this is only to test getConfig and should never be set to override defaults
1719
}
1820

19-
export const isTestEnvironment = getEnvVar('NODE_ENV') === 'test';
20-
2121
const defaultGetConfigOptions: GetConfigOptions = {
2222
warn: !isTestEnvironment,
2323
nullishString: false,

client/utils/parseStringToType.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { parseNumber, parseBoolean } from './parseStringToType';
22

3+
jest.mock('./checkTestEnv', () => ({
4+
isTestEnvironment: false
5+
}));
6+
37
describe('parseNumber', () => {
48
beforeEach(() => {
59
jest.spyOn(console, 'warn').mockImplementation(() => {});

client/utils/parseStringToType.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isTestEnvironment } from './checkTestEnv';
12
/* eslint-disable consistent-return */
23
/**
34
* Parses a string into a number.
@@ -9,7 +10,9 @@ export function parseNumber(
910
nullishNumber = false
1011
): number | undefined {
1112
if (!str) {
12-
console.warn(`parseNumber: got nullish input`);
13+
if (!isTestEnvironment) {
14+
console.warn(`parseNumber: got nullish input`);
15+
}
1316
return nullishNumber ? 0 : undefined;
1417
}
1518

@@ -32,7 +35,9 @@ export function parseBoolean(
3235
nullishBool = false
3336
): boolean | undefined {
3437
if (!str) {
35-
console.warn('parseBoolean: got nullish input');
38+
if (!isTestEnvironment) {
39+
console.warn('parseBoolean: got nullish input');
40+
}
3641
return nullishBool ? false : undefined;
3742
}
3843

0 commit comments

Comments
 (0)