gpt4 book ai didi

mocha.js - mocha - 如何列出将要执行的文件

转载 作者:行者123 更新时间:2023-12-01 00:49:35 27 4
gpt4 key购买 nike

我想知道是否有办法让 mocha 列出它将执行的所有测试。当我使用 mocha --help 列出它们时,我没有看到任何合理的选项;有几个报告者,但似乎没有一个旨在列出将要处理的文件(或命名将要运行的测试)。

最佳答案

记者的工作方式是监听由 mocha 调度的事件,这些事件仅在运行实际测试时调度。

套件包含测试列表,因此有您需要的信息。但是,套件通常仅在 run() 上初始化。

如果您可以从 nodejs 而不是从命令行运行 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.js - mocha - 如何列出将要执行的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31996488/

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