解决办法 一,使用jest.mock
jest.mock('lodash/debounce', () => jest.fn(fn => fn));
解决办法 二, 使用jest.useFakeTimers(), 然后在调用 debounced之后运行 jest.runAllTimers();
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);
});
参考链接:
https://github.com/facebook/jest/issues/3465
https://github.com/rimunroe/lodash-jest-timer-issue/blob/master/tests/lodash-bug-test.js
网友评论