how can I subscribe to mocha suite events?

前端 未结 2 428
情书的邮戳
情书的邮戳 2021-01-31 11:01

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 look

相关标签:
2条回答
  • 2021-01-31 11:34

    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.

    The original example I was using only had: 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
         */ 
    

    much better!

    0 讨论(0)
  • 2021-01-31 11:51

    You can also get similar events at

    mocha.suite.beforeEach(function() {} )
    mocha.suite.afterEach(function() {} )
    mocha.suite.afterAll( function() {} )
    
    0 讨论(0)
提交回复
热议问题