@@ -131,10 +131,7 @@ describe('twakeConfig - Basic Cases', () => {
131131 } )
132132 } )
133133
134- const res : Configuration = twakeConfig (
135- createTestConfigDesc ( ) ,
136- mockFilePath
137- )
134+ const res : Configuration = twakeConfig ( createTestConfigDesc ( ) , mockFilePath )
138135 expect ( res ) . toEqual ( {
139136 STRING_KEY : 'fileString' ,
140137 NUMBER_KEY : 200 ,
@@ -392,16 +389,16 @@ describe('twakeConfig - Hard Cases (Complex Coercion & Defaults)', () => {
392389 test ( 'Array coercion with empty string items' , ( ) => {
393390 process . env . ARRAY_KEY = 'item1,,item2'
394391
395-
396392 const res = twakeConfig ( createTestConfigDesc ( ) , undefined , true )
397393 expect ( res . ARRAY_KEY ) . toEqual ( [ 'item1' , 'item2' ] )
398394
399-
400395 process . env . ARRAY_KEY = ''
401- expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) )
402- . toThrow ( ConfigCoercionError )
403- expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) )
404- . toThrow ( / E m p t y s t r i n g v a l u e s a r e n o t a l l o w e d / )
396+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
397+ ConfigCoercionError
398+ )
399+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
400+ / E m p t y s t r i n g v a l u e s a r e n o t a l l o w e d /
401+ )
405402 } )
406403
407404 test ( 'Default value is a string but needs coercion to target type' , async ( ) => {
@@ -475,11 +472,13 @@ describe('twakeConfig - Edge Cases and Error Handling', () => {
475472 test ( 'Should throw ConfigCoercionError if environment variable is empty string' , ( ) => {
476473 process . env . STRING_KEY = ''
477474
478- expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) )
479- . toThrow ( ConfigCoercionError )
475+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
476+ ConfigCoercionError
477+ )
480478
481- expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) )
482- . toThrow ( / E m p t y s t r i n g v a l u e s a r e n o t a l l o w e d / )
479+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
480+ / E m p t y s t r i n g v a l u e s a r e n o t a l l o w e d /
481+ )
483482 } )
484483
485484 test ( 'Should fall back to default if environment variable is null/undefined (not set)' , ( ) => {
@@ -497,13 +496,13 @@ describe('twakeConfig - Edge Cases and Error Handling', () => {
497496 UNWANTED_KEY_FROM_OBJECT : 'bad'
498497 }
499498
500- expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , initialConfig ) )
501- . toThrow ( UnacceptedKeyError )
499+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , initialConfig ) ) . toThrow (
500+ UnacceptedKeyError
501+ )
502502
503- expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , initialConfig ) )
504- . toThrow (
505- "Configuration key 'UNWANTED_KEY_FROM_OBJECT' isn't accepted as it's not defined in the ConfigDescription."
506- )
503+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , initialConfig ) ) . toThrow (
504+ "Configuration key 'UNWANTED_KEY_FROM_OBJECT' isn't accepted as it's not defined in the ConfigDescription."
505+ )
507506 } )
508507
509508 test ( 'Should throw UnacceptedKeyError for unwanted key in defaultConfigurationFile (JSON file)' , ( ) => {
@@ -514,96 +513,86 @@ describe('twakeConfig - Edge Cases and Error Handling', () => {
514513 unwanted_value : 'bad'
515514 } )
516515 } )
517- expect (
518- ( ) => twakeConfig ( createTestConfigDesc ( ) , mockFilePath )
519- ) . toThrow ( UnacceptedKeyError )
520- expect (
521- ( ) => twakeConfig ( createTestConfigDesc ( ) , mockFilePath )
522- ) . toThrow (
516+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , mockFilePath ) ) . toThrow (
517+ UnacceptedKeyError
518+ )
519+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , mockFilePath ) ) . toThrow (
523520 "Configuration key 'unwanted_value' isn't accepted as it's not defined in the ConfigDescription."
524521 )
525522 } )
526523
527524 test ( 'Should throw ConfigCoercionError for invalid boolean format from environment variable' , ( ) => {
528525 process . env . BOOLEAN_KEY = 'not_a_boolean'
529- expect (
530- ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true )
531- ) . toThrow ( ConfigCoercionError )
532- expect (
533- ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true )
534- ) . toThrow (
526+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
527+ ConfigCoercionError
528+ )
529+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
535530 / C o n f i g u r a t i o n e r r o r f o r ' B O O L E A N _ K E Y ' f r o m e n v i r o n m e n t v a r i a b l e ' B O O L E A N _ K E Y ' : I n v a l i d b o o l e a n f o r m a t f o r v a l u e : ' n o t _ a _ b o o l e a n ' /
536531 )
537532 } )
538533
539534 test ( 'Should throw ConfigCoercionError for invalid number format from environment variable' , ( ) => {
540535 process . env . NUMBER_KEY = 'not_a_number'
541- expect (
542- ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true )
543- ) . toThrow ( ConfigCoercionError )
544- expect (
545- ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true )
546- ) . toThrow (
536+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
537+ ConfigCoercionError
538+ )
539+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
547540 / C o n f i g u r a t i o n e r r o r f o r ' N U M B E R _ K E Y ' f r o m e n v i r o n m e n t v a r i a b l e ' N U M B E R _ K E Y ' : I n v a l i d n u m b e r f o r m a t f o r v a l u e : ' n o t _ a _ n u m b e r ' /
548541 )
549542 } )
550543
551544 test ( 'Should throw ConfigCoercionError for invalid JSON format from environment variable (JSON type)' , ( ) => {
552545 process . env . JSON_KEY = '{invalid json'
553- expect (
554- ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true )
555- ) . toThrow ( ConfigCoercionError )
556- expect (
557- ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true )
558- ) . toThrow (
546+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
547+ ConfigCoercionError
548+ )
549+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
559550 / C o n f i g u r a t i o n e r r o r f o r ' J S O N _ K E Y ' f r o m e n v i r o n m e n t v a r i a b l e ' J S O N _ K E Y ' : I n v a l i d J S O N f o r m a t f o r v a l u e : ' \{ i n v a l i d j s o n ' /
560551 )
561552 } )
562553
563554 test ( 'Should throw ConfigCoercionError for invalid JSON format from environment variable (OBJECT type)' , ( ) => {
564555 process . env . OBJECT_KEY = 'invalid object json'
565- expect (
566- ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true )
567- ) . toThrow ( ConfigCoercionError )
568- expect (
569- ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true )
570- ) . toThrow (
556+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
557+ ConfigCoercionError
558+ )
559+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , undefined , true ) ) . toThrow (
571560 / C o n f i g u r a t i o n e r r o r f o r ' O B J E C T _ K E Y ' f r o m e n v i r o n m e n t v a r i a b l e ' O B J E C T _ K E Y ' : I n v a l i d J S O N f o r m a t f o r v a l u e : ' i n v a l i d o b j e c t j s o n ' /
572561 )
573562 } )
574563
575564 test ( 'Should throw FileReadParseError if config file is not found' , ( ) => {
576565 const nonExistentPath = 'non_existent_config.json'
577566
578- ; ( fs . existsSync as jest . Mock ) . mockReturnValue ( false )
579- ; ( fs . promises . readFile as jest . Mock ) . mockImplementation ( ( ) => {
580- const error = new Error (
581- `ENOENT: no such file or directory, open '${ nonExistentPath } '`
582- )
583- ; ( error as any ) . code = 'ENOENT'
584- throw error
585- } )
567+ ; ( fs . existsSync as jest . Mock ) . mockReturnValue ( false )
568+ ; ( fs . promises . readFile as jest . Mock ) . mockImplementation ( ( ) => {
569+ const error = new Error (
570+ `ENOENT: no such file or directory, open '${ nonExistentPath } '`
571+ )
572+ ; ( error as any ) . code = 'ENOENT'
573+ throw error
574+ } )
586575
587- expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , nonExistentPath ) )
588- . toThrow ( FileReadParseError )
576+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , nonExistentPath ) ) . toThrow (
577+ FileReadParseError
578+ )
589579
590580 expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
591581 } )
592582
593-
594583 test ( 'Should throw FileReadParseError if config file contains invalid JSON' , ( ) => {
595584 const invalidJsonPath = 'invalid.json'
596585 setupMockFs ( {
597586 [ invalidJsonPath ] : '{ "key": "value", "another": }'
598587 } )
599588
600- expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , invalidJsonPath ) )
601- . toThrow ( FileReadParseError )
589+ expect ( ( ) => twakeConfig ( createTestConfigDesc ( ) , invalidJsonPath ) ) . toThrow (
590+ FileReadParseError
591+ )
602592
603593 expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
604594 } )
605595
606-
607596 test ( 'Should handle null default values correctly' , async ( ) => {
608597 const res : Configuration = twakeConfig ( createTestConfigDesc ( ) )
609598 expect ( res . NULL_KEY ) . toBeNull ( )
@@ -632,11 +621,7 @@ describe('twakeConfig - Edge Cases and Error Handling', () => {
632621 MY_REQUIRED_KEY : { type : 'string' , required : true }
633622 }
634623 process . env . MY_REQUIRED_KEY = 'present'
635- const res : Configuration = twakeConfig (
636- descWithRequired ,
637- undefined ,
638- true
639- )
624+ const res : Configuration = twakeConfig ( descWithRequired , undefined , true )
640625 expect ( res . MY_REQUIRED_KEY ) . toBe ( 'present' )
641626 } )
642627
@@ -651,4 +636,4 @@ describe('twakeConfig - Edge Cases and Error Handling', () => {
651636 const res : Configuration = twakeConfig ( descWithRequired )
652637 expect ( res . MY_REQUIRED_KEY ) . toBe ( 'default_value' )
653638 } )
654- } )
639+ } )
0 commit comments