@gojob/wait-for-assertion
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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

Dependencies (1)

Dev Dependencies (9)

Package Sidebar

Install

npm i @gojob/wait-for-assertion

Weekly Downloads

6

Version

1.0.4

License

MIT

Unpacked Size

7.47 kB

Total Files

7

Last publish

Collaborators

  • dedicad
  • bruno.devaux.gojob
  • ericchaps
  • fcarlue
  • alexian-serrano
  • remi-bruguier
  • davidolivier1337
  • miramo