我正在尝试模拟去抖动,以便可以在单元测试中测试去抖动函数,但它告诉我该函数不是函数
错误:
TypeError: (0 , _usersDialog.debounceUpdateSearchText) is not a function
功能:
export const debounceUpdateSearchText = debounce(
(updateText, searchString) => {
if (searchString === '' || searchString.length === 1) {
updateText(' ');
}
updateText(searchString);
},
500
);
测试代码:
// earlier in the file
import debounce from 'lodash/debounce';
jest.mock('lodash/debounce');
// test
it('updates the search text', () => {
// jest.useFakeTimers();
debounce.mockImplementation(fn => fn);
const updateText = jest.fn();
// call function
debounceUpdateSearchText(updateText, 'fuego');
// jest.advanceTimersByTime(501);
expect(props.updateText).toHaveBeenCalledWith('fuego');
});
最佳答案
事实证明,我的去抖模拟只需轻轻一触即可关闭,工作代码:
import debounce from 'lodash/debounce';
jest.mock('lodash/debounce', () => jest.fn(fn => fn));
关于javascript - 单元测试中的去抖函数 'not a function',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58756903/