-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(assertWithTimeout): add async assertion util
- Loading branch information
1 parent
d9b92da
commit 3d8c9b8
Showing
3 changed files
with
34 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Repeatedly attempt to make an assertion over a period of time. | ||
* If the assertion never passes, it will eventually throw. | ||
* Good for tests with unknown or unreliable execution times. | ||
* | ||
* @param {function} assertion - A callback that makes test assertions. | ||
* @param {function} done - The done callback. | ||
*/ | ||
const assertWithTimeout = (assertion, done) => { | ||
const timeout = 1000 * 2 | ||
const start = Date.now() | ||
|
||
const assert = () => { | ||
try { | ||
assertion() | ||
done() | ||
} catch (err) { | ||
if (Date.now() - start >= timeout) throw err | ||
|
||
setTimeout(assert, 10) | ||
} | ||
} | ||
|
||
assert() | ||
} | ||
|
||
|
||
export default assertWithTimeout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters