async-test-util
Utility-functions that can be usefull when you have asynchronous tests in javascript.
Installation
$ npm install async-test-util --save-dev
import AsyncTestUtil from 'async-test-util';
var AsyncTestUtil = require('async-test-util');
wait()
Waits until the given time has expired and then resolves.
it('should wait', async() => {
await AsyncTestUtil.wait(200);
console.log('200 ms is over');
});
waitResolveable()
Waits until the given timeout has expired or the resolve was triggered manually
it('should wait until observable fired or time is over', async() => {
const resolveable = AsyncTestUtil.waitResolveable(200);
myObservable.subscribe(sth => {
console.log('got first event');
resolveable.resolve();
});
await resolveable.promise;
});
waitUntil()
Waits until the given predicate-function returns true. Throws if the optional timeout has passed before.
it('should wait until server is online', async() => {
const checkServer = async() => {
try{
await fetch('http://example.com/api/');
return true;
}catch(){
return false;
}
};
await AsyncTestUtil.waitUntil(checkServer);
});
With timeout:
it('should wait until server is online (maxtime: 1000ms)', async() => {
const checkServer = async() => {
try{
await fetch('http://example.com/api/');
return true;
}catch(){
return false;
}
};
await AsyncTestUtil.waitUntil(checkServer, 1000);
});
waitForever()
Waits forever, never resolves.
it('should never resolve', async() => {
let resolved = false;
AsyncTestUtil
.waitForever()
.then(() => resolved = true);
await AsyncTestUtil.wait(100);
assert.equal(false, resolved);
});
runForever()
Runs the given predicate-function forever. Between each run, the interval-time is awaited.
it('should run forever', async() => {
let t = 0;
const pred = () => t++;
AsyncTestUtil.runForever(
pred,
10
);
await AsyncTestUtil.wait(100);
assert.ok(t > 4);
const lastT = t;
await AsyncTestUtil.wait(100);
assert.ok(t > lastT);
});
assertThrows()
Async-Form of assert.throws. Asserts that the given function throws with the defined error, throws if not.
it('should throw because route does not exist', async() => {
const getServerVersion = async() => {
const response = await fetch('http://example.com/foobar/');
return response;
};
await AsyncTestUtil.assertThrows(
() => getServerVersion(),
Error
);
});
it('should throw because route does not exist', async() => {
const pingServer = async() => {
try{
await fetch('http://example.com/foobar/');
}catch(err){
throw new Error('route not reachable');
}
};
await AsyncTestUtil.assertThrows(
() => pingServer(),
Error,
'reachable'
);
});
it('should have the custom error-property', async() => {
const throwingFunction = async()=>{
const error = new Error('error message');
error.foo = 'bar';
throw error;
}
const thrown = await AsyncTestUtil.assertThrows(
() => pingServer(),
Error,
'message'
);
assert.equal(thrown.foo, 'bar');
});
resolveValues()
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.
const database = await connectDatabase();
const user1 = await getUser();
const user2 = await getUser();
const {
database,
user1,
user2
} = await AsyncTestUtil.resolveValues({
database: connectDatabase();
user1: getUser();
user2: getUser();
});
isPromise()
Returns true if the given value is a Promise
;
const is = AsyncTestUtil.isPromise(myAsyncFunction());
const is = AsyncTestUtil.isPromise('foobar');
promisify
Transforms the given value to a promise if it was no promise before.
const ensurePromise = AsyncTestUtil.isPromise(maybeAsyncFunction());
ensurePromise.then()
randomString()
Creates a random string. Takes length as first parameter an custom charset as second.
console.log(AsyncTestUtil.randomString());
console.log(AsyncTestUtil.randomString(10));
console.log(AsyncTestUtil.randomString(
6,
'abc'
));
randomNumber()
Creates a random number. Optional range can be given.
console.log(AsyncTestUtil.randomNumber());
console.log(AsyncTestUtil.randomNumber(
1000,
2000
));
randomBoolean()
Creates a random boolean. Returns true
or false
.
console.log(AsyncTestUtil.randomBoolean());
clone()
Reference to clone. Does exactly the same thing.
it('should not modify the original object', () => {
const original = {
foo: 'bar',
level: 1
};
const cloned = AsyncTestUtil.clone(original);
cloned.level = 2;
assert.equal(original.level, 1);
});
deepEqual()
Reference to deep-equal. Does exactly the same thing.
it('the 2 objects should be equal', () => {
const obj1 = {
foo: 'bar',
level: 1
};
const obj2 = {
foo: 'bar',
level: 1
};
assert.ok(AsyncTestUtil.deepEqual(obj1, obj2));
});
performanceNow()
Works equal to performance.now but works in browsers and nodeJs.
it('should take less then 200 milliseconds', () => {
const now = AsyncTestUtil.performanceNow();
await doSomething();
const later = AsyncTestUtil.performanceNow();
assert.ok((now + 50 * 1000 ) > later);
});