简体   繁体   中英

Hooking a custom mocha reporter

I am new to javascript and mocha. I have been looking at how to create a third party reporter. I saw some samples at https://github.com/mochajs/mocha/wiki/Third-party-reporters

I created one that meets my needs and was able to install it and use it. But, the requirement is to not install the reporter. It can either be a different file or be part of the same js file.

Can anyone please tell me how to hook the reporter with the js file?

Here is my test js file

const mochasteps = require('mocha-steps')
var Mocha = require('mocha');
var report = require('./report')
var mochainstance = new Mocha({reporter: report});
console.log(mochainstance._reporter)

before(() => console.log('before'))
after(() => console.log('after'))

describe('Array', () => {
    step('should start empty', done => {
    var arr = [];
    if (arr.length == 1) return done()
        done(new Error('array length not 0'))
  });
});

describe('Test', () => {
    step('some Test', done => {
    done(); 
  });
});

Here is my test report.js file that does the reporting.

var mocha = require('mocha');
module.exports = report;

function report(runner) {
  mocha.reporters.Base.call(this, runner);

  runner.on('pass', function(test){
    console.log('[pass]%s', test.title);
  });

  runner.on('fail', function(test, err){
    console.log('[fail]%s(%s)', test.title, err.message);
  });

  runner.on('end', function(){
    process.exit(0);
  });
}

Thanks, r1j1m1n1

You need to pass the path to the custom reporter when running the tests. In your example, assuming test.js and report.js are in the same folder, it would be as follows:

mocha test.js -R 'report.js'

This will run the test.js tests with report.js as the reporter without having to install the reporter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM