Gathering detailed insights and metrics for async-test-util
Gathering detailed insights and metrics for async-test-util
Gathering detailed insights and metrics for async-test-util
Gathering detailed insights and metrics for async-test-util
typechecker
Utilities to get and check variable types (isString, isPlainObject, isRegExp, etc)
@stdlib/utils-async-while
Invoke a function while a test condition is true.
@stdlib/utils-async-until
Invoke a function until a test condition is true.
@stdlib/utils-async-any-by
Test whether at least one element in a collection passes a test implemented by a predicate function.
Utility functions that are useful in async/await tests Utility functions that are useful in async/await tests :thumbsup:
npm install async-test-util
Typescript
Module System
Node Version
NPM Version
84.3
Supply Chain
99.1
Quality
75.6
Maintenance
50
Vulnerability
100
License
JavaScript (100%)
Total Downloads
1,295,764
Last Day
1,623
Last Week
8,410
Last Month
38,651
Last Year
374,170
Apache-2.0 License
27 Stars
424 Commits
2 Forks
2 Watchers
1 Branches
3 Contributors
Updated on May 08, 2025
Minified
Minified + Gzipped
Latest Version
2.5.0
Package Id
async-test-util@2.5.0
Unpacked Size
61.05 kB
Size
14.61 kB
File Count
61
NPM Version
10.7.0
Node Version
22.1.0
Published on
May 05, 2024
Cumulative downloads
Total Downloads
Last Day
45.3%
1,623
Compared to previous day
Last Week
5%
8,410
Compared to previous week
Last Month
11.3%
38,651
Compared to previous month
Last Year
120.8%
374,170
Compared to previous year
5
36
Utility-functions that can be usefull when you have asynchronous tests in javascript.
1$ npm install async-test-util --save-dev
1// es6 2import AsyncTestUtil from 'async-test-util'; 3 4// es5 5var AsyncTestUtil = require('async-test-util');
Waits until the given time has expired and then resolves.
1it('should wait', async() => { 2 await AsyncTestUtil.wait(200); 3 console.log('200 ms is over'); 4});
Waits until the given timeout has expired or the resolve was triggered manually
1it('should wait until observable fired or time is over', async() => { 2 const resolveable = AsyncTestUtil.waitResolveable(200); 3 myObservable.subscribe(sth => { 4 console.log('got first event'); 5 resolveable.resolve(); 6 }); 7 await resolveable.promise; 8});
Waits until the given predicate-function returns a truthy value. Throws if the optional timeout has passed before.
1it('should wait until server is online', async() => { 2 const checkServer = async() => { 3 try{ 4 await fetch('http://example.com/api/'); 5 return true; 6 }catch(err) { 7 return false; 8 } 9 }; 10 await AsyncTestUtil.waitUntil(checkServer); 11});
With timeout:
1it('should wait until server is online (maxtime: 1000ms)', async() => { 2 const checkServer = async() => { 3 try{ 4 await fetch('http://example.com/api/'); 5 return true; 6 }catch(err) { 7 return false; 8 } 9 }; 10 await AsyncTestUtil.waitUntil(checkServer, 1000); 11});
With return value:
1it('should wait until server is online (maxtime: 1000ms)', async() => { 2 const checkServer = async() => { 3 try{ 4 const response = await fetch('http://example.com/api/'); 5 return await response.json(); 6 }catch(err) { 7 return false; 8 } 9 }; 10 const firstSuccessfullResponse = await AsyncTestUtil.waitUntil(checkServer, 1000); 11});
Waits forever, never resolves.
1it('should never resolve', async() => { 2 let resolved = false; 3 AsyncTestUtil 4 .waitForever() 5 .then(() => resolved = true); 6 await AsyncTestUtil.wait(100); 7 assert.equal(false, resolved); 8});
Runs the given predicate-function forever. Between each run, the interval-time is awaited.
1it('should run forever', async() => { 2 let t = 0; 3 const pred = () => t++; 4 AsyncTestUtil.runForever( 5 pred, // predicate-function 6 10 // interval 7 ); 8 9 await AsyncTestUtil.wait(100); 10 assert.ok(t > 4); 11 const lastT = t; 12 await AsyncTestUtil.wait(100); 13 assert.ok(t > lastT); 14});
Async-Form of assert.throws. Asserts that the given function throws with the defined error, throws if not.
1// with error-type 2it('should throw because route does not exist', async() => { 3 const getServerVersion = async() => { 4 const response = await fetch('http://example.com/foobar/'); 5 return response; 6 }; 7 await AsyncTestUtil.assertThrows( 8 () => getServerVersion(), // function that throws (required) 9 Error // Error-type (optional) 10 ); 11}); 12 13// with error-text-flag 14it('should throw because route does not exist', async() => { 15 const pingServer = async() => { 16 try{ 17 await fetch('http://example.com/foobar/'); 18 }catch(err) { 19 throw new Error('route not reachable'); 20 } 21 }; 22 await AsyncTestUtil.assertThrows( 23 () => pingServer(), // function that throws (required) 24 Error, // Error-type (optional) 25 'reachable' // text-flag, throw if error-message does not include this (optional) 26 ); 27 28 // or you can pass a string-array to ensure all is in error message 29 await AsyncTestUtil.assertThrows( 30 () => pingServer(), // function that throws (required) 31 Error, // Error-type (optional) 32 ['route', 'reachable'] // text-flag, throw if error-message does not include this (optional) 33 ); 34}); 35 36// assertThrows returns the error 37it('should have the custom error-property', async() => { 38 const throwingFunction = async()=>{ 39 const error = new Error('error message'); 40 error.foo = 'bar'; 41 throw error; 42 } 43 const thrown = await AsyncTestUtil.assertThrows( 44 () => pingServer(), // function that throws (required) 45 Error, // Error-type (optional) 46 'message' // text-flag, throw if error-message does not include this (optional) 47 ); 48 assert.equal(thrown.foo, 'bar'); 49});
Recieves an object with promises as values. Returns ans object with the resolved promises as values. Use this in test-setups to improve the test-speed by running everything in parallel.
1// instead of this 2const database = await connectDatabase(); 3const user1 = await getUser(); 4const user2 = await getUser(); 5 6// do this 7const { 8 database, 9 user1, 10 user2 11} = await AsyncTestUtil.resolveValues({ 12 database: connectDatabase(); 13 user1: getUser(); 14 user2: getUser(); 15});
Returns true if the given value is a Promise
;
1const is = AsyncTestUtil.isPromise(myAsyncFunction()); // true 2const is = AsyncTestUtil.isPromise('foobar'); // false
Transforms the given value to a promise if it was no promise before.
1const ensurePromise = AsyncTestUtil.promisify(maybeAsyncFunction()); 2 3// now you are sure this is a promise 4ensurePromise.then(/* ... */)
Creates a random string. Takes length as first parameter an custom charset as second.
1console.log(AsyncTestUtil.randomString()); 2// > 'rhfkx' 3 4console.log(AsyncTestUtil.randomString(10)); 5// > 'dhcvkledzu' 6 7console.log(AsyncTestUtil.randomString( 8 6, // (optional) length 9 'abc' // (optional) charset, only this will be used to create the string 10)); 11// > 'acbcba'
Creates a random number. Optional range can be given.
1console.log(AsyncTestUtil.randomNumber()); 2// > 56 3 4console.log(AsyncTestUtil.randomNumber( 5 1000, // min-value (default=0) 6 2000 // max-value (default=1000) 7)); 8// > 1768
Creates a random boolean. Returns true
or false
.
1console.log(AsyncTestUtil.randomBoolean()); 2// > true
Reference to clone. Does exactly the same thing.
1it('should not modify the original object', () => { 2 const original = { 3 foo: 'bar', 4 level: 1 5 }; 6 const cloned = AsyncTestUtil.clone(original); 7 cloned.level = 2; 8 assert.equal(original.level, 1); 9});
Reference to deep-equal. Does exactly the same thing.
1it('the 2 objects should be equal', () => { 2 const obj1 = { 3 foo: 'bar', 4 level: 1 5 }; 6 const obj2 = { 7 foo: 'bar', 8 level: 1 9 }; 10 assert.ok(AsyncTestUtil.deepEqual(obj1, obj2)); 11});
Works equal to performance.now but works in browsers and nodeJs.
1it('should take less then 200 milliseconds', () => { 2 const now = AsyncTestUtil.performanceNow(); // in milliseconds 3 await doSomething(); 4 const later = AsyncTestUtil.performanceNow(); // in milliseconds 5 assert.ok((now + 50 * 1000 ) > later); 6});
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
17 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
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
Score
Last Scanned on 2025-05-05
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