node.js - 如何使用 expresso 显示代码覆盖率输出?

标签 node.js tdd code-coverage expresso

我正在努力设置 Expresso 并运行一些测试。我跟着一个 tutorial on node tuts并有 4 个测试正在运行并通过。现在,我正在尝试在运行测试时显示代码覆盖率输出,例如 docs展示。但是,我有点迷路了。

我的 super 基础学习示例测试在名为 test 的文件夹中名为 test.js 的文件中:

var Account = require('../lib/account');

require('should');

module.exports = {
    "initial balance should be 0" : function(){
        var account = Account.create();
        account.should.have.property('balance');
        account.balance.should.be.eql(0);
    },

    "crediting account should increase the balance" : function(){
        var account = Account.create();
        account.credit(10);
        account.balance.should.be.eql(10);
    },

    "debiting account should decrease the balance" : function(){
        var account = Account.create();
        account.debit(5);
        account.balance.should.be.eql(-5);
    },

    "transferring from account a to b b should decrease from a and increase b": function(){
        var accountA = Account.create();
        var accountB = Account.create();
        accountA.credit(100);
        accountA.transfer(accountB, 25);
        accountA.balance.should.be.eql(75);
        accountB.balance.should.be.eql(25);
    }
}

代码本身在 lib/account.js 中:

var Account = function(){
    this.balance = 0;
}

module.exports.create = function(){
    return new Account();
}

Account.prototype.credit = function(amt){
    this.balance += amt;
}

Account.prototype.debit = function(amt){
    this.balance -= amt;
}

Account.prototype.transfer = function(acct, amt){
    this.debit(amt);
    acct.credit(amt);
}

Account.prototype.empty = function(acct){
    this.debit(this.balance);
}

当我从命令行运行 expresso 时,我得到:

$ expresso

    100% 4 tests

同样,如果我使用 -c 标志或各种其他选项运行 expresso,我会得到相同的输出。我想获得文档中显示的代码覆盖率输出。我还运行了命令 $ node-jscoverage lib lib-cov,现在 lib-cov 文件夹中有东西..

我错过了什么?

最佳答案

到目前为止,我发现的最佳结果是在测试运行时编辑路径:

这是 run_tests.sh

#! /bin/bash

rm -Rf ./app-cov
node_modules/expresso/deps/jscoverage/node-jscoverage app/ app-cov
NODE_PATH=$NODE_PATH:/usr/local/lib/node:/usr/local/lib/node_modules:./mmf_node_modules:./app-cov node_modules/expresso/bin/expresso -c

关于node.js - 如何使用 expresso 显示代码覆盖率输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8825675/

相关文章:

javascript - 编辑数组中对象的值

node.js - MongoDB insertMany 并跳过重复项

dart - 有没有办法在 Dart 语言中获得单元测试的测试覆盖率报告?

python - 如何让 PyC​​harm 显示使用多个进程的程序的代码覆盖率?

angular - Angular 7 中订阅方法中的代码覆盖行

javascript - 当某个延迟部分(例如 setTimeout 或 process.nextTick)中发生异常时,使用 J2V8 的 Java 应用程序会崩溃

node.js - Axios POST 表单数据在服务器端有空正文

unit-testing - 单元测试私有(private)方法似乎使整个解决方案更容易

java - 如何使用 JMockit 模拟实例化具有任何非空参数的新对象

javascript - 包括用于单元测试的外部 .js 文件