ColinE
ColinE

Reputation: 70142

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 (i.e. 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?

Upvotes: 5

Views: 5286

Answers (2)

Amar Zavery -  MSFT
Amar Zavery - MSFT

Reputation: 411

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.

Upvotes: 9

TPresley
TPresley

Reputation: 56

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.

Upvotes: 4

Related Questions