[Bug]: useFakeTimers on setTimeout within a promise
See original GitHub issueVersion
27.1.0
Steps to reproduce
test('should let me test sleep', () => {
jest.useFakeTimers();
const expectedTimeout = 4;
const sleep = (seconds) =>
new Promise((resolve) => setTimeout(resolve, seconds * 1000));
sleep(expectedTimeout);
expect(setTimeout)
.toHaveBeenCalledTimes(1)
.toHaveBeenCalledWith(expect.any(Function), expectedTimeout * 1000);
});
Test passes on jest 26, but fails on 27 unless legacy
is passed.
Receive this error instead.
Cannot read property 'message' of undefined
TypeError: Cannot read property 'message' of undefined
Based on the documentation, I expected setTimeout to be a mock object as it was previously. Thank you for taking a look!
Expected behavior
The test should pass.
Actual behavior
Exception from reproduction steps occurs.
Additional context
No response
Environment
System:
OS: macOS 11.5.2
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Binaries:
Node: 12.22.5 - ~/.nvm/versions/node/v12.22.5/bin/node
Yarn: 1.22.11 - ~/.nvm/versions/node/v12.22.5/bin/yarn
npm: 6.14.14 - ~/.nvm/versions/node/v12.22.5/bin/npm
npmPackages:
jest: 27.1.0 => 27.1.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
useFakeTimer breaks setTimeout in Promise #10880 - GitHub
Bug Report When using the syntax await new Promise(r => setTimeout(r, 2000)), when jest.useFakeTimers() is called, the Promise never ...
Read more >Even after using jest.useFakeTimers getting timeout error for ...
I have created below function for polling export const wait = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ...
Read more >Fake timers - Sinon.JS
Fake timers are synchronous implementations of setTimeout and friends that Sinon.JS can overwrite the global functions with to allow you to more easily...
Read more >p-timeout - npm
Timeout a promise after a specified amount of time. ... Start using p-timeout in your project by running `npm i ... Type: string...
Read more >Mocking setTimeout with Jest. | Medium - Marek Rozmus
Use fake timers ... error. not wrapped in act(…) This is because the setTimeout callback is ... Since version 14.0.0 the APIs always...
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
Note that this has to come after
jest.useFakeTimers()
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.