gpt4 book ai didi

mocha.js - 如何在 sailsjs 中使用 blanket 和 mocha 进行测试覆盖

转载 作者:行者123 更新时间:2023-12-01 04:52:31 25 4
gpt4 key购买 nike

我有一个 Sails 项目,它的 test/ 文件夹包含我所有的 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 框架与 blanket 集成有什么想法吗?

最佳答案

我不熟悉 Sails,但我在使用 Blanket.js 时遇到了同样的问题,并在 Blanket.js bugtracker 上发表了一 strip 有解决方法的评论,这里是:

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

我在那里建议的解决方法感觉非常像黑客。我最终放弃了 Blanket 而选择了 Istanbul 尔:https://github.com/gotwarlost/istanbul

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

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

如果您确实想坚持使用 blanket.js,您可以按照我在 Blanket.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 以换取其他东西。

关于mocha.js - 如何在 sailsjs 中使用 blanket 和 mocha 进行测试覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com