-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Added support for multiple reporters. #2184
Added support for multiple reporters. #2184
Conversation
7d97daa
to
054a1a6
Compare
I rebased it to master. Anything else that can be done to get this one merged to master? Thanks! |
Hi @santiagoaguiar! As soon as I'm done with the browser CI integration, I'll start reviewing the most demanded PRs, which includes this one. Should be very soon! |
OK! Didn't want to rush anyone :) Just let me know if there's anything I can do to help or improve! Thanks. |
Hope this will also support custom reporters. Nice feature added instead of needing to depend on other modules like mocha-multi to get this job done. |
You can specify as many reporters as you want, custom or not. However, if building a custom reporter that must write to a file, it has to accept Here's a custom reporter that works OK with this PR (we are using it) and supports |
Any news on when this might be merged? I'm hoping to use https://github.com/issacg/mocha-bamboo-reporter as well as the |
@domharrington Gonna take a look at it this weekend if no one beats me to it. |
} | ||
reporterOptions[reporterName] = reporterOptions[reporterName] || {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache the options
object for this reporter outside the loop, and then you don't have to check it it's initialised to {}
on each iteration. Having a variable with a short name like options
will also improve the legibility of the code.
After the loop you can assign back options
to reporterOptions[reporterName]
.
I'm going to close this as it's problematic with Mocha's current ability to handle options. A proper configuration file would be a prerequisite to using multiple reporters, given what we've seen here. |
@boneskull can we revisit this? We've now got mocha-multi from @glenjamin and mocha-multi-reporters from @stanleyhlng /cc @benvinegar |
so we just have to keep on using mocha-multi and hack our way into the future |
if there's a solution that has a good solution for multiplexing and individual reporter options, I'm open to it. |
This PR depends on tapjs/tap-mocha-reporter#49; review that first. This is inspired by [Mocha's][1]: ``` --reporter-options <k=v,k2=v2,...> ``` Except: * I'm using a JSON value for easier parsing and more explicit typing. This will end up using a few more characters, but the format is more explicit than Mocha's (which only uses string values?). And callers are likely to already be familiar with JSON, so we save the mental overhead of teaching them a new serialization format. * I'm using a 'reporter' namespace. This allows us to add other namespaces in the future to address other configurable aspects of tap-mocha-reporter's Formatter. For example, we could use this same CLI option to configure the runner. Somewhat related to this, if we have plans for allowing multiple reporters [2], we may want to namespace *those* now. For example: ``` --reporter-options {"reporter": {"progress": {"open": "(", "close": ")"}}} ``` to avoid the issues Mocha bumped into [3] when trying to add multi-reporter support. [1]: https://mochajs.org/#usage [2]: tapjs#335 [3]: mochajs/mocha#2184 (comment)
@boneskull, my PR to add support for individual reporter options was merged into mocha-multi today: glenjamin/mocha-multi#53 Doesn't look like it's made it to NPM yet, but if you're able to use a |
I'm a little confused why this issue was closed due to bad CLI syntax, only to be followed up by new npm modules |
Working on plumbing. Once that works and accepted, it'll be much easier to add the multi-reporter capability. |
Is there anything new regarding this? |
Not yet. |
Can we define several |
@michaelseno, AFAIK using multiple reporters still requires declaring a single reporter that is separately configured to use multiple reporters as an additional step. I was able to use multiple reporters via https://www.npmjs.com/package/mocha-multi |
@ShadSterling Could you provide an example for using multiple custom 3rd party reporter using mocha-multi? since I cannot find any reference for using multiple 3rd party reporters for mocha. the one that I can see are the the native reporters only for mocha. Thanks in advance |
IIRC there's no difference in specifying 3rd party vs native. My test case uses this set of reporters(including 3rd party reporters mochawesome and mocha-sonar-reporter), and generates all of the reports when I run |
@plroebuck , if this issue can't be reopened, where would be a good place to subscribe to get updates on this? |
Having a custom solution is quite easy, assuming the reporters you want to combine already exist. What I did is to create a small and naive custom reporter, and used the reporter in // junit-spec-reporter.js
const mocha = require("mocha");
const JUnit = require("mocha-junit-reporter");
const Spec = mocha.reporters.Spec;
const Base = mocha.reporters.Base;
function JunitSpecReporter(runner, options) {
Base.call(this, runner, options);
this._junitReporter = new JUnit(runner, options);
this._specReporter = new Spec(runner, options);
return this;
}
JunitSpecReporter.prototype.__proto__ = Base.prototype;
module.exports = JunitSpecReporter; // .mocharc.js
module.exports = {
reporter: './junit-spec-reporter.js',
reporterOptions: {
mochaFile: './tests-results/results.xml'
}
}; The example above shows how to use both spec and junit reporter. For my purpose it is working pretty good. Not sure whether other reporters can be combined in the same manner. |
@Sayan751, the mocha-multi README gives me the impression that combining multiple reporters isn't always so easy: https://github.com/glenjamin/mocha-multi#user-content-how-it-works |
@ShadSterling Well, it is not that difficult as well, is it? 😄 Jokes apart, it might be difficult if you want to combine the reporters generically. But generally when setting up the project you know which reporters are going to be used. For my case it is 'junit' (for ci) and 'spec' (for development). Combining these 2 together, gives me all info on ci log as well. And if you do know the reporter requirements, then combining those is few lines of code (with ES6/TypeScript, it is even fewer). So, I will take that solution over a hacky solution. |
Rework of #2091, rebased to master.
Includes work from @benvinegar & @misterdjules, this PR also:
--reporter json:<outputfile>
which was added on previous PRs, instead reporters that support output to files should use--reporter-options
.Base
reporter to open anoutput
option provided on reporter options as an output file, and write to it. This is a generalization of the mechanism used by thexunit
reporter to other reporters:json
,json-cov
,html-cov
,markdown
. All those reporters, and thexunit
reporter, now inherit fromBase
and use these utility methods to write their output.json
reporter writes to the file specified by the : option.Submitted as PR independent of #2091 since it's a somewhat different approach. I think this one is better, simpler, and makes more sense, but I leave the other PR in case we want to go the way @benvinegar was proposing (using the
--reporter <name>:<outputfile>
syntax.)