gpt4 book ai didi

mocha.js - 如何订阅 mocha 套件事件?

转载 作者:行者123 更新时间:2023-12-03 15:10:35 29 4
gpt4 key购买 nike

我希望能够扩展 mocha 测试结果并从可用的 mocha 对象中收听它们。首先,我正在寻找“通过”结果。

看起来他们可能是从套件中订阅的,但我不确定如何......

我尝试了以下我认为会听到我所有测试结束的方法:

var suite = mocha.suite.suites[0];
suite.on("end", function(e){ console.log(e, "mocha - heard the end of my test suite"); } );

我的简单技巧有效但一点也不优雅 - 真的很伤心:
setTimeout(function(){ 
var passes = $(".passes").find("em").text();
console.log("ui - heard the end of my test suite - passes: " + passes);
}, 500);

最佳答案

我在 mocha.js 中进行了更多挖掘,最后发现 mocha.run() 实际上返回了发出我正在寻找的所有事件的运行者。

我使用的原始示例只有: mocha.run()

因此,如果 Mocha.run() 返回一个运行者,那么我意识到我可以订阅它:

 var runner = mocha.run();
var testsPassed = 0;

var onTestPassedHandler = function(e){
testsPassed++;
console.log("onTestPassedHandler - title: " + e.title + " - total:" + testsPassed);

};

runner.on("pass", onTestPassedHandler);


/**
* These are all the events you can subscribe to:
* - `start` execution started
* - `end` execution complete
* - `suite` (suite) test suite execution started
* - `suite end` (suite) all tests (and sub-suites) have finished
* - `test` (test) test execution started
* - `test end` (test) test completed
* - `hook` (hook) hook execution started
* - `hook end` (hook) hook complete
* - `pass` (test) test passed
* - `fail` (test, err) test failed
*/

好多了!

关于mocha.js - 如何订阅 mocha 套件事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18660916/

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