简体   繁体   English

使用Q promise的node.js项目的代码覆盖率

[英]Code coverage for node.js project using Q promises

I'm currently working on a node.js project. 我目前正在开发一个node.js项目。 We use Q library for promises ( https://github.com/kriskowal/q ). 我们使用Q库进行承诺( https://github.com/kriskowal/q )。

We are using mocha for tests and code-coverage provided with grunt tasks ( https://github.com/pghalliday/grunt-mocha-test ) which uses blanket.js for coverage and travis-cov reporter for asserting the coverage threshold. 我们使用mocha进行测试和代码覆盖,其中包含grunt任务( https://github.com/pghalliday/grunt-mocha-test ),它使用blanket.js进行覆盖,使用travis-cov报告来断言覆盖阈值。

Unfortunately the solution does not provide branches coverage for promises. 不幸的是,该解决方案不为承诺提供分支机构覆盖。

I have tried Intern ( http://theintern.io/ ), however basic example I wrote does not show correct branch coverage too. 我试过Intern( http://theintern.io/ ),但是我写的基本示例也没有显示正确的分支覆盖。

Can you recommend a solution that would provide correct coverage for Q promises and works with grunt and node seamlessly? 您能否推荐一种能够为Q promises提供正确覆盖并与grunt和节点无缝协作的解决方案?

Well, checking promises for coverage should not be too hard because JavaScript has absolutely sick aspect oriented programming abilities. 那么,检查承诺的覆盖范围应该不会太难,因为JavaScript具有绝对恶劣的面向方面的编程能力。 While automated tools are offtopic here, let's go through what branches a promise has: 虽然自动化工具在这里是offtopic,但让我们来看看承诺的分支:

  • First, the resolver function for the promise (either a promise constructor or a deferred) have their own branches. 首先,promise的解析器函数(promise构造函数或延迟函数)都有自己的分支。
  • Each .then clause has 3 branches (one for success, one for failure, one for progress), if you want loop coverage too, you'd want each progress event when progress is attached to fire zero times, once and multiple times - although let's avoid progress for this question and in general since it's being deprecated and replaced in Q. 每个.then子句有3个分支(一个用于成功,一个用于失败,一个用于进度),如果你也想要循环覆盖,你需要每个进度事件,当进度被连接到火零次,一次和多次 - 虽然让我们避免这个问题的进展,因为它在Q中被弃用和替换。
  • The .done , .catch etc... are all private cases of .then . .done.catch等等是所有私人诉讼.then Aggregation methods like .all are private cases of the promise constructor since they create a new aggregate promise and their .then needs to be checked just as well. .all这样的聚合方法是promise构造函数的私有案例,因为它们创建了一个新的聚合承诺,并且它们的.then需要进行检查。

So, let's see how we can do this, first - note that Q uses Promise.prototype.then internally for aggregation methods, so we can override it: 那么,让我们看看我们如何做到这一点,首先 - 请注意Q Promise.prototype.then内部使用Promise.prototype.then进行聚合方法,因此我们可以覆盖它:

Promise.prototype._then = Promise.prototype.then;
var branches = 0;
Promise.prototype.then = function(fulfilled,rejected,progressed){
    branches += (fulfilled.call) +(rejected.call) + (progressed.call);
    var nFulfilled = function(){ branches--;return fulfilled.apply(this,arguments); };
    var nRejected = function(){ branches--; return rejected.apply(this,arguments); };
    //progression may happen more than once
    var nProgressed = function(){ 
        if(!nProgress.happened) {
            branches--;
            nProgress.happened = true;
        }
        return progressed.apply(this,arguments); 
    };
    nProgressed.happened = false;
    return this._then((fulfilled.call) ? nFulfilled : undefined,
                      (rejected.call) ? nRejected : undefined,
                      (progressed.call) ? nProgressed : undefined);
};

What we're doing here, is hooking on every .then and keeping track of handlers attached and handlers being called. 我们在这里做什么,被钩在每一个.then和跟踪附加的处理程序和处理程序被调用。

The branches variable will contain the number of code paths created with promises, and not actually executed. branches变量将包含使用promises创建的代码路径的数量,而不是实际执行的。 You can use more sophisticated logic here (for example - count rather than subtract) and I'd like to see someone pick up the glove and make a git repo from this :) 你可以在这里使用更复杂的逻辑(例如 - 计数而不是减去),我希望看到有人拿起手套并从这里制作一个git repo :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM