question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consistently called async functions do not work well with fake timers

See original GitHub issue

🐛 Bug Report

Consistently called async functions do not work well with fake timers.

To Reproduce

beforeEach(() => {
  jest.useFakeTimers();
});

const fn = async () => {
  console.log(1);
  await new Promise(resolve => setTimeout(() => resolve(), 10));
  console.log(2);
  await new Promise(resolve => setTimeout(() => resolve(), 10));
  console.log(3);
};

it('test', (done) => {
  fn().then(() => done());
  console.log(4);
  jest.advanceTimersByTime(10);
  console.log(5);
  jest.advanceTimersByTime(10);
});

Here is a result of execution:

yarn run v1.7.0
$ jest --env=node --verbose --resetMocks --resetModules --testMatch=**\\test.test.js
  console.log tests/unit/test.test.js:6
    1

  console.log tests/unit/test.test.js:15
    4

  console.log tests/unit/test.test.js:17
    5

  console.log tests/unit/test.test.js:8
    2

 FAIL  tests/unit/test.test.js (6.749s)
  × test (5052ms)

  ● test

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

Expected behavior

The second promise should be resolved after the second timer advance. Also both of them should be resolved if I call the advanceTimersByTime function once with 20 ms param.

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: Windows 10
    CPU: x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
  Binaries:
    Yarn: 1.7.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 5.4.2 - C:\Program Files\nodejs\npm.CMD

Jest version: 23.5.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

1reaction
BrendanBallcommented, Jun 19, 2020

this is still an issue and a workaround is discussed on stackoverflow. I think it would still be great if jest can support this usecase with fake timers

0reactions
github-actions[bot]commented, May 1, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest: Timer and Promise don't work well. (setTimeout and ...
async always returns a Promise and await essentially wraps the rest of the function in a then callback attached to the Promise it...
Read more >
How to test async functions with fake timers - Sinon.JS
With fake timers (lolex), testing code that depends on timers is easier, as it sometimes becomes possible to skip the waiting part and...
Read more >
Getting Started With Async Features in Python
This step-by-step tutorial gives you the tools you need to start making asynchronous programming techniques a part of your repertoire.
Read more >
Python behind the scenes #12: how async/await works in Python
Mark functions as async. Call them with await. All of a sudden, your program becomes asynchronous – it can do useful things while...
Read more >
Handling those unhandled promise rejections with JS async ...
The Problem · You put your code inside an async function in order to use await calls · You put an await call...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found