1
- import { OpenFeature , Provider , ResolutionDetails , Client , FlagValueType , EvaluationContext , Hook } from '../src' ;
1
+ import {
2
+ OpenFeature ,
3
+ Provider ,
4
+ ResolutionDetails ,
5
+ Client ,
6
+ FlagValueType ,
7
+ EvaluationContext ,
8
+ Hook ,
9
+ StandardResolutionReasons ,
10
+ ErrorCode ,
11
+ } from '../src' ;
2
12
3
13
const BOOLEAN_VALUE = true ;
4
14
5
15
const BOOLEAN_VARIANT = `${ BOOLEAN_VALUE } ` ;
6
16
const REASON = 'mocked-value' ;
7
- const ERROR_REASON = 'error' ;
8
- const ERROR_CODE = 'MOCKED_ERROR' ;
9
17
10
18
// a mock provider with some jest spies
11
19
const MOCK_PROVIDER : Provider = {
@@ -28,8 +36,8 @@ const MOCK_ERROR_PROVIDER: Provider = {
28
36
} ,
29
37
resolveBooleanEvaluation : jest . fn ( ( ) : Promise < ResolutionDetails < boolean > > => {
30
38
return Promise . reject ( {
31
- reason : ERROR_REASON ,
32
- errorCode : ERROR_CODE ,
39
+ reason : StandardResolutionReasons . ERROR ,
40
+ errorCode : ErrorCode . GENERAL ,
33
41
} ) ;
34
42
} ) ,
35
43
} as unknown as Provider ;
@@ -357,6 +365,27 @@ describe('Hooks', () => {
357
365
] ,
358
366
} ) ;
359
367
} ) ;
368
+
369
+ it ( '"error" must run if resolution details contains an error code' , async ( ) => {
370
+ ( MOCK_ERROR_PROVIDER . resolveBooleanEvaluation as jest . Mock ) . mockResolvedValueOnce ( {
371
+ value : BOOLEAN_VALUE ,
372
+ errorCode : ErrorCode . FLAG_NOT_FOUND ,
373
+ } ) ;
374
+
375
+ const mockErrorHook = jest . fn ( ) ;
376
+
377
+ const details = await client . getBooleanDetails ( FLAG_KEY , false , undefined , {
378
+ hooks : [ { error : mockErrorHook } ] ,
379
+ } ) ;
380
+
381
+ expect ( mockErrorHook ) . toHaveBeenCalled ( ) ;
382
+ expect ( details ) . toEqual (
383
+ expect . objectContaining ( {
384
+ errorCode : ErrorCode . FLAG_NOT_FOUND ,
385
+ reason : StandardResolutionReasons . ERROR ,
386
+ } ) ,
387
+ ) ;
388
+ } ) ;
360
389
} ) ;
361
390
} ) ;
362
391
@@ -636,8 +665,8 @@ describe('Hooks', () => {
636
665
] ,
637
666
resolveBooleanEvaluation : jest . fn ( ( ) : Promise < ResolutionDetails < boolean > > => {
638
667
return Promise . reject ( {
639
- reason : ERROR_REASON ,
640
- errorCode : ERROR_CODE ,
668
+ reason : StandardResolutionReasons . ERROR ,
669
+ errorCode : ErrorCode . INVALID_CONTEXT ,
641
670
} ) ;
642
671
} ) ,
643
672
} as unknown as Provider ;
@@ -717,8 +746,8 @@ describe('Hooks', () => {
717
746
] ,
718
747
resolveBooleanEvaluation : jest . fn ( ( ) : Promise < ResolutionDetails < boolean > > => {
719
748
return Promise . reject ( {
720
- reason : ERROR_REASON ,
721
- errorCode : ERROR_CODE ,
749
+ reason : StandardResolutionReasons . ERROR ,
750
+ errorCode : ErrorCode . PROVIDER_NOT_READY ,
722
751
} ) ;
723
752
} ) ,
724
753
} as unknown as Provider ;
0 commit comments