11import ResilientOperation from '../ResilientOperation.js' ;
2- import { jest , describe , expect , test , beforeEach } from '@jest/globals' ;
2+ import { jest , describe , expect , test , beforeEach , afterEach } from '@jest/globals' ;
33
44describe ( 'ResilientOperation E2E Tests' , ( ) => {
55 let resilientOp ;
@@ -17,6 +17,10 @@ describe('ResilientOperation E2E Tests', () => {
1717 } ) ;
1818 } ) ;
1919
20+ afterEach ( ( ) => {
21+ jest . clearAllMocks ( ) ;
22+ } ) ;
23+
2024 describe ( 'Test 1: Basic Retry Logic' , ( ) => {
2125 test ( 'should retry failed calls and eventually succeed' , async ( ) => {
2226 // Create a ResilientOperation with longer timeout for this specific test
@@ -31,9 +35,6 @@ describe('ResilientOperation E2E Tests', () => {
3135
3236 let callCount = 0 ;
3337 const mockAsyncFn = jest . fn ( ) . mockImplementation ( async ( apiUrl , requestBody , headers ) => {
34- console . log ( 'apiUrl' , apiUrl ) ;
35- console . log ( 'requestBody' , requestBody ) ;
36- console . log ( 'headers' , headers ) ;
3738 callCount ++ ;
3839
3940 // Fail first 2 times with server error (5xx), succeed on 3rd try
@@ -53,7 +54,7 @@ describe('ResilientOperation E2E Tests', () => {
5354 // Test arguments passed to the function
5455 expect ( mockAsyncFn ) . toHaveBeenCalledWith ( ...asynFnArgs ) ;
5556 expect ( result ) . toEqual ( { data : 'success' } ) ;
56- } , 10000 ) ;
57+ } , 60000 ) ;
5758
5859 test ( 'should handle rate limit errors with retry' , async ( ) => {
5960 let callCount = 0 ;
@@ -79,7 +80,7 @@ describe('ResilientOperation E2E Tests', () => {
7980
8081 expect ( mockAsyncFn ) . toHaveBeenCalledTimes ( 2 ) ;
8182 expect ( result ) . toEqual ( { data : 'success' } ) ;
82- } , 10000 ) ;
83+ } , 60000 ) ;
8384 } ) ;
8485
8586 describe ( 'Test 2: Circuit Breaker' , ( ) => {
@@ -107,12 +108,12 @@ describe('ResilientOperation E2E Tests', () => {
107108 expect ( resilientOp . failCount ) . toBeGreaterThan ( 5 ) ;
108109
109110 // Debug: Log the actual failCount to understand what's happening
110- console . log ( 'Circuit breaker state:' , {
111- circuitOpen : resilientOp . circuitOpen ,
112- failCount : resilientOp . failCount ,
113- circuitBreakerThreshold : resilientOp . circuitBreakerThreshold
114- } ) ;
115- } , 10000 ) ;
111+ // console.log('Circuit breaker state:', {
112+ // circuitOpen: resilientOp.circuitOpen,
113+ // failCount: resilientOp.failCount,
114+ // circuitBreakerThreshold: resilientOp.circuitBreakerThreshold
115+ // });
116+ } , 60000 ) ;
116117
117118 test ( 'should not open circuit breaker with mixed success/failure' , async ( ) => {
118119 // Create a fresh ResilientOperation to avoid interference from previous test
@@ -135,37 +136,36 @@ describe('ResilientOperation E2E Tests', () => {
135136
136137 // Fail every 3rd call with server error, succeed otherwise
137138 if ( callCount % 3 === 0 ) {
138- console . log ( `Call ${ callCount } is FAILING` ) ;
139139 const error = new Error ( 'Server error' ) ;
140140 error . response = { status : 500 } ;
141141 throw error ;
142142 }
143- console . log ( `Call ${ callCount } is SUCCEEDING` ) ;
143+ // console.log(`Call ${callCount} is SUCCEEDING`);
144144 return { data : 'success' } ;
145145 } ) ;
146146
147147 // Disable retries for this test to see the actual failure pattern
148148 freshResilientOp . retries = 0 ;
149149
150- console . log ( 'Mock function created, callCount starts at 0' ) ;
150+ // console.log('Mock function created, callCount starts at 0');
151151
152152 const promises = [ ] ;
153153 for ( let i = 0 ; i < 6 ; i ++ ) {
154- console . log ( `Starting call ${ i + 1 } ` ) ;
154+ // console.log(`Starting call ${i + 1}`);
155155 promises . push ( freshResilientOp . execute ( mockAsyncFn ) . catch ( err => {
156- console . log ( `Call ${ i + 1 } failed:` , err . message ) ;
156+ // console.log(`Call ${i + 1} failed:`, err.message);
157157 return err ;
158158 } ) ) ;
159159 }
160160
161161 const results = await Promise . all ( promises ) ;
162162
163163 // Debug: Check circuit breaker state immediately after execution
164- console . log ( 'Circuit breaker state after execution:' , {
165- circuitOpen : freshResilientOp . circuitOpen ,
166- failCount : freshResilientOp . failCount ,
167- circuitBreakerThreshold : freshResilientOp . circuitBreakerThreshold
168- } ) ;
164+ // console.log('Circuit breaker state after execution:', {
165+ // circuitOpen: freshResilientOp.circuitOpen,
166+ // failCount: freshResilientOp.failCount,
167+ // circuitBreakerThreshold: freshResilientOp.circuitBreakerThreshold
168+ // });
169169
170170 // Circuit should remain closed due to mixed success/failure
171171 expect ( freshResilientOp . circuitOpen ) . toBe ( false ) ;
@@ -176,24 +176,24 @@ describe('ResilientOperation E2E Tests', () => {
176176 const failureCount = results . filter ( r => r instanceof Error ) . length ;
177177
178178 // Debug: Log each result
179- console . log ( 'Individual results:' ) ;
180- results . forEach ( ( result , index ) => {
181- console . log ( `Result ${ index + 1 } :` , result instanceof Error ? 'Error' : 'Success' , result ) ;
182- } ) ;
179+ // console.log('Individual results:');
180+ // results.forEach((result, index) => {
181+ // console.log(`Result ${index + 1}:`, result instanceof Error ? 'Error' : 'Success', result);
182+ // });
183183
184- // Debug: Log what we got
185- console . log ( 'Mixed success/failure test results:' , {
186- totalResults : results . length ,
187- successCount,
188- failureCount,
189- circuitOpen : freshResilientOp . circuitOpen ,
190- failCount : freshResilientOp . failCount ,
191- results : results . map ( r => r instanceof Error ? 'Error' : 'Success' )
192- } ) ;
184+ // // Debug: Log what we got
185+ // console.log('Mixed success/failure test results:', {
186+ // totalResults: results.length,
187+ // successCount,
188+ // failureCount,
189+ // circuitOpen: freshResilientOp.circuitOpen,
190+ // failCount: freshResilientOp.failCount,
191+ // results: results.map(r => r instanceof Error ? 'Error' : 'Success')
192+ // });
193193
194194 expect ( successCount ) . toBeGreaterThan ( 0 ) ;
195195 expect ( failureCount ) . toBeGreaterThan ( 0 ) ;
196- } , 10000 ) ;
196+ } , 50000 ) ;
197197 } ) ;
198198
199199 describe ( 'Test 3: Caching' , ( ) => {
@@ -234,7 +234,7 @@ describe('ResilientOperation E2E Tests', () => {
234234
235235 // Verify cache store has the entry
236236 expect ( Object . keys ( cacheStore ) . length ) . toBe ( 1 ) ;
237- } , 10000 ) ;
237+ } , 60000 ) ;
238238
239239 test ( 'should apply different preset configurations' , async ( ) => {
240240 const presetResilientOp = new ResilientOperation ( {
@@ -262,6 +262,6 @@ describe('ResilientOperation E2E Tests', () => {
262262 expect ( presetResilientOp . presets . fast . retries ) . toBe ( 1 ) ;
263263 expect ( presetResilientOp . presets . reliable . timeout ) . toBe ( 300000 ) ;
264264 expect ( presetResilientOp . presets . reliable . retries ) . toBe ( 5 ) ;
265- } , 10000 ) ;
265+ } , 60000 ) ;
266266 } ) ;
267267} ) ;
0 commit comments