Skip to content

Commit

Permalink
new reporter option fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
CheadleCheadle committed Aug 8, 2024
1 parent fd54588 commit 0f0c3d4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
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
6 changes: 3 additions & 3 deletions lib/reporters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/reporters/json.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 0f0c3d4

Please sign in to comment.