5
5
EvaluationContext ,
6
6
EvaluationDetails ,
7
7
FlagNotFoundError ,
8
+ Hook ,
8
9
JsonArray ,
9
10
JsonObject ,
10
11
JsonValue ,
@@ -18,6 +19,8 @@ import {
18
19
TransactionContextPropagator ,
19
20
} from '../src' ;
20
21
import { OpenFeatureClient } from '../src/client/internal/open-feature-client' ;
22
+ import { isDeepStrictEqual } from 'node:util' ;
23
+ import { HookContext } from '@openfeature/core' ;
21
24
22
25
const BOOLEAN_VALUE = true ;
23
26
const STRING_VALUE = 'val' ;
@@ -740,9 +743,35 @@ describe('OpenFeatureClient', () => {
740
743
// Set Client Context
741
744
const client = OpenFeature . getClient ( 'contextual' , 'test' , clientContext ) ;
742
745
// Set Hook Context
743
- client . addHooks ( { before : ( ) => beforeHookContext } ) ;
746
+ const hook = {
747
+ before : jest . fn ( ( hookContext : HookContext ) => {
748
+ // we have to put this assertion here because of limitations in jest with expect.objectContaining and mutability
749
+ if ( isDeepStrictEqual ( hookContext . context , {
750
+ ...globalContext ,
751
+ ...transactionContext ,
752
+ ...clientContext ,
753
+ ...invocationContext ,
754
+ // before hook context should be missing here (and not overridden)
755
+ } ) ) {
756
+ return beforeHookContext ;
757
+ }
758
+ } ) ,
759
+ after : jest . fn ( ( hookContext : HookContext ) => {
760
+ // we have to put this assertion here because of limitations in jest with expect.objectContaining and mutability
761
+ if ( isDeepStrictEqual ( hookContext . context , {
762
+ ...globalContext ,
763
+ ...transactionContext ,
764
+ ...clientContext ,
765
+ ...invocationContext ,
766
+ ...beforeHookContext ,
767
+ } ) ) {
768
+ return beforeHookContext ;
769
+ }
770
+ } ) ,
771
+ } as unknown as Hook ;
772
+
773
+ await client . getBooleanValue ( flagKey , defaultValue , invocationContext , { hooks : [ hook ] } ) ;
744
774
745
- await client . getBooleanValue ( flagKey , defaultValue , invocationContext ) ;
746
775
expect ( provider . resolveBooleanEvaluation ) . toHaveBeenCalledWith (
747
776
expect . anything ( ) ,
748
777
expect . anything ( ) ,
0 commit comments