最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to ignore .catch promise in code coverage with istanbul? - Stack Overflow

programmeradmin0浏览0评论

I'm using BluebirdJS promise library and I have .catch that I can't mock that is why I can't cover it with my code coverage using istanbul.

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

Is this possible with istanbul library?

This is the full code, my test can't reach the .catch because it's always passing and I can't seem to another way to force mongoose to throw an error

 const { payload } = request

 const group = new LocationGroups(payload)

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

I'm using BluebirdJS promise library and I have .catch that I can't mock that is why I can't cover it with my code coverage using istanbul.

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

Is this possible with istanbul library?

This is the full code, my test can't reach the .catch because it's always passing and I can't seem to another way to force mongoose to throw an error

 const { payload } = request

 const group = new LocationGroups(payload)

 group.save()
   .then(reply)
   .catch((error) => reply(boomify(error)))
Share Improve this question edited Mar 24, 2023 at 11:18 Dharman 33.5k27 gold badges101 silver badges148 bronze badges asked Sep 18, 2017 at 0:14 kdlcruzkdlcruz 1,38813 silver badges20 bronze badges 5
  • Try making it testable? Why do you think you can't mock it? – Bergi Commented Sep 18, 2017 at 0:53
  • How about putting the ment inside the callback that should be ignored? – Bergi Commented Sep 18, 2017 at 0:53
  • @Bergi Please see my edit. I'm not sure how to force mongoose to force an error with .save – kdlcruz Commented Sep 18, 2017 at 1:04
  • @Bergi putting the ment inside won't work because I can't reach it. Thanks btw, for the ment :) – kdlcruz Commented Sep 18, 2017 at 1:07
  • You can't force mongoose to do anything, but you could just mock save?What libraries do you use for testing? – Bergi Commented Sep 18, 2017 at 1:08
Add a ment  | 

2 Answers 2

Reset to default 6
try {

} catch (err) /* istanbul ignore next */ {

}

In your case

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

Alternatively, you can wrap your catch in another block

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

发布评论

评论列表(0)

  1. 暂无评论