@@ -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} ) ;
0 commit comments