@@ -50,6 +50,7 @@ const tryRefreshCredentials = async (parentRoleArn: string, childRoleArn?: strin
5050} ;
5151
5252let isRotationBackgroundTaskAlreadyScheduled = false ;
53+ let credentialRefreshTimer : ReturnType < typeof setInterval > | undefined ;
5354
5455/**
5556 * Schedules a background task that attempts to refresh test account credentials
@@ -66,20 +67,34 @@ export const tryScheduleCredentialRefresh = () => {
6667
6768 if ( process . env . CHILD_ACCOUNT_ROLE ) {
6869 // Attempts to refresh credentials in background every 10 minutes.
69- setInterval ( ( ) => {
70+ credentialRefreshTimer = setInterval ( ( ) => {
7071 void tryRefreshCredentials ( process . env . TEST_ACCOUNT_ROLE , process . env . CHILD_ACCOUNT_ROLE ) ;
7172 } , 10 * 60 * 1000 ) ;
73+ credentialRefreshTimer . unref ( ) ;
7274
7375 console . log ( 'Test profile credentials refresh was scheduled for child account' ) ;
7476 } else {
7577 // CDK tests and tests with USE_PARENT_ACCOUNT only use the parent account role.
7678 // Refresh the parent account credentials to prevent expiration during long-running tests.
77- setInterval ( ( ) => {
79+ credentialRefreshTimer = setInterval ( ( ) => {
7880 void tryRefreshCredentials ( process . env . TEST_ACCOUNT_ROLE ) ;
7981 } , 10 * 60 * 1000 ) ;
82+ credentialRefreshTimer . unref ( ) ;
8083
8184 console . log ( 'Test profile credentials refresh was scheduled for parent account' ) ;
8285 }
8386
8487 isRotationBackgroundTaskAlreadyScheduled = true ;
8588} ;
89+
90+ /**
91+ * Stops the credential refresh timer. Call this in afterAll to prevent
92+ * "Cannot log after tests are done" warnings from Jest.
93+ */
94+ export const stopCredentialRefresh = ( ) => {
95+ if ( credentialRefreshTimer ) {
96+ clearInterval ( credentialRefreshTimer ) ;
97+ credentialRefreshTimer = undefined ;
98+ }
99+ isRotationBackgroundTaskAlreadyScheduled = false ;
100+ } ;
0 commit comments