Skip to content

Commit

Permalink
fix: js config color boolean option will be removed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anthony-redFox committed May 8, 2024
1 parent 7e42fb7 commit b72a03e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 73 deletions.
104 changes: 34 additions & 70 deletions lib/reporters/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
)
)
Expand All @@ -98,18 +74,14 @@ 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'
)
)
return
}
}

function getLogSymbol(color) {
return colorUsages ? color.print(color.symbol) : color.symbol
}

/**
* Returns a unified diff between two strings.
*
Expand All @@ -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
Expand All @@ -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')
)
Expand All @@ -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
})
Expand All @@ -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`

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
}

Expand All @@ -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
Expand Down Expand Up @@ -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)))
}
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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++
Expand Down Expand Up @@ -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
)
)
Expand All @@ -585,7 +549,7 @@ function MochaReporter(baseReporterDecorator, formatError, config) {

self.write(
'\n' +
colors.success.print(
colors.success(
'Finished in ' +
formatTimeInterval(self.totalTime) +
' / ' +
Expand All @@ -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 +
' ' +
Expand All @@ -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 +
' ' +
Expand All @@ -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 +
' ' +
Expand All @@ -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 +
' ' +
Expand Down
4 changes: 1 addition & 3 deletions test/e2e/mocharepoter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -29,15 +28,14 @@ Feature: Mocha reporter
files = ['mocha/plus.js', 'mocha/test.js'];
browsers = ['ChromeHeadlessNoSandbox'];
frameworks = ['mocha', 'chai']
colors = false
plugins = [
'karma-jasmine',
'karma-chrome-launcher',
'karma-chai'
];
reporters = ['mocha'];
"""
When I start Karma
When I start Karma with additional arguments: "--no-color"
Then it passes with like:
"""
✔ 2 tests completed
Expand Down

0 comments on commit b72a03e

Please sign in to comment.