【问题标题】:how can I subscribe to mocha suite events?如何订阅 mocha 套件活动?
【发布时间】:2013-09-06 15:06:37
【问题描述】:

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

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

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

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

我的简单 hack 有效但一点也不优雅 - 真的很伤心:

setTimeout(function(){ 
        var passes = $(".passes").find("em").text();
        console.log("ui - heard the end of my test suite - passes: " + passes); 
    }, 500);

【问题讨论】:

    标签: mocha.js


    【解决方案1】:

    我在 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 文档能够更全面地说明如何更好地使用编程 API。
    • 请注意,在当前版本的 Mocha 中,fail 事件也会在 before 或 after 钩子抛出异常时发生。
    【解决方案2】:

    您也可以在

    获得类似的活动
    mocha.suite.beforeEach(function() {} )
    mocha.suite.afterEach(function() {} )
    mocha.suite.afterAll( function() {} )
    

    【讨论】:

      猜你喜欢
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2018-07-09
      • 2010-10-09
      • 1970-01-01
      • 2020-08-14
      • 2014-05-06
      相关资源
      最近更新 更多