I'm trying to write a custom mocha reporter that integrates with a 3rd party that has a HTTP API.
I took the basic example given of a custom reporter (https://github.com/mochajs/mocha/wiki/Third-party-reporters) and attempted to add a call to the API in the start event handler. However, the callback for my http request to the third party never seems to fire. Example code shown below.
I've tested that the request code works as expected in other contexts, it just seems to be within the event handler in the mocha reporter that nothing happens.
Note - please forgive the current hack to wait on the result of the callback, I just want to make sure I wasn't exiting before the callback had a chance to fire, I'll tidy this up when I get it working!
var request = require('request');
module.exports = function (runner) {
var passes = 0;
var failures = 0;
runner.on('start', function() {
var callbackFired = false;
request('http://www.testapi.com', function (error, response, body) {
callbackFired = true;
});
while(!callbackFired){
console.log('waiting...');
}
});
runner.on('pass', function(test){
passes++;
console.log('pass: %s', test.fullTitle());
});
runner.on('fail', function(test, err){
failures++;
console.log('fail: %s -- error: %s', test.fullTitle(), err.message);
});
runner.on('end', function(){
console.log('end: %d/%d', passes, passes + failures);
process.exit(failures);
});
};