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'
);
});
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
));
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));
});