用 Jest 和 Enzyme 覆盖空操作函数

Cover no-op functions with Jest and Enzyme

在我的 React 组件中,我有一些无操作函数,它们基本上只是函数的占位符。我有伊斯坦布尔测试覆盖率,它抱怨无操作功能覆盖率:

有什么方法可以遮盖吗?

要使 Instanbul 忽略 noop 函数,我们可以使用 /* istanbul ignore next */ 提到的 here

对于您的报道示例,您可以做类似的事情

const defaultContext = {
  activeInput: null,
  removeFocus: /* istanbul ignore next */ () => {},
  focusInput: /* istanbul ignore next */ () => {},
}

或者您可以定义一个独立的 noop 函数并在需要时重用

/* istanbul ignore next */
function noop() {}

const defaultContext = {
  activeInput: null,
  removeFocus: noop,
  focusInput: noop,
}