Promise 'sleep' not working in Jasmine test

Viewed 27

I am trying to test a setInterval call in a component and I have been having a bunch of problems. My current thoughts is to sleep for a second, but within the Jasmine test it seems to be running forever; therefore, failing with the following:

Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

The simplest test is as follows:

it( 'should await synchronously ...', async () => {
  const startTime: number = Date.now();
  await new Promise(fn => setTimeout(fn, 1000));
  expect( Date.now() - startTime ).toBeGreaterThan( 999 );
} );

It's a mystery why the 'sleep' isn't working inside of a Jasmine test.

0 Answers
Related