我如何订阅mocha套件活动?

时间:2013-09-06 15:06:38

标签: mocha

我希望能够扩展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);

2 个答案:

答案 0 :(得分:37)

我在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
     */ 

好多了!

答案 1 :(得分:5)

您还可以在

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