【发布时间】:2018-11-21 19:10:25
【问题描述】:
我有一组使用 Mocha 和 jsdom 运行的 Web 应用程序的单元测试。我正在使用 jsdom 而不是真正的浏览器,以便让我的 mocha 单元测试尽可能快地运行,并且我们将进行运行 selenium 的集成测试,以测试更多真正的浏览器内容。我也在使用 istanbul 来生成代码覆盖率报告,但是有些代码无法使用 Mocha/jsdom 进行测试。以这段代码为例:
var myObject = {
//...
/* istanbul ignore next */
_resizeEvent: function() {
if(this.props.isActive) {
//.9 match the scss max-height: 90%, this value needs to be kept in sync with the sass code
this.getDOMNode().querySelector('.modal__content').style.maxHeight = Math.floor(window.innerHeight * .9) + 'px';
this.getDOMNode().querySelector('.modal__content').style.maxWidth = Math.floor(window.innerWidth * .9) + 'px';
this._centerPosition();
}
},
//...
}
由于没有真正的浏览器,这个方法不能真正用jsdom测试。我遇到的问题是,即使我在对象方法之前有/* istanbul ignore next */,它仍然告诉我方法内的代码没有被覆盖(而不是只是告诉我它被忽略了)。
有没有一种简单的方法可以完全忽略对象方法及其所有内容,而不必在方法中的每个语句之前添加/* istanbul ignore next */?
【问题讨论】:
标签: javascript unit-testing istanbul