Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVA: setup different timeout for each test case

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?

like image 948
Max Vinogradov Avatar asked Nov 25 '25 09:11

Max Vinogradov


2 Answers

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.

like image 82
Mark Wubben Avatar answered Nov 27 '25 22:11

Mark Wubben


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);
}));
like image 30
sdgluck Avatar answered Nov 27 '25 22:11

sdgluck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!