1+ import { InvalidConfigException } from '@ukef/config/invalid-config.exception' ;
12import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator' ;
23
34interface Options < ConfigUnderTest > {
4- configDirectlyFromEnvironmentVariables : {
5+ configDirectlyFromEnvironmentVariables ? : {
56 configPropertyName : keyof ConfigUnderTest ;
67 environmentVariableName : string ;
78 } [ ] ;
8- configParsedAsIntFromEnvironmentVariablesWithDefault : {
9+ configParsedBooleanFromEnvironmentVariablesWithDefault ?: {
10+ configPropertyName : keyof ConfigUnderTest ;
11+ environmentVariableName : string ;
12+ defaultConfigValue : boolean ;
13+ } [ ] ;
14+ configParsedAsIntFromEnvironmentVariablesWithDefault ?: {
915 configPropertyName : keyof ConfigUnderTest ;
1016 environmentVariableName : string ;
1117 defaultConfigValue : number ;
@@ -15,6 +21,7 @@ interface Options<ConfigUnderTest> {
1521
1622export const withEnvironmentVariableParsingUnitTests = < ConfigUnderTest > ( {
1723 configDirectlyFromEnvironmentVariables,
24+ configParsedBooleanFromEnvironmentVariablesWithDefault,
1825 configParsedAsIntFromEnvironmentVariablesWithDefault,
1926 getConfig,
2027} : Options < ConfigUnderTest > ) : void => {
@@ -30,18 +37,79 @@ export const withEnvironmentVariableParsingUnitTests = <ConfigUnderTest>({
3037 process . env = originalProcessEnv ;
3138 } ) ;
3239
33- describe . each ( configDirectlyFromEnvironmentVariables ) ( '$configPropertyName' , ( { configPropertyName, environmentVariableName } ) => {
34- it ( `is the env variable ${ environmentVariableName } ` , ( ) => {
35- const environmentVariableValue = valueGenerator . string ( ) ;
36- process . env = {
37- [ environmentVariableName ] : environmentVariableValue ,
38- } ;
40+ if ( configDirectlyFromEnvironmentVariables ) {
41+ describe . each ( configDirectlyFromEnvironmentVariables ) ( '$configPropertyName' , ( { configPropertyName, environmentVariableName } ) => {
42+ it ( `is the env variable ${ environmentVariableName } ` , ( ) => {
43+ const environmentVariableValue = valueGenerator . string ( ) ;
44+ process . env = {
45+ [ environmentVariableName ] : environmentVariableValue ,
46+ } ;
3947
40- const { [ configPropertyName ] : configPropertyValue } = getConfig ( ) ;
48+ const { [ configPropertyName ] : configPropertyValue } = getConfig ( ) ;
4149
42- expect ( configPropertyValue ) . toBe ( environmentVariableValue ) ;
50+ expect ( configPropertyValue ) . toBe ( environmentVariableValue ) ;
51+ } ) ;
4352 } ) ;
44- } ) ;
53+ }
54+
55+ if ( configParsedBooleanFromEnvironmentVariablesWithDefault ) {
56+ describe . each ( configParsedBooleanFromEnvironmentVariablesWithDefault ) (
57+ '$configPropertyName' ,
58+ ( { configPropertyName, environmentVariableName, defaultConfigValue } ) => {
59+ it ( `is true if environment variable ${ environmentVariableName } is true` , ( ) => {
60+ const expectedConfigValue = true ;
61+ const environmentVariableValue = expectedConfigValue . toString ( ) ;
62+ process . env = {
63+ [ environmentVariableName ] : environmentVariableValue ,
64+ } ;
65+
66+ const { [ configPropertyName ] : configPropertyValue } = getConfig ( ) ;
67+
68+ expect ( configPropertyValue ) . toBe ( expectedConfigValue ) ;
69+ } ) ;
70+
71+ it ( `is false if environment variable ${ environmentVariableName } is false` , ( ) => {
72+ const expectedConfigValue = false ;
73+ const environmentVariableValue = expectedConfigValue . toString ( ) ;
74+ process . env = {
75+ [ environmentVariableName ] : environmentVariableValue ,
76+ } ;
77+
78+ const { [ configPropertyName ] : configPropertyValue } = getConfig ( ) ;
79+
80+ expect ( configPropertyValue ) . toBe ( expectedConfigValue ) ;
81+ } ) ;
82+
83+ it ( `is the default value if ${ environmentVariableName } is any string other than true or false` , ( ) => {
84+ process . env = {
85+ [ environmentVariableName ] : valueGenerator . string ( ) ,
86+ } ;
87+
88+ const { [ configPropertyName ] : configPropertyValue } = getConfig ( ) ;
89+
90+ expect ( configPropertyValue ) . toBe ( defaultConfigValue ) ;
91+ } ) ;
92+
93+ it ( `is the default value if ${ environmentVariableName } is not specified` , ( ) => {
94+ process . env = { } ;
95+
96+ const { [ configPropertyName ] : configPropertyValue } = getConfig ( ) ;
97+
98+ expect ( configPropertyValue ) . toBe ( defaultConfigValue ) ;
99+ } ) ;
100+
101+ it ( `is the default value if ${ environmentVariableName } is empty string` , ( ) => {
102+ process . env = {
103+ [ environmentVariableName ] : '' ,
104+ } ;
105+
106+ const { [ configPropertyName ] : configPropertyValue } = getConfig ( ) ;
107+
108+ expect ( configPropertyValue ) . toBe ( defaultConfigValue ) ;
109+ } ) ;
110+ } ,
111+ ) ;
112+ }
45113
46114 describe . each ( configParsedAsIntFromEnvironmentVariablesWithDefault ) (
47115 '$configPropertyName' ,
@@ -66,15 +134,52 @@ export const withEnvironmentVariableParsingUnitTests = <ConfigUnderTest>({
66134 expect ( configPropertyValue ) . toBe ( defaultConfigValue ) ;
67135 } ) ;
68136
69- it ( `is the default value ${ defaultConfigValue } if ${ environmentVariableName } is not parseable as an integer` , ( ) => {
137+ it ( `throws InvalidConfigException if ${ environmentVariableName } is not parseable as an integer` , ( ) => {
70138 const environmentVariableValue = 'abc' ;
71139 process . env = {
72140 [ environmentVariableName ] : environmentVariableValue ,
73141 } ;
74142
75- const { [ configPropertyName ] : configPropertyValue } = getConfig ( ) ;
143+ const gettingTheConfig = ( ) => getConfig ( ) ;
76144
77- expect ( configPropertyValue ) . toBe ( defaultConfigValue ) ;
145+ expect ( gettingTheConfig ) . toThrow ( InvalidConfigException ) ;
146+ expect ( gettingTheConfig ) . toThrow ( `Invalid integer value "${ environmentVariableValue } " for configuration property.` ) ;
147+ } ) ;
148+
149+ it ( `throws InvalidConfigException if ${ environmentVariableName } is float number` , ( ) => {
150+ const environmentVariableValue = valueGenerator . nonnegativeFloat ( ) . toString ( ) ;
151+ process . env = {
152+ [ environmentVariableName ] : environmentVariableValue ,
153+ } ;
154+
155+ const gettingTheConfig = ( ) => getConfig ( ) ;
156+
157+ expect ( gettingTheConfig ) . toThrow ( InvalidConfigException ) ;
158+ expect ( gettingTheConfig ) . toThrow ( `Invalid integer value "${ environmentVariableValue } " for configuration property.` ) ;
159+ } ) ;
160+
161+ it ( `throws InvalidConfigException if ${ environmentVariableName } is hex number` , ( ) => {
162+ const environmentVariableValue = '0xFF' ;
163+ process . env = {
164+ [ environmentVariableName ] : environmentVariableValue ,
165+ } ;
166+
167+ const gettingTheConfig = ( ) => getConfig ( ) ;
168+
169+ expect ( gettingTheConfig ) . toThrow ( InvalidConfigException ) ;
170+ expect ( gettingTheConfig ) . toThrow ( `Invalid integer value "${ environmentVariableValue } " for configuration property.` ) ;
171+ } ) ;
172+
173+ it ( `throws InvalidConfigException if ${ environmentVariableName } is binary number` , ( ) => {
174+ const environmentVariableValue = '0b101' ;
175+ process . env = {
176+ [ environmentVariableName ] : environmentVariableValue ,
177+ } ;
178+
179+ const gettingTheConfig = ( ) => getConfig ( ) ;
180+
181+ expect ( gettingTheConfig ) . toThrow ( InvalidConfigException ) ;
182+ expect ( gettingTheConfig ) . toThrow ( `Invalid integer value "${ environmentVariableValue } " for configuration property.` ) ;
78183 } ) ;
79184 } ,
80185 ) ;
0 commit comments