Skip to content
On this page

Empower Your Test Results with AI-Powered Insights ->

retry

Retry an HTTP request until certain conditions are met.

Syntax

js
retry()
retry(count)
retry(count, delay)
retry(options)

Usage

โœ… Correct Usage

js
// retry on expectations
await spec()
  .get('/api/users')
  .expectStatus(200)
  .retry();
js
// retry with custom count
await spec()
  .get('/api/users')
  .expectStatus(200)
  .retry(1);
js
// retry with custom count and delay
await spec()
  .get('/api/users')
  .expectStatus(200)
  .retry(1, 500);
js
// retry with options
await spec()
  .get('/api/users')
  .expectStatus(200)
  .retry({
    count: 2,
    delay: 2000,
    strategy: ({res}) => { return res.statusCode === 200 }
  });
js
// retry with retry-handler
await spec()
  .get('/api/users')
  .expectStatus(200)
  .retry({
    strategy: 'on 404'
  });
js
// retry with status code
await spec()
  .get('/api/users')
  .expectStatus(200)
  .retry({
    status: 500
  });
js
// retry with multiple status codes
await spec()
  .get('/api/users')
  .expectStatus(200)
  .retry({
    status: [502, 503]
  });

Arguments

> count (number)

Max retry attempts.

> delay (number)

Delay between retry attempts in milliseconds.

> options (number)

Retry options.

PropertyTypeDescription
countnumbernumber of times to retry - defaults to 1
delaynumberdelay between retries - defaults to 1000ms
strategyfunctionretry strategy function - returns boolean
strategystringretry strategy handler name
statusnumberretry for the given status code
statusnumber[]retry for the given status codes

Examples

Using Retry Handlers

js
const { spec, handler } = require('pactum');

handler.addRetryHandler('on 404', (ctx) => {
  const res = ctx.res;
  if (res.statusCode === 404) {
    return false;
  } else {
    return true;
  }
});

await spec()
  .get('http://jsonplaceholder.typicode.com/posts')
  .retry({
    strategy: 'on 404'
  })
  .expectStatus(200);

See Also

Released under the MIT License.