我有一个测试运行器应用程序,它接受一个目标并基于该目标运行一个测试集。简化版本如下所示:
"use strict";
const mocha = require("mocha");
const mochaEngine = new mocha({ });
const execute = async function(args)
{
if(!args.targets || args.targets.length < 1)
{
args.targets = ["all"];
}
for(let targetIndex = 0; targetIndex < args.targets.length; targetIndex++)
{
switch(args.targets[targetIndex])
{
case "all" :
{
mochaEngine.addFile(__dirname + "/tests/module/service_1/core_tests.js");
mochaEngine.run();
break;
}
default :
{
console.log("Unrecognized test target: " + args.targets[targetIndex]);
break;
}
}
}
};
module.exports.execute = execute;
它正在运行的测试文件是规范的,据我所知看起来就像一个 hello world 级别的测试文件,但似乎运行的唯一代码是“描述”。 “之前”和“它”被完全忽略。
const mocha = require("mocha");
const before = mocha.before;
const describe = mocha.describe;
const it = mocha.it;
const done = mocha.done;
const assert = require("assert");
//set local https
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
describe("core functions", function () {
before(function() {
console.log("running test file " + __dirname + "/" + __filename);
});
console.log("outer describe");
describe("core functions", function () {
console.log("inner describe");
it("knows how to equal things", function() {
console.log("it 1");
assert.equal(5, 5);
done();
});
it("knows how to not equal things", function() {
console.log("it 2");
assert.equal(6, 7);
});
it("knows how to keep running after a failed test", function() {
console.log("it 3");
assert.equal(5, 5);
});
});
});
这是输出:
outer describe
inner describe
请注意,它不会记录“it”或“before”语句中的任何内容。调试器也永远不会去那里。
我在这里做错了什么?这看起来像我见过的所有其他测试文件,唯一的区别是我从代码中调用 .run,这无关紧要。
最佳答案
Mocha.run
是一个异步函数,需要回调作为参数。
结帐签名:https://mochajs.org/api/mocha#run
所以我的猜测是您的代码只是在测试有机会运行之前退出。
关于javascript - 通过 mocha.run() 调用的 Mocha 测试不会在 "it"函数内部运行代码,也不会在 "before"函数内部运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59221071/