首页 文章

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

提问于
浏览
1

由于我们已切换到生成器,因此无法找到支持此工具的覆盖工具 .

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

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

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

2 回答

  • 0

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

    但是,我无法正确使用此工具:我有错误,当使用 class 时 .

  • 0

    一段时间后,co-mocha很适合我 . 后来在一个新的代码库中,它没有正确地处理承诺,并且's probably related to node 5; I'已经记录了ticket .

    由于承诺处理正常,理论上你不需要monkeypatch mocha来使用生成器 . 简单地使用 co.wrap() 就像这样应该工作:

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

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

    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();    
    }));
    

相关问题