Gathering detailed insights and metrics for wait-for-expect
Gathering detailed insights and metrics for wait-for-expect
Gathering detailed insights and metrics for wait-for-expect
Gathering detailed insights and metrics for wait-for-expect
wait-for-throwable
Simple utility to retry an erroring function until it succeeds
@sadams/wait-for-expect
Wait for expectation to be true, useful for integration and end to end testing
chai-wait-for
Drop-in replacement for expect that waits for the assertion to succeed (retries on an interval you choose, until a timeout you choose)
Wait for expectation to be true, useful for integration and end to end testing. Integral part of react-testing-library.
npm install wait-for-expect
Typescript
Module System
Node Version
NPM Version
99.4
Supply Chain
100
Quality
76.1
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
257,929,619
Last Day
15,817
Last Week
528,665
Last Month
2,253,097
Last Year
30,674,490
294 Stars
52 Commits
31 Forks
3 Watching
3 Branches
19 Contributors
Minified
Minified + Gzipped
Latest Version
3.0.2
Package Id
wait-for-expect@3.0.2
Size
9.15 kB
NPM Version
6.4.1
Node Version
8.10.0
Publised On
06 Feb 2020
Cumulative downloads
Total Downloads
Last day
-13.1%
15,817
Compared to previous day
Last week
-5.9%
528,665
Compared to previous week
Last month
-12%
2,253,097
Compared to previous month
Last year
-22.9%
30,674,490
Compared to previous year
27
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.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 12/22 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
103 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More