3

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?

Max Vinogradov
  • 1,343
  • 1
  • 13
  • 31

2 Answers2

3

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.

Mark Wubben
  • 3,329
  • 1
  • 19
  • 16
  • Thanks, I've seen this issue before, but I was hoping that someone knows how to do this, knows any workaround or/and extension points how we can add this custom functionality. – Max Vinogradov Dec 06 '17 at 07:32
  • 1
    @MaxVinogradov, there are workarounds with sample code in the linked issue. – Motin Oct 10 '18 at 09:28
2

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);
}));
sdgluck
  • 24,894
  • 8
  • 75
  • 90
  • Thanks a lot, I liked your approach to solving the problem and now I use it in my project - it allowed me to reduce the number of code and make it more readable. – Max Vinogradov Dec 06 '17 at 07:33