Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put time limit on Jest test?

I'm running a set of tests with Jest to demonstrate Big O using two different methods for Fibonacci.

const fastFib = require('./../fastFib');
const slowFib = require('./../slowFib');

test('Fast way of getting Fibonacci of 44', () => {
  expect(fastFib(44)).toBe(701408733);
});

test('Slow way of getting Fibonacci of 44', () => {
  expect(slowFib(44)).toBe(701408733);
});

I'm wondering if there is a way to specify the maximum length of a test? I saw you can pass a third variable for an async timeout but it doesn't seem to have any effect on normal functions:

test('Slow way of getting Fibonacci of 44', () => {
  expect(slowFib(44)).toBe(701408733);
}, 5000);

Is there a way I can specify the maximum execution time for a function with Jest?


I will share slowFib.js for reference:

function fib(n) {
  return (n<=1) ? n : fib(n - 1) + fib(n - 2);
}

module.exports = fib;
like image 635
Philip Kirkbride Avatar asked May 28 '19 14:05

Philip Kirkbride


People also ask

How do you increase timeout in Jest test?

Use jest. setTimeout(newTimeout) to increase the timeout value, if this is a long-running test." 1)Increased jest. setTimeout(30000) from 30000 to 60000.

Can I use setTimeout in Jest?

The native timer functions (i.e., setTimeout() , setInterval() , clearTimeout() , clearInterval() ) are less than ideal for a testing environment since they depend on real time to elapse. Jest can swap out timers with functions that allow you to control the passage of time.

How does Jest timeout work?

Note: The default timeout is 5 seconds. Note: If a promise is returned from test , Jest will wait for the promise to resolve before letting the test complete. Jest will also wait if you provide an argument to the test function, usually called done . This could be handy when you want to test callbacks.

How do you mock time in Jest?

To use the new mock system, you need to pass the "modern" argument to the jest. useFakeTimers function. This system will allow you not only to mock timers as you already could but also to mock the system clock. This way, the test will be green, but will also be stable in time.


2 Answers

so your test pauses because of sync execution - there is no way to interrupt that by timeout. You need to "split execution". Next version fails to me:

test('Slow way of getting Fibonacci of 44', (done) => {
  expect(slowFib(44)).toBe(701408733);
  setTimeout(done, 10); // smallest timeout possible to make different macrotask
}, 5000);

PS I also believe this should be achievable by marking test async but have not yet figured how exactly.

[UPD] you actually may achieve your goal without using test's timeout:

test('Slow way of getting Fibonacci of 44', () => {
  const start = new Date();
  expect(slowFib(44)).toBe(701408733);
  expect(new Date() - start).toBeLessThan(5000);
});
like image 128
skyboyer Avatar answered Oct 23 '22 16:10

skyboyer


In your test file you can set jest.setTimeout(5000); which overrides the default timeout for a test case in jest

like image 1
Khaled Osman Avatar answered Oct 23 '22 16:10

Khaled Osman