【问题标题】:debounced function 'not a function' in unit test单元测试中的去抖函数“不是函数”
【发布时间】:2019-11-07 21:05:20
【问题描述】:

我正在尝试模拟 debounce,以便我可以在单元测试中测试 debounce 函数,但它告诉我函数 is not a function

错误:

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');
    });

【问题讨论】:

  • 如果你导出你的非去抖动函数,你就不必模拟任何东西了。

标签: javascript unit-testing jestjs lodash enzyme


【解决方案1】:

结果我的 debounce 模拟只是通过触摸工作代码关闭:

import debounce from 'lodash/debounce';
jest.mock('lodash/debounce', () => jest.fn(fn => fn));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 2016-04-02
    • 1970-01-01
    • 2019-11-20
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多