mocha - 如何列出将要执行的文件
mocha - how do I list files that will be executed
我想知道是否有办法让 mocha 列出它将要执行的所有测试。当我使用 mocha --help 列出它们时,我没有看到任何合理的选项;有几个记者,但 none 似乎旨在列出将要处理的文件(或将测试命名为 运行)。
reporters 的工作方式是监听 mocha 派发的事件,这些事件仅在 运行 进行真实测试时派发。
套件包含测试列表,因此有您需要的信息。但是,该套件通常仅在 运行() 上初始化。
如果您可以从 nodejs 而不是命令行使用 运行ning mocha,您可以根据 Using-mocha-programmatically:
中的代码创建此 diy 解决方案
var Mocha = require('mocha'),
fs = require('fs'),
path = require('path');
// First, you need to instantiate a Mocha instance.
var mocha = new Mocha(),
testdir = 'test';
// Then, you need to use the method "addFile" on the mocha
// object for each file.
// Here is an example:
fs.readdirSync(testdir).filter(function(file){
// Only keep the .js files
return file.substr(-3) === '.js';
}).forEach(function(file){
// Use the method "addFile" to add the file to mocha
mocha.addFile(
path.join(testdir, file)
);
});
// Here is the code to list tests without running:
// call mocha to load the files (and scan them for tests)
mocha.loadFiles(function () {
// upon completion list the tests found
var count = 0;
mocha.suite.eachTest(function (t) {
count += 1;
console.log('found test (' + count + ') ' + t.title);
})
console.log('done');
});
我想知道是否有办法让 mocha 列出它将要执行的所有测试。当我使用 mocha --help 列出它们时,我没有看到任何合理的选项;有几个记者,但 none 似乎旨在列出将要处理的文件(或将测试命名为 运行)。
reporters 的工作方式是监听 mocha 派发的事件,这些事件仅在 运行 进行真实测试时派发。
套件包含测试列表,因此有您需要的信息。但是,该套件通常仅在 运行() 上初始化。
如果您可以从 nodejs 而不是命令行使用 运行ning mocha,您可以根据 Using-mocha-programmatically:
中的代码创建此 diy 解决方案var Mocha = require('mocha'),
fs = require('fs'),
path = require('path');
// First, you need to instantiate a Mocha instance.
var mocha = new Mocha(),
testdir = 'test';
// Then, you need to use the method "addFile" on the mocha
// object for each file.
// Here is an example:
fs.readdirSync(testdir).filter(function(file){
// Only keep the .js files
return file.substr(-3) === '.js';
}).forEach(function(file){
// Use the method "addFile" to add the file to mocha
mocha.addFile(
path.join(testdir, file)
);
});
// Here is the code to list tests without running:
// call mocha to load the files (and scan them for tests)
mocha.loadFiles(function () {
// upon completion list the tests found
var count = 0;
mocha.suite.eachTest(function (t) {
count += 1;
console.log('found test (' + count + ') ' + t.title);
})
console.log('done');
});