Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

retry-assert

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

retry-assert

Retry a function until or while it passes an assertion

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
561
decreased by-13.43%
Maintainers
1
Weekly downloads
 
Created
Source

Retry Assert

Retry a function, either until or while an assertion passes, seamlessly integrating with Promise-aware test runners such as Mocha, Jest and Cucumber JS, producing better failure messages than other similar libraries. Inspired by RSpec-Wait and TryTryAgain.

Installation

npm install retry-assert

Usage

Retry until: retry a function until it's returned value passes an assertion:

import retry = from 'retry-assert';
import { expect } from 'expect';

async function getUser (id) {
  console.log('get user');
  return { id, active: Date.now() % 10 === 0 };
}

// call the asynchronous "getUser" method until user is active:
const activeUser = await retry()
  .fn(() => getUser(1))
  .until(user => expect(user).toHaveProperty('active', true));
console.log(activeUser);

Retry ensure: retry a function until timeout, failing immediately when an assertion fails:

import retry from 'retry-assert';
import { expect } from 'expect';

async function getUser (id) {
  console.log('get user');
  return { id, active: Date.now() % 10 === 0 };
}

// call the asynchronous "getUser" function repeatedly for 2 seconds,
// ensuring user is not active:
const inactiveUser = await retry()
  .fn(() => getUser(2))
  .withTimeout(2000)
  .ensure(user => expect(user).toHaveProperty('active', false));
console.log(inactiveUser);

Motivation

Testing asynchronous state changes requires waiting for the state change to occur. Waiting a fixed amount of time (using setTimeout or equivalent) makes the test suite slow and fails to communicate the reason for waiting. UI test frameworks often have retry capabilities built in but when testing outside of the browser some other utility is required to reliably wait for asynchronous state changes.

Features

There are many libraries that already solve this problem. This one is unique because it offers all of the following features:

  • Async - Retried function can be async.
  • Config - Configurable retry timeout.
  • Fluent - Retry behavior is configured with fluent builder syntax.
  • Yields - Result of retried function is yielded when predicate passes.
  • Assert - Retry condition/assertion defined independently of retried function.
  • Ensure - Supports negative test cases (ensuring no state change).

As of the time of writing, the following table describes the feature sets of similar retry libraries:

LibraryAsyncConfigYieldsFluentAssertEnsureNotes / Syntax
Retry-AssertYesYesYesYesYesYesretry(fn).until(() => assertion)
RSpec-WaitYesYesYesYesRuby
TryTryAgainYesYesYesYesretry(fn, options)
Async-Wait-UntilYesYesYeswaitUntil(fn, timeout, delay)
Retry-As-PromisedYesYesYesretry(fn, options)
Wait-UntilYesYesYeswaitUntil().times(5).condition(fn)
Wait-Until-PromiseYesYeswaitUntil(fn, timeout, delay)
P-Wait-ForYesYespWaitFor(condition, options)
Mocha-RetryYesYesRetries entire test
Wait-For-StuffYeswait.for.predicate(fn)

API

Module

The retry-assert module exports a single function. The following describes its usage assuming it is imported as "retry".

retry()

Create a new RetryBuilder.

Returns: RetryBuilder

retry(fn)

Shorthand for retry().fn(fn).

Retuns: RetryBuilder

retry.defaultRetryDelay

Default RetryBuilder retry delay milliseconds (default 200). Modifying only affects future RetryBuilder instances.

retry.defaultTimeout

Default RetryBuilder timeout milliseconds (default 1000). Modifying only affects future RetryBuilder instances.

RetryBuilder

A RetryBuilder allows chaining of the retry configuration. It should have any number of "chainable" configuration methods called followed by a single "terminal" method invocation. The terminal methods all return a promise resolving the latest return value of the retried function.

.fn(fn)

chainable

Set the function to be retried. The given function may return a Promise. The result of the final invocation of the given function will be resolved by the promise returned by this builder's terminal operation.

Returns: RetryBuilder

Example:

retry()
  .fn(() => httpClient.get('/deleted-resource'))
  .until(response => expect(response).toHaveProperty('status', 404))
.until(fn)

terminal

Set the assertion function to apply to the result of each invocation of the retried function and begin retrying until the assertion passes or the timeout is reached.

Any assertion library can be used as long as a failed assertion throws an exception.

Retuns: Promise

The returned Promise will resolve the last result of the retried function. If retrying timed out then the Promise will be rejected.

Example:

retry(() => client.getUser(id))
  .until(user => expect(user).toHaveProperty('active', true))
.ensure(fn)

terminal

Set the assertion function to apply to the result of each invocation of the retried function and begin retrying until the timeout is reached or until assertion fails.

Any assertion library can be used as long as a failed assertion throws an exception.

Retuns: Promise

The returned Promise will resolve the last result of the retried function. If an assertion failed then the Promise will be rejected.

Example:

retry(() => client.getUser(id))
  .ensure(user => expect(user).toHaveProperty('active', false))
.untilTruthy()

terminal

Shorthand for .untilTruthy(x => x).

Retuns: Promise

.untilTruthy(fn)

terminal

Set the predicate function to apply to the result of each invocation of the retried function and begin retrying until the predicate is truthy or the timeout is reached.

Retuns: Promise

The returned Promise will resolve the last result of the retried function. If retrying timed out then the Promise will be rejected.

Example:

retry(() => client.getUser(id))
  .untilTruthy(user => user.active)
.ensureTruthy()

terminal

Shorthand for .ensureTruthy(x => x).

Retuns: Promise

.ensureTruthy(fn)

terminal

Set the predicate function to apply to the result of each invocation of the retried function and begin retrying until the timeout is reached or until the predicate is falsy.

Retuns: Promise

The returned Promise will resolve the last result of the retried function. If a predicate was falsy then the Promise will be rejected.

Example:

retry(() => client.getUser(id))
  .ensureTruthy(user => user.active)
.withRetryDelay(n)

chainable

Set the number of milliseconds between retries (default 200).

Returns: RetryBuilder

.withTimeout(n)

chainable

Set the number of milliseconds before timing out (default 1000).

This value is use to determine when to stop retrying; it is not used to timeout individual invocations of the retried function. The retried function needs to be responsible for timing out long running operations such as via http client configuration etc.

The amount of time passed before timing out is only guaranteed to be at least this long. This value is used to approximate the number of retries up-front without considering the length of time each retry takes. If retries are long-running then the time until timeout occurs may be significantly longer than this value.

Returns: RetryBuilder

Copyright 2018 Practiv Ltd and Copyright 2023 Nathan Jones. Licensed under the Apache License, Version 2.0.

Keywords

FAQs

Package last updated on 28 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc