Search code examples
typescriptapiwebdriver-iosupertest

How to make an supertest Get API request every minute and then process the results


I have an API which returns the status of the server. When I start a service, time to time it's returns different status like --> sending data , validating , complete , ready.. etc. When service is fully up and running, then it's sending the state as "Ready". To get the status we have a separate API and I am using superTest client to get the response.

When(/^service is up and running$/, async () => {
    response = await request
        .get('/api/status/')
        .set('Accept', 'application/json')
        .auth('user', 'password')
});

The response of the Get request is similar to

{"env": "auto", "state": { "state": "ready", "msg": "Events Queued", }

To get into the "ready" state it's will take around 5-7 minutes. so I want to loop through the get request in every minute until I am getting the state as "ready". please, I want to know what is the best way of doing this using Typescripts. what I am doing right now is ,

When(/^service is up and running$/, async () => {
var responseState = '';
while(responseState != 'ready'){
response = await request
        .get('/api/status/')
        .set('Accept', 'application/json')
        .auth('user', 'password')
responseState = response.body.state;
delay(60000);
  } 
});

Solution

  • Your code looks good except that it is not accounting for a case where the server is never started because of some exception and your loop keeps trying until something times out. I would override the default cucumber step timeout for this and also add a counter for the loop which fails after it reaches the threshold. This way you can avoid timeout and infinite loop.

    The documentation on cucumberjs timeout is here.