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.

[Bug]: useFakeTimers on setTimeout within a promise

See original GitHub issue

Version

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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sigveiocommented, Aug 31, 2021
jest.spyOn(global, 'setTimeout');

Note that this has to come after jest.useFakeTimers()

0reactions
github-actions[bot]commented, Oct 1, 2021

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

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 >

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