我对 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 以换取其他东西。