@@ -10,6 +10,7 @@ import {
1010 OpenFeature ,
1111 OpenFeatureClient ,
1212 Provider ,
13+ ProviderStatus ,
1314 ResolutionDetails ,
1415 StandardResolutionReasons ,
1516 TransactionContext ,
@@ -82,14 +83,73 @@ const MOCK_PROVIDER: Provider = {
8283} ;
8384
8485describe ( 'OpenFeatureClient' , ( ) => {
85- beforeAll ( ( ) => {
86+ beforeEach ( ( ) => {
8687 OpenFeature . setProvider ( MOCK_PROVIDER ) ;
8788 } ) ;
8889
8990 afterEach ( ( ) => {
9091 jest . clearAllMocks ( ) ;
9192 } ) ;
9293
94+ describe ( 'Requirement 1.1.8' , ( ) => {
95+ class mockAsyncProvider implements Provider {
96+ metadata = {
97+ name : 'mock-async' ,
98+ } ;
99+
100+ status = ProviderStatus . NOT_READY ;
101+ readonly runsOn = 'server' ;
102+
103+ constructor ( private readonly throwInInit : boolean ) { }
104+
105+ async initialize ( ) : Promise < void > {
106+ if ( this . throwInInit ) {
107+ try {
108+ throw new Error ( 'provider failed to initialize' ) ;
109+ } catch ( err ) {
110+ this . status = ProviderStatus . ERROR ;
111+ throw err ;
112+ }
113+ }
114+ this . status = ProviderStatus . READY ;
115+ return ;
116+ }
117+
118+ resolveBooleanEvaluation ( ) : Promise < ResolutionDetails < boolean > > {
119+ throw new Error ( 'Method not implemented.' ) ;
120+ }
121+ resolveStringEvaluation ( ) : Promise < ResolutionDetails < string > > {
122+ throw new Error ( 'Method not implemented.' ) ;
123+ }
124+ resolveNumberEvaluation ( ) : Promise < ResolutionDetails < number > > {
125+ throw new Error ( 'Method not implemented.' ) ;
126+ }
127+ resolveObjectEvaluation < T extends JsonValue > ( ) : Promise < ResolutionDetails < T > > {
128+ throw new Error ( 'Method not implemented.' ) ;
129+ }
130+ }
131+
132+ it ( 'should wait for the provider to successfully initialize' , async ( ) => {
133+ const spy = jest . spyOn ( mockAsyncProvider . prototype , 'initialize' ) ;
134+
135+ const provider = new mockAsyncProvider ( false ) ;
136+ expect ( provider . status ) . toBe ( ProviderStatus . NOT_READY ) ;
137+ await OpenFeature . setProviderAndWait ( provider ) ;
138+ expect ( provider . status ) . toBe ( ProviderStatus . READY ) ;
139+ expect ( spy ) . toBeCalled ( ) ;
140+ } ) ;
141+
142+ it ( 'should wait for the provider to fail during initialization' , async ( ) => {
143+ const spy = jest . spyOn ( mockAsyncProvider . prototype , 'initialize' ) ;
144+
145+ const provider = new mockAsyncProvider ( true ) ;
146+ expect ( provider . status ) . toBe ( ProviderStatus . NOT_READY ) ;
147+ await expect ( OpenFeature . setProviderAndWait ( provider ) ) . rejects . toThrow ( ) ;
148+ expect ( provider . status ) . toBe ( ProviderStatus . ERROR ) ;
149+ expect ( spy ) . toBeCalled ( ) ;
150+ } ) ;
151+ } ) ;
152+
93153 describe ( 'Requirement 1.2.1' , ( ) => {
94154 it ( 'should allow addition of hooks' , ( ) => {
95155 expect ( OpenFeatureClient . prototype . addHooks ) . toBeDefined ( ) ;
@@ -358,7 +418,7 @@ describe('OpenFeatureClient', () => {
358418 const defaultNumberValue = 123 ;
359419 const defaultStringValue = 'hey!' ;
360420
361- beforeAll ( async ( ) => {
421+ beforeEach ( async ( ) => {
362422 OpenFeature . setProvider ( errorProvider ) ;
363423 client = OpenFeature . getClient ( ) ;
364424 nonOpenFeatureErrorDetails = await client . getNumberDetails ( 'some-flag' , defaultNumberValue ) ;
0 commit comments