I am facing an error saying unsyntactic break, how do I resolve it?
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 the API is not healthy and stop the execution.
When the 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>
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;
}
}
})
}
});
Unsyntactic Break should be inside a loop, you are calling break inside a callback function , this is why you are getting unsyntactic break if you want to break from call back replace break with return but this won't break the outside for loop.
you should use await :
for (let i = 0; i < maxRetry xss=removed xss=removed xss=removed xss=removed>>'));
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;
}
}
Note: To use await the IT block or function inside which you are calling this code should be declared as asyn
eg:
it('should edit a new item', async () => {
for (let i = 0; i < maxRetry xss=removed xss=removed xss=removed xss=removed>>'));
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;
}
}
}