带有生成器的mocha的代码覆盖率

时间:2015-01-23 08:38:10

标签: node.js automated-tests generator code-coverage mocha

由于我们已切换到发电机,我无法找到支持此功能的覆盖工具。

我们在代码和mocha测试中都使用生成器。

我们使用co-mocha在mocha测试中启用了生成器。

我唯一想到的选择是转换测试,而不是在和声模式下运行它们。

2 个答案:

答案 0 :(得分:0)

Unit-coverage有基本的和谐支持。 详情here

但是,我无法正确使用此工具:使用class时出错。

答案 1 :(得分:0)

一段时间后,co-mocha很适合我。后来在一个新的代码库中它没有。经过一番挖掘后,我发现mocha不再适当地处理承诺了,它可能与节点5有关;我已经记录了ticket

由于承诺处理正常,理论上你不需要monkeypatch mocha来使用生成器。只需使用co.wrap()就可以了:

it('should yield in the generator', co.wrap(function*() {
   yield aPromiseReturningFunction();
   yield aGeneratorFunction();    
}));

与此同时,我编写了一个lil实用程序函数,它使用co.wrap()包装生成器,并将done作为thencatch处理程序传递给承诺它返回:

function done(gen) {
  const wrapper = co.wrap(gen); 
  return function(done) {
    return wrapper.call(this, done).then(done).catch(done);
  };
}

然后我这样做:

it('should yield in the generator', done(function*() {
   yield aPromiseReturningFunction();
   yield aGeneratorFunction();    
}));
相关问题