Jest: useFakeTimers() with async/await caused Timeout error

Created on 21 Nov 2017  Â·  6Comments  Â·  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?

bug(or my misunderstanding?)

What is the current behavior?

This code ends up with Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

jest.useFakeTimers();

test('why timeout?', async () => {
  // If you comment out this line, test passes
  await new Promise(r => setTimeout(r, 1000));
  expect(1).toBe(1);

  jest.runTimersToTime(3000);
});

Do I misunderstand something?
Thanks in advance.

What is the expected behavior?

Expect test to be passed with 1000ms delay.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

  • Jest v21.2.1
  • node v8.1.4
  • npm v5.5.1
  • macOS Siera 10.12.6

Most helpful comment

Yeah these two don't work together as @lukeapage identified in the latest comment. I'm closing this as wontfix but you can try doing something like:

const promise = startPromiseTask();
jest.runAllTimers();
const result = await promise;

All 6 comments

I'm not 100% sure but i think you Either want to write something like

done => {
 setTimeout(() => done(), 1000);
}

or

async () => {
  result =  await someOtherPromise;
  expect(await).toBe(...);
}

or without the es6 sugar the last one could be rewritten as

() => {
  return someOtherPromise.then((result) => {
    expect(result).toBe(...);
  });
}

so EITHER promises OR done... not both.

That said I'm having a bad time with async tests right now and that's why I'm here, so please someone with more knowledge feel free to correct me.

Thanks for the comment.
But I need to use BOTH done for callback and await for promises.

Because some libraries only provide callback style API, then this situation happens.

Use promise and new Promise(resolve => callbackneedingfunction(resolve))

On Tue, Nov 28, 2017, 8:55 PM Yuji Sugiura notifications@github.com wrote:

Thanks for the comment.
But I need to use BOTH done for callback and await for promises.

Because some libraries only provide callback style API, then this
situation happens.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/facebook/jest/issues/4928#issuecomment-347726893, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAQpRZiMyqD88sZsV8rtMIi0VZvyDW5oks5s7LmRgaJpZM4Qlf7I
.

Of course I know that.

But in this issue, the point I want to know, is why the code above does not work.
Is this limitation of Jest or some mistakes there.

(I updated repro codes more clearly

Because it never gets to runTimersToTime
Because it’s awaiting the promise that only gets resolved when time goes forward, which doesn’t happen till you call runTimersToTime.

Yeah these two don't work together as @lukeapage identified in the latest comment. I'm closing this as wontfix but you can try doing something like:

const promise = startPromiseTask();
jest.runAllTimers();
const result = await promise;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Secretmapper picture Secretmapper  Â·  3Comments

Antho2407 picture Antho2407  Â·  3Comments

ticky picture ticky  Â·  3Comments

StephanBijzitter picture StephanBijzitter  Â·  3Comments

stephenlautier picture stephenlautier  Â·  3Comments