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

_.debounce() breaks fake timers #2893

Closed
aldendaniels opened this issue Dec 19, 2016 · 9 comments
Closed

_.debounce() breaks fake timers #2893

aldendaniels opened this issue Dec 19, 2016 · 9 comments

Comments

@aldendaniels
Copy link

aldendaniels commented Dec 19, 2016

_.debounce() breaks fake timers

In tests, it's common to replace JS timer methods such as setTimeout and setInterval with fake methods in order to control the passage of time. See Jest Timer Mocks and Sinon Fake Timers. Doing this allows you to march the clock forward in tests, executing all timers synchronously.

Recursive calls to setTimeout pose a problem when doing this, because the recursive setTimeout calls are executed synchronously, causing infinite recursion.

@jdalton
Copy link
Member

jdalton commented Dec 19, 2016

The latest v4 should no longer be breaking fake timers.

Dup of #2533.

@rimunroe
Copy link

rimunroe commented May 3, 2017

I'm still seeing the same issue with it in v4.17.4 of Lodash. I tried it with Jest 18 & 19 on Node 4 & 6.

jest.useFakeTimers();

var _ = require('lodash');

test('fails to mock Lodash timers correctly', function() {
  var test = jest.fn();
  var debounced = _.debounce(test, 1000);

  debounced();
  debounced();

  jest.runAllTimers();

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

Running that with Jest causes it to say "Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out...". Do you think this is a Lodash issue, or is this something I should pursue on Jest's end?

@jdalton
Copy link
Member

jdalton commented May 3, 2017

Lodash doesn't hold on to references to setTimeout, clearTimeout, or Date.now so your guess is as good as mine.

@jkettmann
Copy link

@rimunroe Did you solve your issue somehow? Having a similar problem

@rimunroe
Copy link

I ended up just avoiding using it, or actually waiting in tests when I absolutely had to. I opened jestjs/jest#3465 about the problem though.

@NickHeiner
Copy link

I believe I know what the problem with Jest is: jestjs/jest#3465 (comment).

@skyboyer
Copy link

@jdalton so what could we(library consumers) do here? do we need mocking Date.now alongside using fake timers to mock _.debounce? should it help?

@jdalton
Copy link
Member

jdalton commented Dec 29, 2018

@skyboyer I'd go for trying to mock _.debounce.

@lock
Copy link

lock bot commented Dec 29, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

6 participants