【问题标题】:How to do test coverage with blanket and mocha in sailsjs如何在sailsjs中使用毯子和摩卡进行测试覆盖
【发布时间】:2014-04-12 08:50:45
【问题描述】:

我有一个带有 test/ 文件夹的 Sails 项目,其中包含我所有的 mocha 测试,并希望使用以下命令创建测试覆盖率报告:

mocha --require blanket --reporter html-cov > coverage.html

package.json 中的总括配置如下所示:

"blanket": {
  "pattern": ["lib", "api", "config"],
  "data-cover-never": "node_modules",
  "data-cover-reporter-options": {
    "shortnames": true
  }
}

我将 Sails 文件夹 api/config/ 都包含在内,因为它们可能包含可测试的代码和一个文件夹 lib/,其中包含我的应用程序的大部分逻辑。

遗憾的是,blanket 覆盖模块仅涵盖直接包含在我的测试文件中的文件。由于 Sails 会动态加载我在 api/config/ 中的大部分文件,因此它们不会出现在我的报道报告中。

关于如何将 Sails 框架与毯子集成的任何想法?

【问题讨论】:

    标签: mocha.js sails.js blanket.js


    【解决方案1】:

    我对 Sails 不熟悉,但我在使用 Blanket.js 时遇到了同样的问题,并在 Blanket.js 错误跟踪器上发布了一条带有解决方法的评论,这里是:

    https://github.com/alex-seville/blanket/issues/361#issuecomment-34002054

    我在那里建议的解决方法感觉非常像黑客。我最终放弃了毯子,转而支持伊斯坦布尔:https://github.com/gotwarlost/istanbul

    Istanbul 为您提供更多指标(语句、行、函数和分支覆盖率)并输出大量 .html 文件,让您分析如何改进代码。

    鉴于目前有 79 多个未解决的问题,Blanket.js 似乎没有得到很好的维护。

    如果您确实想坚持使用毯子.js,您可以按照我在毯子.js 错误跟踪器上发布的建议,并尝试通过递归循环所有相关代码目录来将所有文件包含在测试运行中。我当时这样做的代码如下(我肯定会重构它,但它显示了意图):

    'use strict';
    
    /**
     * This file is loaded by blanket.js automatically before it instruments code to generate a code coverage report.
     */
    
    var fs = require('fs');
    var log = require('winston');
    var packageJson = require('./package.json');
    
    // For some reason the blanket config in package.json does not work automatically, set the settings manually instead
    require('blanket')({
        // Only files that match this pattern will be instrumented
        pattern: packageJson.config.blanket.pattern
    });
    
    /**
     * Walks through a directory structure recursively and executes a specified action on each file.
     * @param dir {(string|string[])} The directory path or paths.
     * @param action {function} The function that will be executed on any files found.
     *      The function expects two parameters, the first is an error object, the second the file path.
     */
    function walkDir(dir, action) {
    
        // Assert that action is a function
        if (typeof action !== "function") {
            action = function (error, file) {
            };
        }
    
        if (Array.isArray(dir)) {
            // If dir is an array loop through all elements
            for (var i = 0; i < dir.length; i++) {
                walkDir(dir[i], action);
            }
        } else {
            // Make sure dir is relative to the current directory
            if (dir.charAt(0) !== '.') {
                dir = '.' + dir;
            }
    
            // Read the directory
            fs.readdir(dir, function (err, list) {
    
                // Return the error if something went wrong
                if (err) return action(err);
    
                // For every file in the list, check if it is a directory or file.
                // When it is a directory, recursively loop through that directory as well.
                // When it is a file, perform action on file.
                list.forEach(function (file) {
                    var path = dir + "/" + file;
                    fs.stat(path, function (err, stat) {
                        if (stat && stat.isDirectory()) {
                            walkDir(path, action);
                        } else {
                            action(null, path);
                        }
                    });
                });
            });
        }
    };
    
    // Loop through all paths in the blanket pattern
    walkDir(packageJson.config.blanket.pattern, function (err, path) {
        if (err) {
            log.error(err);
            return;
        }
        log.error('Including ' + path + ' for blanket.js code coverage');
        require(path);
    });
    

    我的建议是放弃 Blanket.js 以换取其他东西。

    【讨论】:

    • 我必须同意。不幸的是(这就是我开始考虑使用Blanket.js 而不是伊斯坦布尔的原因),当前版本的Sails.js 有一个issuesails.lower() 删除了伊斯坦布尔的听众使用,因此它不输出任何覆盖率报告。但是有一个pull request 来修复它。
    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多