【发布时间】:2019-03-27 06:41:27
【问题描述】:
我有一个rateLimit 函数(它只是this code 的修改版本):
function rateLimit(func, wait) {
var timeout;
return function () {
var context = this;
var args = arguments;
var later = function () {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
这个功能在我的应用程序中运行良好,所以我相当确定实现很好。但是,以下测试失败:
jest.useFakeTimers();
test('rateLimit', () => {
const action = jest.fn();
const doAction = rateLimit(action, 100);
doAction(); // This should increment the call count
doAction(); // This shouldn't, because 100ms hasn't elapsed yet
jest.advanceTimersByTime(101);
doAction(); // This should increment the count again
expect(action).toHaveBeenCalledTimes(2);
});
出现错误:
Expected mock function to have been called two times, but it was called one time.
Here is a runnable version of this code on repl.it.
我在这里做错了什么?
【问题讨论】:
标签: javascript unit-testing jestjs