javascript - 单元测试中的去抖函数 'not a function'

标签 javascript unit-testing jestjs lodash enzyme

我正在尝试模拟去抖动,以便可以在单元测试中测试去抖动函数,但它告诉我该函数不是函数

错误:

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/

相关文章:

javascript - 为 ExtJs 中的多个 Controller 绑定(bind)事件

c# - 有没有办法告诉 Autofixture 只设置具有特定属性的属性?

unit-testing - VueJS-开 Jest : How to test/spy the methods which are getting called inside the watcher?

jestjs - 如何配置 jest 在超过 x 秒时失败

javascript - 带有 React 的 Google map - "Required props loadingElement or googleMapURL is missing. You need to provide both of them"

c# - 页面渲染和 http 处理程序

javascript - 如何将 javascript 函数延迟到 Jquery 和 Facebook 都加载之后?

python - 如何仅在内存中运行 Django 测试数据库?

unit-testing - 如何为一小时后函数返回时间编写单元测试

javascript - 如何测试 ErrorBoundary 的后备组件?