2013-09-06 11 views
11

Mi piacerebbe essere in grado di estendere i risultati del test moka e ascoltarli dall'oggetto mocha disponibile. In primo luogo, sto cercando di ottenere i risultati "passa".Come posso iscrivermi agli eventi della suite Mocha?

Sembra che potrebbero essere sottoscritte da suite, ma io non sono sicuro di come ...

Ho provato quanto segue, che ho pensato che sarebbe ascoltare la fine di tutti i miei test:

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

mio semplice trucco che funziona, ma non è elegante a tutti - davvero triste:

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

risposta

33

ho fatto un po 'di scavare in mocha.js e infine scoperto che mocha.run() in realtà restituisce il corridore wh Che emette tutti gli eventi che stavo cercando.

L'esempio originale stavo usando solo aveva: mocha.run()

Quindi, se Mocha.run() restituisce un corridore, poi ho capito che avrei potuto iscriversi ad esso:

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

molto meglio!

+5

Questo è grande - grazie. Mi piacerebbe davvero che la documentazione della moka fosse un po 'più approfondita su come utilizzare meglio l'API programmatica. –

+0

@headwinds Puoi accettare la tua risposta! –

3

È possibile anche ottenere gli eventi simili in

mocha.suite.beforeEach(function() {}) 
mocha.suite.afterEach(function() {}) 
mocha.suite.afterAll(function() {}) 
Problemi correlati