From e29345607863d39dd38d402de8ada339c2a18422 Mon Sep 17 00:00:00 2001 From: CheadleCheadle Date: Tue, 17 Oct 2023 15:09:32 -0700 Subject: [PATCH 1/6] added optional jsw format to json reporter --- lib/cli/run-option-metadata.js | 3 ++- lib/reporters/json.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/cli/run-option-metadata.js b/lib/cli/run-option-metadata.js index 492608fbdd..58821fa4e7 100644 --- a/lib/cli/run-option-metadata.js +++ b/lib/cli/run-option-metadata.js @@ -49,7 +49,7 @@ const TYPES = (exports.types = { 'sort', 'watch' ], - number: ['retries', 'jobs'], + number: ['retries', 'jobs', 'jsonStringifyWhitespace'], string: [ 'config', 'fgrep', @@ -78,6 +78,7 @@ exports.aliases = { ignore: ['exclude'], invert: ['i'], jobs: ['j'], + jsonStringifyWhitespace: ['jsw'], 'no-colors': ['C'], 'node-option': ['n'], parallel: ['p'], diff --git a/lib/reporters/json.js b/lib/reporters/json.js index 6194d8747d..ff52d3b8a6 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -78,7 +78,7 @@ function JSONReporter(runner, options = {}) { runner.testResults = obj; - var json = JSON.stringify(obj, null, 2); + var json = JSON.stringify(obj, null, options.jsonStringifyWhitespace ?? 2); if (output) { try { fs.mkdirSync(path.dirname(output), {recursive: true}); From ce07d18d20b0ec311edabbd3a8d8f9b0c5664c55 Mon Sep 17 00:00:00 2001 From: Grant Cheadle Date: Mon, 4 Mar 2024 19:58:35 -0800 Subject: [PATCH 2/6] reporter option indentSize --- lib/cli/run-option-metadata.js | 3 +-- lib/reporters/json.js | 9 ++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/cli/run-option-metadata.js b/lib/cli/run-option-metadata.js index 58821fa4e7..492608fbdd 100644 --- a/lib/cli/run-option-metadata.js +++ b/lib/cli/run-option-metadata.js @@ -49,7 +49,7 @@ const TYPES = (exports.types = { 'sort', 'watch' ], - number: ['retries', 'jobs', 'jsonStringifyWhitespace'], + number: ['retries', 'jobs'], string: [ 'config', 'fgrep', @@ -78,7 +78,6 @@ exports.aliases = { ignore: ['exclude'], invert: ['i'], jobs: ['j'], - jsonStringifyWhitespace: ['jsw'], 'no-colors': ['C'], 'node-option': ['n'], parallel: ['p'], diff --git a/lib/reporters/json.js b/lib/reporters/json.js index ff52d3b8a6..988f6d0249 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -45,6 +45,7 @@ function JSONReporter(runner, options = {}) { var output; if (options.reporterOption && options.reporterOption.output) { + Base.consoleLog('JSON Options:', options.reporterOption); if (utils.isBrowser()) { throw createUnsupportedError('file output not supported in browser'); } @@ -78,7 +79,13 @@ function JSONReporter(runner, options = {}) { runner.testResults = obj; - var json = JSON.stringify(obj, null, options.jsonStringifyWhitespace ?? 2); + var optionsIndentSize; + if (options.reporterOption && 'indentSize' in options.reporterOption) { + optionsIndentSize = options.reporterOption.indentSize; + } + var indentSize = parseInt(optionsIndentSize, 10) || 2; // Cast string to int or default indentation + + var json = JSON.stringify(obj, null, indentSize); if (output) { try { fs.mkdirSync(path.dirname(output), {recursive: true}); From 8d165129c4712d51d213936cfd2afe8fbdf18235 Mon Sep 17 00:00:00 2001 From: "Grant A. Cheadle" <108553712+CheadleCheadle@users.noreply.github.com> Date: Mon, 4 Mar 2024 20:11:14 -0800 Subject: [PATCH 3/6] removed Base.consoleLog within json.js --- lib/reporters/json.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/reporters/json.js b/lib/reporters/json.js index 988f6d0249..7cc096db79 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -45,7 +45,6 @@ function JSONReporter(runner, options = {}) { var output; if (options.reporterOption && options.reporterOption.output) { - Base.consoleLog('JSON Options:', options.reporterOption); if (utils.isBrowser()) { throw createUnsupportedError('file output not supported in browser'); } From 092824c8c2bd755409a3051e7f589f1727d74b7c Mon Sep 17 00:00:00 2001 From: Grant Cheadle Date: Wed, 6 Mar 2024 18:30:54 -0800 Subject: [PATCH 4/6] refactor json reporter --- lib/reporters/json.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/reporters/json.js b/lib/reporters/json.js index 7cc096db79..2ea4ed913b 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -81,8 +81,11 @@ function JSONReporter(runner, options = {}) { var optionsIndentSize; if (options.reporterOption && 'indentSize' in options.reporterOption) { optionsIndentSize = options.reporterOption.indentSize; + } else { + optionsIndentSize = 2; // Default Indentation size } - var indentSize = parseInt(optionsIndentSize, 10) || 2; // Cast string to int or default indentation + + var indentSize = parseInt(optionsIndentSize, 10); // Cast string to int var json = JSON.stringify(obj, null, indentSize); if (output) { From 5a65f9cd80093220388b8121fd30bbff9407904e Mon Sep 17 00:00:00 2001 From: Grant Cheadle Date: Mon, 8 Jul 2024 12:40:55 -0700 Subject: [PATCH 5/6] refactor #5017 --- bin/mocha.js | 0 lib/reporters/json.js | 14 +++++++------- test/reporters/json.spec.js | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) mode change 100644 => 100755 bin/mocha.js diff --git a/bin/mocha.js b/bin/mocha.js old mode 100644 new mode 100755 diff --git a/lib/reporters/json.js b/lib/reporters/json.js index 2ea4ed913b..993fa379f5 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -78,16 +78,16 @@ function JSONReporter(runner, options = {}) { runner.testResults = obj; - var optionsIndentSize; - if (options.reporterOption && 'indentSize' in options.reporterOption) { - optionsIndentSize = options.reporterOption.indentSize; + options.indentation = options.reporterOption?.indentSize ?? 2; // Default indenation + + var json; + + if (options.indentation === '\\t') { + json = JSON.stringify(obj, null, '\t'); } else { - optionsIndentSize = 2; // Default Indentation size + json = JSON.stringify(obj, null, parseInt(options.indentation, 10)); } - var indentSize = parseInt(optionsIndentSize, 10); // Cast string to int - - var json = JSON.stringify(obj, null, indentSize); if (output) { try { fs.mkdirSync(path.dirname(output), {recursive: true}); diff --git a/test/reporters/json.spec.js b/test/reporters/json.spec.js index f1f1f87922..a824a0419f 100644 --- a/test/reporters/json.spec.js +++ b/test/reporters/json.spec.js @@ -229,5 +229,19 @@ describe('JSON reporter', function () { 'file output not supported in browser' ); }); + + it('should set options.indentation correctly', 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'); + }); }); }); From 0f0c3d4eff6594153733d20f4b4e2dfeade2855d Mon Sep 17 00:00:00 2001 From: Grant Cheadle Date: Thu, 8 Aug 2024 13:48:23 -0700 Subject: [PATCH 6/6] new reporter option fixes --- docs/index.md | 2 ++ lib/reporters/json.js | 6 +++--- test/reporters/json.spec.js | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) 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);