mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.61k stars 3.02k forks source link

🚀 Feature: JSON reporter with optional formatting #4889

Open dandandy opened 2 years ago

dandandy commented 2 years ago

Is your feature request related to a problem or a nice-to-have?? Please describe.

The problem is the Mocha JSON reporter is hardcoded to output the JSON object over multiple lines. This makes it impossible to use with Splunk which requires the object to just be on one line.

This is the line of code I am referring to: https://github.com/mochajs/mocha/blob/master/lib/reporters/json.js#L81

...
var json = JSON.stringify(obj, null, 2);
...

Describe the solution you'd like

I would like the spacing in the JSON reporter output to be an option so we can configure this as required.

We could add a option to the reporter which allows the user to set the whitespace, but defaults to the current value if it's not provided so the change is backwards compatible.

For example, the line I linked above would be changed to something like this:

var json = JSON.stringify(obj, null, options.jsonStringifyWhitespace ?? 2);

Describe alternatives you've considered An alternative is forking the code and creating my own reporter, but it is such a small change and I believe the community would benefit from this extra option so I would rather make the change here.

dandandy commented 2 years ago

If this feature request is accepted I am happy to make the pull request

JoshuaKGoldberg commented 9 months ago

Seems pretty straightforward to me to add a --reporter-option specifically for JSON.

I'm not convinced that jsw/jsonStringifyWhitespace would be the right name for it. It seems kind of verbose. If it's a option specifically for the JSON reporter, then it doesn't need to mention "json" by name... 🤔

dandandy commented 9 months ago

Yes thank you for your suggestion. I’ll give it a shot and work on a PR