Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gojob-1337/wait-for-assertion
A wrapper for asynchronous testing assertions
https://github.com/gojob-1337/wait-for-assertion
Last synced: 3 days ago
JSON representation
A wrapper for asynchronous testing assertions
- Host: GitHub
- URL: https://github.com/gojob-1337/wait-for-assertion
- Owner: gojob-1337
- License: mit
- Created: 2019-05-23T12:49:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T22:28:52.000Z (almost 2 years ago)
- Last Synced: 2024-05-06T19:07:55.224Z (6 months ago)
- Language: TypeScript
- Homepage:
- Size: 720 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Wait for Assertion
A wrapper for asynchronous testing assertions.
## Getting started
```bash
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:
```typescript
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:
```typescript
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
```typescript
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](https://twitter.com/VinceOPS) at [Gojob](https://twitter.com/GojobT).
Initial blog story: 🇫🇷 [article](https://vinceops.me/nest-e2e-tests-effets-de-bord/)