Socket
Socket
Sign inDemoInstall

@gojob/wait-for-assertion

Package Overview
Dependencies
2
Maintainers
4
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @gojob/wait-for-assertion

Wait for asynchronous testing assertions


Version published
Weekly downloads
7
increased by600%
Maintainers
4
Install size
4.93 MB
Created
Weekly downloads
 

Readme

Source

Wait for Assertion

A wrapper for asynchronous testing assertions.

Getting started

yarn add @gojob/wait-for-assertion
# or
npm install @gojob/wait-for-assertion

How

By "asynchronous assertions", we means Assertions that must be executed with a delay. The typical use case that we want to avoid is the following:

await myFunctionWithAsyncSideEffects();

await new Promise(resolve => setTimeout(resolve, 1000)); // ❌ 🤢 🤮
expect(mySideEffectExists()).toBe(true);

waitForAssertion will run the given function (containing assertions) until it stops throwing exceptions (which means that it "passed"), or will throw a Timeout error.

waitForAssertion accepts up to three parameters:

function waitForAssertion(
  assertion: () => any,
  timeoutDelay: number = 1000,
  intervalDelay: number = 100
);
  • assertion: Closure containing asynchronous assertions to be made.
  • timeoutDelay: [1000ms] How long should the assertion be repeated until it passes (or times out).
  • intervalDelay: [100ms] How often should the assertion be repeated during timeoutDelay.

Example

import { waitForAssertion } from '@gojob/wait-for-assertion';

// [...]

it('should asynchronously update the value in Elasticsearch', async () => {
  await request(server)
    .put(endpointURL)
    .send(input)
    .expect(HttpStatus.NO_CONTENT);

  // the document is not immediatly available in Elasticsearch: wait
  await waitForAssertion(async () => {
    const { document } = await elasticsearchService.get(UserIndex, userId);
    return expect(document.firstName).toBe(updatedUser.firstName);
  });
});

Credits

Developed by VinceOPS at Gojob.
Initial blog story: 🇫🇷 article

FAQs

Last updated on 23 May 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc