I am writing a program to check the health endpoint of one of our APIs. So, before starting execution, I try to hit the health endpoint 5 times. If I get the desired response code during the 5 attempts, I break the loop and start test execution else give a message saying API is not healthy and stop the execution.
When below function is executed, it results in an error saying unsyntactic break
Can someone suggest a better way to address this? I am using cypress.
const maxRetry = Cypress.env('maxWarmUpRetries');
const sleepTimeMs = 200;
console.log(chalk.yellow('Checking API health before test execution...'));
let healthCheckResponse;
let healthStatusCode;
for (let i = 0; i < maxRetry; i++) {
console.log('Value of i is: ',i)
apiHelper.requestHealth.then(response=>{
healthCheckResponse=response;
console.log('healthCheckStatusCode: ',healthCheckResponse.status)
if (healthCheckResponse) {
healthStatusCode = healthCheckResponse.status;
if (healthStatusCode === 200) {
console.log(chalk.green('API is healthy, moving forward with execution >>'));
break;
} else {
console.log(chalk.yellow('Warm up - Checking API /health before test execution...'));
if (i === maxRetry - 1) {
console.log(chalk.red('Warm up - API /health is down!!! Suspending test execution.'));
break;
}
}
} else {
console.log(chalk.yellow('Warm up - Checking API /health before test execution...'));
if (i === maxRetry - 1) {
console.log(chalk.red('Warm up - API /health is down!!! Suspending test execution.'));
break;
}
}
})
}
});