Skip to content

Commit 5a65f9c

Browse files
refactor mochajs#5017
1 parent 11a0615 commit 5a65f9c

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

bin/mocha.js

100644100755
File mode changed.

lib/reporters/json.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ function JSONReporter(runner, options = {}) {
7878

7979
runner.testResults = obj;
8080

81-
var optionsIndentSize;
82-
if (options.reporterOption && 'indentSize' in options.reporterOption) {
83-
optionsIndentSize = options.reporterOption.indentSize;
81+
options.indentation = options.reporterOption?.indentSize ?? 2; // Default indenation
82+
83+
var json;
84+
85+
if (options.indentation === '\\t') {
86+
json = JSON.stringify(obj, null, '\t');
8487
} else {
85-
optionsIndentSize = 2; // Default Indentation size
88+
json = JSON.stringify(obj, null, parseInt(options.indentation, 10));
8689
}
8790

88-
var indentSize = parseInt(optionsIndentSize, 10); // Cast string to int
89-
90-
var json = JSON.stringify(obj, null, indentSize);
9191
if (output) {
9292
try {
9393
fs.mkdirSync(path.dirname(output), {recursive: true});

test/reporters/json.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,19 @@ describe('JSON reporter', function () {
229229
'file output not supported in browser'
230230
);
231231
});
232+
233+
it('should set options.indentation correctly', function () {
234+
var options = {indentation: 4};
235+
var mochaReporter = new mocha._reporter(runner, options);
236+
237+
expect(mochaReporter.options.indentation, 'to be', 4);
238+
});
239+
240+
it('should set options.indentation correctly with a tab', function () {
241+
var options = {indentation: '\t'};
242+
var mochaReporter = new mocha._reporter(runner, options);
243+
244+
expect(mochaReporter.options.indentation, 'to be', '\t');
245+
});
232246
});
233247
});

0 commit comments

Comments
 (0)