简体   繁体   中英

How to create a custom reporter with Mocha

I am sure I am missing something obvious here!

I have read the instructions here ( https://github.com/visionmedia/mocha/wiki/Third-party-reporters ), and have taken their code and added as a new node module (ie it is within node_modules/my-reporter/reporter.js ). However, I can't seem to get mocha to load this reporter.

I have tried numerous variations …

mocha allTests.js -R ./node_modules/my-reporter/reporter.js

mocha allTests.js -R my-reporter

But nothing works :-(

I can successfully load my reporter within a JS file:

my_reporter = require('./node_modules/my-reporter/reporter.js')
console.log(my_reporter);

Has anyone got any hints?

u should provide the reporter like this:

mocha allTests.js -R './node_modules/my-reporter/reporter'

You should not provide the .js file extension as that is the normal convention for including modules.

It appears that if mocha is installed globally (which I believe it almost always is), you have to install your reporter the same way.

If you don't want to publish the reporter as a public module, you can just:

npm pack
npm install /path/to/your/module.gz -g

I've tried putting the reporter everywhere else that would make sense, but was getting "invalid reporter" unless it was installed globally.

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