Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify reporterOptions in .mocharc.js #60

Open
pkuczynski opened this issue May 9, 2019 · 9 comments
Open

Specify reporterOptions in .mocharc.js #60

pkuczynski opened this issue May 9, 2019 · 9 comments

Comments

@pkuczynski
Copy link

Expected behavior

I would like to specify reporterOptions inside .mocharc.js file like this:

module.exports = {
  reporter: 'mocha-multi-reporters'
  reporterOptions: {
    reporterEnabled: 'mocha-junit-reporter, spec',
    mochaJunitReporterReporterOptions: {
      mochaFile: '...',
      includePending: true,
      outputs: true
    }
  }
}

Actual behavior

When specified in .mocarc.js, reportIOptions are being ignored and I have to still use external file:

module.exports = {
  reporter: 'mocha-multi-reporters'
  reporterOption: `configFile=config.js'
}

Information about the Issue

I am using following packages:

  • "mocha": "^6.1.4",
  • "mocha-junit-reporter": "^1.22.0",
  • "mocha-multi-reporters": "^1.1.7",
@pkuczynski
Copy link
Author

Maybe I am missing something, but I tried every possible combination without success :(

@dragancla
Copy link

dragancla commented May 17, 2019

Fixed in #32
Try:

const mocha = new Mocha({
  reporter: "mocha-multi-reporters",
  reporterOptions: {
    "reporterEnabled": 'spec, mocha-junit-reporter',
    "mochaJunitReporterReporterOptions": {
      "mochaFile": "junit-custom.xml",
      "includePending": true,
      "outputs": true
    }
  }
});

@unickq
Copy link

unickq commented Dec 18, 2019

That doesn't work @dragancla

Because we are here about .mocharc.js

meaning

"use strict";

module.exports = {
  reporter: "mocha-multi-reporters",
  reporterOptions: {
    "reporterEnabled": 'spec, mocha-junit-reporter',
    "mochaJunitReporterReporterOptions": {
      "mochaFile": "junit-custom.xml",
      "includePending": true,
      "outputs": true
    }
  }
}

@brettz9
Copy link
Contributor

brettz9 commented Jan 6, 2020

While I understand this is a different issue, is anyone able to get reporterOptions (or reporter-options) working in .mocharc.js for any reporter (including configFile for mocha-multi-reporters)? If this isn't working for Mocha, then that would presumably need to be fixed first. (I'm able to get other mocharc working, including reporter, but not reporter-options; looks like someone else had this issue as well.)

@angelocordon
Copy link

I think I was able to fix this:

module.exports = {
    reporter: 'mocha-multi-reporters',
    'reporter-options': 'configFile=reporter.config.json'
}

You still have to have a separate file, unfortunately, but it looks like using quotes around reporter-options allows mocha to read this flag.

https://mochajs.org/#configuration-format

I was looking at their examples: https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.js

and noticed that two-worded flags were kept hyphenated with quotes and not camel-cased.

@rob4629
Copy link

rob4629 commented Jun 9, 2020

👋. Using the examples above (in the .mocharc), it looks like the results output to mocha-xunit-reporter instead of mocha-junit-reporter 🤷‍♂️.

I'm not sure if this has been fully implemented yet based on this comment... but it always defaults to spec, mocha-xunit-reporter no matter what is entered.

@binhtho
Copy link

binhtho commented Jan 26, 2021

I ran into this issue as well, though I'm using the JSON format for mocharc instead of JS. I believe it would be a similar issue. What looks like is happening if you do not specify configFile in the reporterOptions is that it first loads the default file here:
https://github.com/stanleyhlng/mocha-multi-reporters/blob/master/lib/MultiReporters.js#L178

This file is defaulting to spec and xunit, which is the behavior that rob4629 saw:
https://github.com/stanleyhlng/mocha-multi-reporters/blob/master/config.json

But then it's supposed to find custom options to overlay over the defaults, which can come from mocharc.js, here:
https://github.com/stanleyhlng/mocha-multi-reporters/blob/master/lib/MultiReporters.js#L171

However, if I define something like this in mocharc.js:

{
"reporter": "mocha-multi-reporters",
"reporterEnabled": "spec,mocha-junit-reporter",
"mochaJunitReporterReporterOptions": {
"mochaFile": "dist/test/test-results.xml"
}
}

The call on 171 fails to get any options. If you print out the contents of options, which is the input to line 171, you will see this piece:

...
'reporterOptions.reporterEnabled': 'spec,mocha-junit-reporter',
'reporterOptions.mochaJunitReporterReporterOptions.mochaFile': 'dist/test/test-results.xml',
reporterOptions: undefined,
...

So the call to get() returns undefined, even though there are options under reporterOptions. This looks to be the reason you cannot currently specify reporter options directly in mocharc, they cannot be loaded correctly by the call on line 171.

@mielp
Copy link

mielp commented Aug 25, 2021

A working solution: tell mocha-multi-reporters that your configFile is your mocharc file. It would be better to have support for the current Mocha reporterOptions as an object, but that will do it in the meantime. Here is the relevant excerpt of my .mocharc.json:

{
  "reporter": "mocha-multi-reporters",
  "reporterEnabled": "spec, mocha-junit-reporter",
  "reporterOptions": "configFile=.mocharc.json",
  "mochaJunitReporterReporterOptions": {
    "mochaFile": "./junit.xml",
    "includePending": true,
    "outputs": true
  }
}

@dorkokotek651
Copy link

@mielp Didn't this solution cause you problems with parallel mode?
After deep debugging it seems like it's kind of messes with the junit reporter options in a way that fails the run :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants