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

feat: added optional jsw format to json reporter #5017

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Empty file modified bin/mocha.js
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,8 @@ The JSON reporter outputs a single large JSON object when the tests have complet

By default, it will output to the console. To write directly to a file, use `--reporter-option output=filename.json`.

The indentation of the JSON reporter can be set with --reporter-option indentation=Number. Number being any integer. Alternatively, you can set the indentation to a tab character using --reporter-option indentation='\t'.

![json reporter](images/reporter-json.png?withoutEnlargement&resize=920,9999){:class="screenshot" loading="lazy"}

### JSON Stream
Expand Down
11 changes: 10 additions & 1 deletion lib/reporters/json.js
CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ function JSONReporter(runner, options = {}) {

runner.testResults = obj;

var json = JSON.stringify(obj, null, 2);
const indentation = options.reporterOption?.indentation?? 2;

var json;

if (indentation === '\\t') {
json = JSON.stringify(obj, null, '\t');
} else {
json = JSON.stringify(obj, null, parseInt(indentation, 10));
}

if (output) {
try {
fs.mkdirSync(path.dirname(output), {recursive: true});
Expand Down
14 changes: 14 additions & 0 deletions test/reporters/json.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,19 @@ describe('JSON reporter', function () {
'file output not supported in browser'
);
});

it('should set options.indentation with a number', function () {
var options = {indentation: 4};
var mochaReporter = new mocha._reporter(runner, options);

expect(mochaReporter.options.indentation, 'to be', 4);
});

it('should set options.indentation correctly with a tab', function () {
var options = {indentation: '\t'};
var mochaReporter = new mocha._reporter(runner, options);

expect(mochaReporter.options.indentation, 'to be', '\t');
});
});
});
Loading