我如何订阅mocha套件活动?

hea*_*nds 15 mocha.js

我希望能够扩展mocha测试结果并从可用的mocha对象中收听它们.首先,我正在寻找获得"通行证"的结果.

看起来他们可能会从套房订阅,但我不确定如何......

我尝试过以下内容,我认为会听到我所有测试的结束:

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

我的简单黑客有效,但根本不优雅 - 真的很伤心:

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

hea*_*nds 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
     */ 
Run Code Online (Sandbox Code Playgroud)

好多了!

  • 这很棒 - 谢谢.我真的希望mocha文档对于如何更好地使用程序化API更加彻底. (6认同)

Din*_*ruz 5

您还可以在此处获得类似的活动

mocha.suite.beforeEach(function() {} )
mocha.suite.afterEach(function() {} )
mocha.suite.afterAll( function() {} )
Run Code Online (Sandbox Code Playgroud)