2013-09-06 4 views
11

모카 테스트 결과를 확장하여 사용 가능한 모카 개체에서들을 수 있기를 바랍니다. 첫째, "통과"결과를 얻으려고합니다.모카 슈츠 이벤트를 구독하려면 어떻게해야합니까?

을 : 그들은 스위트에서 가입 할 수 있지만이 방법을 잘 모르겠어요처럼

나는 내 모든 테스트의 말을들을 것이라고 생각 다음을 시도했습니다 ... 보인다 작동하지만 전혀 우아하지

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); 

답변

33

나는 실제로 주자를 반환) (좀 더 mocha.js 파고 마침내 그 mocha.run을 발견했다 wh ich는 내가보고 있었던 모든 사건들을 내 보낸다.

내가 가진에만 사용 된 원래 예 : 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 
    */ 

훨씬 낫다! 감사 -

+5

에서 유사한 이벤트를 얻을 수 있습니다. mocha 문서가 프로그래밍 방식 API를 더 잘 사용하는 방법에 대해 좀 더 철저히 연구 되었으면합니다. –

+0

@headwinds 답변을 수락 할 수 있습니다! –

3

또한 이것은 대단한

mocha.suite.beforeEach(function() {}) 
mocha.suite.afterEach(function() {}) 
mocha.suite.afterAll(function() {}) 
관련 문제