diff --git a/docs/index.md b/docs/index.md index d63d3d0cfe..f03365da7c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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 diff --git a/lib/reporters/json.js b/lib/reporters/json.js index 993fa379f5..7dff11c5fe 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -78,14 +78,14 @@ function JSONReporter(runner, options = {}) { runner.testResults = obj; - options.indentation = options.reporterOption?.indentSize ?? 2; // Default indenation + const indentation = options.reporterOption?.indentation?? 2; var json; - if (options.indentation === '\\t') { + if (indentation === '\\t') { json = JSON.stringify(obj, null, '\t'); } else { - json = JSON.stringify(obj, null, parseInt(options.indentation, 10)); + json = JSON.stringify(obj, null, parseInt(indentation, 10)); } if (output) { diff --git a/test/reporters/json.spec.js b/test/reporters/json.spec.js index a824a0419f..bcebca0b9b 100644 --- a/test/reporters/json.spec.js +++ b/test/reporters/json.spec.js @@ -230,7 +230,7 @@ describe('JSON reporter', function () { ); }); - it('should set options.indentation correctly', function () { + it('should set options.indentation with a number', function () { var options = {indentation: 4}; var mochaReporter = new mocha._reporter(runner, options);