如何订阅摩卡套票活动? [英] how can I subscribe to mocha suite events?

查看:97
本文介绍了如何订阅摩卡套票活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to be able to extend the mocha test results and listen to them from the available mocha object. First, I'm looking at getting the "passes" results.

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

It looks like they might be subscribed to from suite but I'm not sure how...

我尝试了以下方法,我认为这些方法会监听所有测试的结束:

I've tried the following which I thought would listen to the end of all of my tests:

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

我的简单hack可行,但一点也不优雅-真可悲:

My simple hack which works but isn't elegant at all - sad really:

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()实际上返回了运行程序,该运行程序发出了我正在查看的所有事件.

I did some more digging in mocha.js and finally discovered that mocha.run() actually returns the runner which emits all the events I was looking.

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

The original example I was using only had: mocha.run()

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

So if Mocha.run() returns a runner, then I realized that I could subscribe to it:

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

好多了!

这篇关于如何订阅摩卡套票活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆