Gathering detailed insights and metrics for @sadams/wait-for-expect
Gathering detailed insights and metrics for @sadams/wait-for-expect
Gathering detailed insights and metrics for @sadams/wait-for-expect
Gathering detailed insights and metrics for @sadams/wait-for-expect
Wait for expectation to be true, useful for integration and end to end testing. Integral part of react-testing-library.
npm install @sadams/wait-for-expect
Typescript
Module System
Node Version
NPM Version
74.4
Supply Chain
99.4
Quality
79.3
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
5,720
Last Day
4
Last Week
191
Last Month
3,670
Last Year
5,720
1 Stars
63 Commits
2 Branches
2 Contributors
Latest Version
1.1.0
Package Id
@sadams/wait-for-expect@1.1.0
Unpacked Size
49.52 kB
Size
12.32 kB
File Count
22
NPM Version
10.8.2
Node Version
20.16.0
Publised On
13 Aug 2024
Cumulative downloads
Total Downloads
Last day
-98.4%
4
Compared to previous day
Last week
-78.2%
191
Compared to previous week
Last month
205.3%
3,670
Compared to previous month
Last year
0%
5,720
Compared to previous year
23
Wait for expectation to be true, useful for integration and end to end testing
Think things like calling external APIs, database operations, or even GraphQL subscriptions. We will add examples for all of them soon, for now please enjoy the simple docs. :-)
1const waitForExpect = require("wait-for-expect") 2 3test("it waits for the number to change", async () => { 4 let numberToChange = 10; 5 // we are using random timeout here to simulate a real-time example 6 // of an async operation calling a callback at a non-deterministic time 7 const randomTimeout = Math.floor(Math.random() * 300); 8 9 setTimeout(() => { 10 numberToChange = 100; 11 }, randomTimeout); 12 13 await waitForExpect(() => { 14 expect(numberToChange).toEqual(100); 15 }); 16});
instead of:
1 2test("it waits for the number to change", () => { 3 let numberToChange = 10; 4 const randomTimeout = Math.floor(Math.random() * 300); 5 6 setTimeout(() => { 7 numberToChange = 100; 8 }, randomTimeout); 9 10 setTimeout(() => { 11 expect(numberToChange).toEqual(100); 12 }, 700); 13});
It will check whether the expectation passes right away in the next available "tick" (very useful with, for example, integration testing of react when mocking fetches, like here: https://github.com/kentcdodds/react-testing-library#usage).
If it doesn't, it will keep repeating for the duration of, at most, the specified timeout, every 50 ms. The default timeout is 4.5 seconds to fit below the default 5 seconds that Jest waits for before throwing an error.
Nice thing about this simple tool is that if the expectation keeps failing till the timeout, it will check it one last time, but this time the same way your test runner would run it - so you basically get your expectation library error, the sam way like if you used setTimeout to wait but didn't wait long enough.
To show an example - if I change the expectation to wait for 105 in above code, you will get nice and familiar:
FAIL src/waitForExpect.spec.js (5.042s)
✕ it waits for the number to change (4511ms)
● it waits for the number to change
expect(received).toEqual(expected)
Expected value to equal:
105
Received:
100
9 | }, 600);
10 | await waitForExpect(() => {
> 11 | expect(numberToChange).toEqual(105);
12 | });
13 | });
14 |
at waitForExpect (src/waitForExpect.spec.js:11:28)
at waitUntil.catch (src/index.js:61:5)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 5.807s
You can add multiple expectations to wait for, all of them have to pass, and if one of them don't, it will be marked. For example, let's add another expectation for a different number, notice how jest tells you that that's the expectation that failed.
expect(received).toEqual(expected)
Expected value to equal:
110
Received:
105
11 | await waitForExpect(() => {
12 | expect(numberToChange).toEqual(100);
> 13 | expect(numberThatWontChange).toEqual(110);
14 | });
15 | });
16 |
at waitForExpect (src/waitForExpect.spec.js:13:34)
at waitUntil.catch (src/index.js:61:5)
Since 0.6.0 we can now work with promises, for example, this is now possible:
1test("rename todo by typing", async () => { 2 // (..) 3 const todoToChange = getTodoByText("original todo"); 4 todoToChange.value = "different text now"; 5 Simulate.change(todoToChange); 6 7 await waitForExpect(() => 8 expect( 9 todoItemsCollection.findOne({ 10 text: "different text now" 11 })).resolves.not.toBeNull() 12 ); 13});
Async Await also works, as in this example - straight from our test case
1test("it works with promises", async () => { 2 let numberToChange = 10; 3 const randomTimeout = Math.floor(Math.random() * 300); 4 5 setTimeout(() => { 6 numberToChange = 100; 7 }, randomTimeout); 8 9 const sleep = (ms) => 10 new Promise(resolve => setTimeout(() => resolve(), ms)); 11 12 await waitForExpect(async () => { 13 await sleep(10); 14 expect(numberToChange).toEqual(100); 15 }); 16});
(Note: Obviously, in this case it doesn't make sense to put the await sleep there, this is just for demonstration purpose)
waitForExpect takes 3 arguments, 2 optional.
1/** 2 * Waits for predicate to not throw and returns a Promise 3 * 4 * @param expectation Function Predicate that has to complete without throwing 5 * @param timeout Number Maximum wait interval, 4500ms by default 6 * @param interval Number Wait interval, 50ms by default 7 * @return Promise Promise to return a callback result 8 */
The defaults for timeout
and interval
can also be edited globally, e.g. in a jest setup file:
1import waitForExpect from 'wait-for-expect'; 2 3waitForExpect.defaults.timeout = 2000; 4waitForExpect.defaults.interval = 10;
1.0.0 - 15 June 2018
( For most people this change doesn't matter. ) Export the function directly in module.exports instead of exporting as an object that has default key. If that's not clear (...it isn't ;-) ) - check #8 #9 . Thanks to @mbaranovski for the PR and @BenBrostoff for creating the issue! I'm making this 1.0.0 as this is breaking for people that currently did:
1const { default: waitFor } = require('wait-for-expect');
0.6.0 - 3 May 2018
Work with promises.
0.5.0 - 10 April 2018
Play nicely with jest fake timers (and also in any test tool that overwrites setTimeout) - thanks to @slightlytyler and @kentcoddods for helping to get this resolved.
Originally based on ideas from https://github.com/devlato/waitUntil. Simplified highly and rewritten for 0.1.0 version. Simplified even more and rewritten even more for 0.2.0 with guidance from Kent C. Dodds: https://github.com/kentcdodds/react-testing-library/pull/25
No vulnerabilities found.
No security vulnerabilities found.