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.
{
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(ts|tsx)$": "babel-jest"
},
"testMatch": [
"**/__tests__/*.(ts|tsx|js)"
],
"setupFiles": [
"<rootDir>/jestEnvironment.js"
]
}
You have to enable fake timers before requiring lodash or using setTimeout, otherwise fake timers will not be used.
This still does not work, see the updated repl:
I can confirm it doesn't work, might be better tracked in #3465, though
Yup this is a dupe. It won鈥檛 work because lodash doesn鈥檛 use timers to schedule denounce and throttle
Interestingly, using sinon's fake timers to do the same thing works.
Most helpful comment
Interestingly, using sinon's fake timers to do the same thing works.