Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I subscribe to mocha suite events?

Tags:

mocha.js

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

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);
like image 298
headwinds Avatar asked Sep 06 '13 15:09

headwinds


People also ask

Is Mocha a runner test?

Mocha is one of the most popular testing frameworks for JavaScript. In particular, Mocha has been the test runner of choice in the Node.

How do you skip the mocha test?

This inclusive ability is available in Mocha by appending . skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.

What is mocha chai?

Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.

What is describe and it in mocha?

The describe() function is a way to group tests in Mocha. You can nest your tests in groups as deep as you deem necessary. describe() takes two arguments, the name of the test group and a callback function.


2 Answers

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!

like image 72
headwinds Avatar answered Oct 11 '22 18:10

headwinds


You can also get similar events at

mocha.suite.beforeEach(function() {} )
mocha.suite.afterEach(function() {} )
mocha.suite.afterAll( function() {} )
like image 36
Dinis Cruz Avatar answered Oct 11 '22 16:10

Dinis Cruz