【问题标题】:How to ignore .catch promise in code coverage with istanbul?如何在伊斯坦布尔的代码覆盖率中忽略 .catch 承诺?
【发布时间】:2017-09-18 00:14:50
【问题描述】:

我正在使用 BluebirdJS 承诺库,我有 .catch 我无法模拟这就是为什么我不能使用伊斯坦布尔的代码覆盖率来覆盖它。

.then(() => ....)
/* istanbul ignore next */ -> Does not work
.catch((err) => err) /// I want to ignore this

有人知道伊斯坦布尔图书馆是否可以做到这一点?

谢谢!

编辑:这是完整的代码,我的测试无法到达 .catch,因为它总是通过,我似乎无法以另一种方式强制 mongoose 抛出错误

 const { payload } = request

 const group = new LocationGroups(payload)

 group.save()
   .then(reply)
   .catch((error) => reply(boomify(error)))

【问题讨论】:

  • 尝试让它可测试?为什么你认为你不能嘲笑它?
  • 将注释放在应该被忽略的回调中怎么样?
  • @Bergi 请看我的编辑。我不确定如何强制猫鼬使用.save 强制出错
  • @Bergi 将评论放入其中不起作用,因为我无法访问它。顺便说一句,感谢您的评论:)
  • 你不能强迫猫鼬做任何事情,但你可以模拟save?你用什么库来测试?

标签: javascript promise code-coverage istanbul


【解决方案1】:

你的情况

group.save()
   .then(reply)
   .catch(  /* istanbul ignore next */(error) => reply(boomify(error)))

或者,您可以将捕获的内容包裹在另一个块中

 try {
    stuff()
  } catch (err) {
    /* istanbul ignore next */ {
      console.error(err)
      throw err
    }
  }

【讨论】:

    【解决方案2】:
    try {
    
    } catch (err) /* istanbul ignore next */ {
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2015-08-26
      • 2019-04-28
      • 2015-07-29
      • 2015-09-10
      • 2016-07-22
      相关资源
      最近更新 更多