@gojob/wait-for-assertion
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "@gojob/wait-for-assertion", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Wait for asynchronous testing assertions", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/gojob-1337/wait-for-assertion.git", |
@@ -13,2 +13,29 @@ # 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 | ||
@@ -27,3 +54,3 @@ | ||
// this would fail directly, if not awaited, as the document is not immediatly available in Elasticsearch | ||
// the document is not immediatly available in Elasticsearch: wait | ||
await waitForAssertion(async () => { | ||
@@ -34,2 +61,7 @@ const { document } = await elasticsearchService.get(UserIndex, userId); | ||
}); | ||
``` | ||
``` | ||
## 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/) |
7249
64