Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple reporters in Mocha browser?

Is it possible to use multiple reporters in the browser version of Mocha? I'm creating a reporter that sends test results to my server but I still want to use the default HTML reporter Mocha defaults to. Right now I'm modifying the source code to get this to work. I know Mocha uses commonJS for it's reporters too.

like image 784
Scott Moss Avatar asked Aug 18 '14 22:08

Scott Moss


People also ask

What is mocha multi reporters?

Generate multiple mocha reports in a single mocha execution. This is functionally the same as cypress-multi-reporters ; this is the ongoing fork.


1 Answers

mocha.run returns a runner object which emits useful events such as 'test end' and 'suite end'. Therefore, we can do something like this:

mocha.run()
.on('test end', function(test) {
  if ('passed' === test.state) {
    console.log('passed!', test.title)
  } else if (test.pending) {
    console.log('pending!', test.title)
  } else {
    console.log('fail!', test.title)
    let err = test.err
    console.log(err)
  }
  // logic to collect results
})
.on('suite end', function(suite) {
  if (suite.root) {
    // logic to send collected results to server
  }
})
like image 50
Thai Avatar answered Oct 01 '22 11:10

Thai