Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runTimersToTime does not execute a function that is debounced using lodash's debounce. #5160

Closed
cameron-martin opened this issue Dec 22, 2017 · 6 comments

Comments

@cameron-martin
Copy link

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Advancing jest timers does not call a function which is debounced using lodash's debounce function.

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

Execute the following test, and it should pass but at the moment it does not.

var debounce = require('lodash/debounce');

jest.useFakeTimers();

test('foo', function() {
    const mock = jest.fn();

    const fn = debounce(mock, 1000);
    fn();

    jest.runTimersToTime(1200);

    expect(mock).toHaveBeenCalledTimes(1);
});

https://repl.it/repls/BlushingGrayGharial

What is the expected behavior?

The debounced function should be called.

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

Jest configuration

  {
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "^.+\\.(ts|tsx)$": "babel-jest"
    },
    "testMatch": [
      "**/__tests__/*.(ts|tsx|js)"
    ],
    "setupFiles": [
      "<rootDir>/jestEnvironment.js"
    ]
  }

Versions

  • npm - 5.3.0
  • node - v8.6.0
  • jest - v22.0.3
  • OS - Windows 10
@cpojer
Copy link
Member

cpojer commented Dec 22, 2017

You have to enable fake timers before requiring lodash or using setTimeout, otherwise fake timers will not be used.

@cpojer cpojer closed this as completed Dec 22, 2017
@cameron-martin
Copy link
Author

This still does not work, see the updated repl:

https://repl.it/repls/BlushingGrayGharial

@SimenB
Copy link
Member

SimenB commented Dec 22, 2017

I can confirm it doesn't work, might be better tracked in #3465, though

@thymikee
Copy link
Collaborator

Yup this is a dupe. It won’t work because lodash doesn’t use timers to schedule denounce and throttle

@thymikee thymikee reopened this Dec 22, 2017
@cameron-martin
Copy link
Author

Interestingly, using sinon's fake timers to do the same thing works.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants