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:
- Created 5 years ago
- Reactions:5
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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.