1.0.4 • Published 5 years ago

@gojob/wait-for-assertion v1.0.4

Weekly downloads
65
License
MIT
Repository
github
Last release
5 years ago

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

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago