在Mocha浏览器中使用多个记者?

时间:2014-08-18 22:21:17

标签: javascript testing mocha

是否可以在浏览器版本的Mocha中使用多个记者?我正在创建一个将测试结果发送到我的服务器的记者,但我仍然想使用默认的HTML记者Mocha默认值。现在我正在修改源代码以使其工作。我知道摩卡也为它的记者使用commonJS。

1 个答案:

答案 0 :(得分:2)

mocha.run返回一个转发器对象,该对象会发出有用的事件,例如'test end''suite end'。因此,我们可以这样做:

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