I writing asynchronous tests using AVA, and need to setup custom timeout for each test cases. I've not found out any information about this possibility and my tests seems like this:
import test from 'ava';
test.cb('super test', t => {
setTimeout(() => {
t.is(1, 1);
t.end();
}, 10000);
setTimeout(() => {
t.fail("Timeout error!");
t.end();
}, 100);
});
Does anybody know another way to implement this in AVA?
There's an open issue to support this in AVA itself: https://github.com/avajs/ava/issues/1565
Until that lands you'll have to manage a timer yourself. Don't forget to clear it once your normal test completes.
I don't know if AVA has something like this built in. I suspect not, as it seems like quite an unusual use-case.
But you could create a utility function that implements some kind of a "timeout test":
import test from 'ava';
function timeout (ms, fn) {
return function (t) {
setTimeout(() => {
t.fail("Timeout error!")
t.end()
}, ms)
fn(t)
}
}
test.cb('super test', timeout(10000, t => {
t.is(1, 1);
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With