如何以编程方式向摩卡添加递归选项?
How to add recursive option to mocha programatically?
我以编程方式跟随 tutorial 到 运行 mocha。但是,当我使用 npm test
.
进行测试时,我无法弄清楚如何添加我可以添加的 --recursive
选项
var Mocha = require('mocha'),
fs = require('fs'),
path = require('path');
// Instantiate a Mocha instance.
var mocha = new Mocha();
var testDir = 'some/dir/test'
// Add each .js file to the mocha instance
fs.readdirSync(testDir).filter(function(file){
// Only keep the .js files
return file.substr(-3) === '.js';
}).forEach(function(file){
mocha.addFile(
path.join(testDir, file)
);
});
// Run the tests.
mocha.run(function(failures){
process.on('exit', function () {
process.exit(failures); // exit with non-zero status if there were failures
});
});
--recursive option for Mocha 是一个命令行选项。它只能在通过命令行调用 Mocha 时应用,即在使用以下使用语法时:
Usage: mocha [debug] [options] [files]
您当前在节点脚本中实现的 fs.readdirSync()
不会递归地获取 .js
测试文件。它仅获取目录顶层的路径。
fs.readdirSync()
不提供递归读取目录的选项。
考虑编写一个递归获取 .js
测试文件的自定义函数。
例如:
var fs = require('fs'),
path = require('path'),
Mocha = require('mocha');
// Instantiate a Mocha instance.
var mocha = new Mocha();
var testDir = 'path/to/test/files/';
/**
* Gets the test .js file paths recursively from a given directory.
* @param {String} dir - path to directory containing test files.
* @returns {Array} Filepaths to each test .js file.
*/
function getTestPaths(dir, fileList) {
var files = fs.readdirSync(dir);
fileList = fileList || [];
files.forEach(function(file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = getTestPaths(path.join(dir, file), fileList);
} else {
fileList.push(path.join(dir, file));
}
});
return fileList.filter(function (file) {
return path.extname(file) === '.js';
});
}
// Get all .js paths and add each file to the mocha instance.
getTestPaths(testDir).forEach(function(file) {
mocha.addFile(
path.join(file)
);
});
// Run the tests.
mocha.run(function(failures) {
process.on('exit', function () {
process.exit(failures);
});
});
注意: getTestPaths
函数递归遍历目录和 returns .js
文件路径的数组。
我以编程方式跟随 tutorial 到 运行 mocha。但是,当我使用 npm test
.
--recursive
选项
var Mocha = require('mocha'),
fs = require('fs'),
path = require('path');
// Instantiate a Mocha instance.
var mocha = new Mocha();
var testDir = 'some/dir/test'
// Add each .js file to the mocha instance
fs.readdirSync(testDir).filter(function(file){
// Only keep the .js files
return file.substr(-3) === '.js';
}).forEach(function(file){
mocha.addFile(
path.join(testDir, file)
);
});
// Run the tests.
mocha.run(function(failures){
process.on('exit', function () {
process.exit(failures); // exit with non-zero status if there were failures
});
});
--recursive option for Mocha 是一个命令行选项。它只能在通过命令行调用 Mocha 时应用,即在使用以下使用语法时:
Usage: mocha [debug] [options] [files]
您当前在节点脚本中实现的 fs.readdirSync()
不会递归地获取 .js
测试文件。它仅获取目录顶层的路径。
fs.readdirSync()
不提供递归读取目录的选项。
考虑编写一个递归获取 .js
测试文件的自定义函数。
例如:
var fs = require('fs'),
path = require('path'),
Mocha = require('mocha');
// Instantiate a Mocha instance.
var mocha = new Mocha();
var testDir = 'path/to/test/files/';
/**
* Gets the test .js file paths recursively from a given directory.
* @param {String} dir - path to directory containing test files.
* @returns {Array} Filepaths to each test .js file.
*/
function getTestPaths(dir, fileList) {
var files = fs.readdirSync(dir);
fileList = fileList || [];
files.forEach(function(file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = getTestPaths(path.join(dir, file), fileList);
} else {
fileList.push(path.join(dir, file));
}
});
return fileList.filter(function (file) {
return path.extname(file) === '.js';
});
}
// Get all .js paths and add each file to the mocha instance.
getTestPaths(testDir).forEach(function(file) {
mocha.addFile(
path.join(file)
);
});
// Run the tests.
mocha.run(function(failures) {
process.on('exit', function () {
process.exit(failures);
});
});
注意: getTestPaths
函数递归遍历目录和 returns .js
文件路径的数组。