From b72a03e0e4c19f633c70259e609873aee560cf78 Mon Sep 17 00:00:00 2001 From: Anton Shchekota Date: Wed, 8 May 2024 14:47:03 +0400 Subject: [PATCH] fix: js config color boolean option will be removed BREAKING CHANGE: color boolean option JS config will be removed and don't use anymore. But still works other options like --no-color, --color and inv variable NO_COLOR or FORCE_COLOR --- lib/reporters/mocha.js | 104 +++++++++++----------------------- test/e2e/mocharepoter.feature | 4 +- 2 files changed, 35 insertions(+), 73 deletions(-) diff --git a/lib/reporters/mocha.js b/lib/reporters/mocha.js index c198833b..48daddc2 100644 --- a/lib/reporters/mocha.js +++ b/lib/reporters/mocha.js @@ -50,39 +50,15 @@ function MochaReporter(baseReporterDecorator, formatError, config) { process.stdout.columns || 80 ) - const colorUsages = config.colors !== false - if (colorUsages) { - const withoutColor = (msg) => msg - config.mochaReporter.colors = { - info: withoutColor, - success: withoutColor, - warning: withoutColor, - error: withoutColor - } - } - - const colors = { - success: { - symbol: config.mochaReporter.symbols.success, - print: config.mochaReporter.colors.success - }, - info: { - symbol: config.mochaReporter.symbols.info, - print: config.mochaReporter.colors.info - }, - warning: { - symbol: config.mochaReporter.symbols.warning, - print: config.mochaReporter.colors.warning - }, - error: { - symbol: config.mochaReporter.symbols.error, - print: config.mochaReporter.colors.error - } - } + const symbols = config.mochaReporter.symbols + const colors = config.mochaReporter.colors + Object.keys(symbols).forEach( + (key) => (symbols[key] = colors[key](symbols[key])) + ) if (isNaN(config.mochaReporter.maxLogLines)) { this.write( - colors.warning.print( + colors.warning( 'Option "config.mochaReporter.maxLogLines" must be of type number. Default value 999 is used!' ) ) @@ -98,7 +74,7 @@ function MochaReporter(baseReporterDecorator, formatError, config) { diff = require('diff') } catch (e) { this.write( - colors.error.print( + colors.error( 'Error loading module mocha!\nYou have enabled diff output. That only works with mocha installed!\nRun the following command in your command line:\n npm install mocha diff\n' ) ) @@ -106,10 +82,6 @@ function MochaReporter(baseReporterDecorator, formatError, config) { } } - function getLogSymbol(color) { - return colorUsages ? color.print(color.symbol) : color.symbol - } - /** * Returns a unified diff between two strings. * @@ -121,10 +93,10 @@ function MochaReporter(baseReporterDecorator, formatError, config) { function cleanUp(line) { if (line[0] === '+') { - return indent + colors.success.print(line) + return indent + colors.success(line) } if (line[0] === '-') { - return indent + colors.error.print(line) + return indent + colors.error(line) } if (line.match(/@@/)) { return null @@ -143,9 +115,9 @@ function MochaReporter(baseReporterDecorator, formatError, config) { const lines = msg.split('\n').splice(4) return ( '\n ' + - colors.success.print('+ expected') + + colors.success('+ expected') + ' ' + - colors.error.print('- actual') + + colors.error('- actual') + '\n\n' + lines.map(cleanUp).filter(notBlank).join('\n') ) @@ -164,10 +136,10 @@ function MochaReporter(baseReporterDecorator, formatError, config) { return diff['diff' + type](actual, expected) .map(function (str) { if (str.added) { - return colors.success.print(str.value) + return colors.success(str.value) } if (str.removed) { - return colors.error.print(str.value) + return colors.error(str.value) } return str.value }) @@ -193,7 +165,7 @@ function MochaReporter(baseReporterDecorator, formatError, config) { } // legend - msg = `\n${colors.success.print('expected')} ${colors.error.print( + msg = `\n${colors.success('expected')} ${colors.error( 'actual' )}\n\n${msg}\n` @@ -259,12 +231,10 @@ function MochaReporter(baseReporterDecorator, formatError, config) { if (item.type === 'it') { if (item.skipped) { // print skipped tests info - line = colors.info.print(line + ' (skipped)') + line = colors.info(line + ' (skipped)') } else { // set color to success or error - line = item.success - ? colors.success.print(line) - : colors.error.print(line) + line = item.success ? colors.success(line) : colors.error(line) } } else { // print name of a suite block in bold @@ -334,14 +304,14 @@ function MochaReporter(baseReporterDecorator, formatError, config) { // it block if (item.type === 'it') { // make item name error - line = colors.error.print(line) + '\n' + line = colors.error(line) + '\n' // add all browser in which the test failed with color warning for (let bi = 0; bi < item.failed.length; bi++) { const browserName = item.failed[bi] line += ' '.repeat(depth + 1) + - ansis.italic(colors.warning.print(browserName)) + + ansis.italic(colors.warning(browserName)) + '\n' } @@ -365,7 +335,7 @@ function MochaReporter(baseReporterDecorator, formatError, config) { const errorMessage = log.splice(0, 1)[0] // print error message before diff - line += colors.error.print(' '.repeat(depth) + errorMessage + '\n') + line += colors.error(' '.repeat(depth) + errorMessage + '\n') const expected = item.assertionErrors[0].expected const actual = item.assertionErrors[0].actual @@ -398,13 +368,11 @@ function MochaReporter(baseReporterDecorator, formatError, config) { // print formatted stack trace after diff for (ii; ii < linesToLog; ii++) { - line += colors.error.print(formatError(log[ii])) + line += colors.error(formatError(log[ii])) } } else { for (ii; ii < linesToLog; ii++) { - line += colors.error.print( - formatError(log[ii], ' '.repeat(depth)) - ) + line += colors.error(formatError(log[ii], ' '.repeat(depth))) } } } @@ -482,11 +450,7 @@ function MochaReporter(baseReporterDecorator, formatError, config) { item.failed = item.failed || [] item.success = result.success && item.success item.name = - (item.success - ? getLogSymbol(colors.success) - : getLogSymbol(colors.error)) + - ' ' + - item.name + (item.success ? symbols.success : symbols.error) + ' ' + item.name item.skipped = result.skipped item.visited = item.visited || [] item.visited.push(browser.name) @@ -509,7 +473,7 @@ function MochaReporter(baseReporterDecorator, formatError, config) { if (config.reportSlowerThan && result.time > config.reportSlowerThan) { // add slow report warning - item.name += colors.warning.print( + item.name += colors.warning( ' (slow: ' + formatTimeInterval(result.time) + ')' ) self.numberOfSlowTests++ @@ -572,8 +536,8 @@ function MochaReporter(baseReporterDecorator, formatError, config) { if (results.error && isRunCompleted) { self.write('\n') self.write( - getLogSymbol(colors.error) + - colors.error.print( + symbols.error + + colors.error( ' Error while running the tests! Exit code: ' + results.exitCode ) ) @@ -585,7 +549,7 @@ function MochaReporter(baseReporterDecorator, formatError, config) { self.write( '\n' + - colors.success.print( + colors.success( 'Finished in ' + formatTimeInterval(self.totalTime) + ' / ' + @@ -599,8 +563,8 @@ function MochaReporter(baseReporterDecorator, formatError, config) { if (browsers.length > 0 && !results.disconnected) { self.write(ansis.underline.bold('SUMMARY:') + '\n') self.write( - colors.success.print( - getLogSymbol(colors.success) + + colors.success( + symbols.success + ' ' + results.success + ' ' + @@ -612,8 +576,8 @@ function MochaReporter(baseReporterDecorator, formatError, config) { if (self.numberOfSkippedTests > 0) { self.write( - colors.info.print( - getLogSymbol(colors.info) + + colors.info( + symbols.info + ' ' + self.numberOfSkippedTests + ' ' + @@ -626,8 +590,8 @@ function MochaReporter(baseReporterDecorator, formatError, config) { if (self.numberOfSlowTests > 0) { self.write( - colors.warning.print( - getLogSymbol(colors.warning) + + colors.warning( + symbols.warning + ' ' + self.numberOfSlowTests + ' ' + @@ -640,8 +604,8 @@ function MochaReporter(baseReporterDecorator, formatError, config) { if (results.failed) { self.write( - colors.error.print( - getLogSymbol(colors.error) + + colors.error( + symbols.error + ' ' + results.failed + ' ' + diff --git a/test/e2e/mocharepoter.feature b/test/e2e/mocharepoter.feature index a9000e85..2c9eba40 100644 --- a/test/e2e/mocharepoter.feature +++ b/test/e2e/mocharepoter.feature @@ -9,7 +9,6 @@ Feature: Mocha reporter files = ['mocha/plus.js', 'mocha/test.js']; browsers = ['ChromeHeadlessNoSandbox']; frameworks = ['mocha', 'chai'] - colors = true plugins = [ 'karma-jasmine', 'karma-chrome-launcher', @@ -29,7 +28,6 @@ Feature: Mocha reporter files = ['mocha/plus.js', 'mocha/test.js']; browsers = ['ChromeHeadlessNoSandbox']; frameworks = ['mocha', 'chai'] - colors = false plugins = [ 'karma-jasmine', 'karma-chrome-launcher', @@ -37,7 +35,7 @@ Feature: Mocha reporter ]; reporters = ['mocha']; """ - When I start Karma + When I start Karma with additional arguments: "--no-color" Then it passes with like: """ ✔ 2 tests completed