Skip to content

Commit

Permalink
refactor(index): basic refactoring and better mgmt of this
Browse files Browse the repository at this point in the history
feat(color): load color reporter like other base reporters
  • Loading branch information
tmcgee123 committed Apr 12, 2022
1 parent e299f36 commit f0ee528
Showing 1 changed file with 158 additions and 120 deletions.
278 changes: 158 additions & 120 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,152 +1,178 @@
var colors = require('colors');

var SpecReporter = function (baseReporterDecorator, formatError, config) {
baseReporterDecorator(this);

var platform = process ? process.platform : 'unknown';
var selectPrefix = function(defaultMarker, win32Marker) {
const colors = require('colors');
const BaseColorReporter = require('karma/lib/reporters/base_color');

const SpecReporter = function (baseReporterDecorator, formatError, config) {
//use a safe version of this
let self = this;
baseReporterDecorator(self);

//set up local primitives
self.currentSuite = [];
self.failures = [];
self.slowPokes = [];

//read system info for marker differences
const platform = process ? process.platform : 'unknown';
const selectPrefix = function(defaultMarker, win32Marker) {
return platform === 'win32' ? win32Marker : defaultMarker;
}
var reporterCfg = config.specReporter || {};
this.prefixes = Object.assign({

//get reporter config from yml
const reporterCfg = config.specReporter || {};

//set local config options for later
self.doLog = config && config.browserConsoleLogOptions && config.browserConsoleLogOptions.terminal;
self.suppressSummary = reporterCfg.suppressSummary || false;
self.suppressErrorSummary = reporterCfg.suppressErrorSummary || false;
self.showSpecTiming = reporterCfg.showSpecTiming || false;
self.showBrowser = reporterCfg.showBrowser || false;
//this one is from the base config
self.reportSlowerThan = config.reportSlowerThan || false;

//base reporter overrides
self.USE_COLORS = false;
self.prefixes = Object.assign({
success: selectPrefix('✓ ', '\u221A '),
failure: selectPrefix('✗ ', '\u00D7 '),
skipped: selectPrefix('- ', '\- ')
}, reporterCfg.prefixes);

this.failures = [];
this.USE_COLORS = false;
this.slowPokes = [];
self.LOG_SINGLE_BROWSER = '%s LOG: %s\n';
self.LOG_MULTI_BROWSER = '%s %s LOG: %s\n';

// colorize output of BaseReporter functions
if (config.colors) {
colors.enabled = true;
this.USE_COLORS = true;
this.SPEC_FAILURE = '%s %s FAILED'.red + '\n';
this.SPEC_SLOW = '%s SLOW %s: %s'.yellow + '\n';
this.ERROR = '%s ERROR'.red + '\n';
this.FINISHED_ERROR = ' ERROR'.red;
this.FINISHED_SUCCESS = ' SUCCESS'.green;
this.FINISHED_DISCONNECTED = ' DISCONNECTED'.red;
this.X_FAILED = ' (%d FAILED)'.red;
this.TOTAL_SUCCESS = 'TOTAL: %d SUCCESS'.green + '\n';
this.TOTAL_FAILED = 'TOTAL: %d FAILED, %d SUCCESS'.red + '\n';
self.USE_COLORS = true;
self.SPEC_FAILURE = '%s %s FAILED'.red + '\n';
self.SPEC_SLOW = '%s SLOW %s: %s'.yellow + '\n';
self.ERROR = '%s ERROR'.red + '\n';
self.FINISHED_ERROR = ' ERROR'.red;
self.FINISHED_SUCCESS = ' SUCCESS'.green;
self.FINISHED_DISCONNECTED = ' DISCONNECTED'.red;
self.X_FAILED = ' (%d FAILED)'.red;
self.TOTAL_SUCCESS = 'TOTAL: %d SUCCESS'.green + '\n';
self.TOTAL_FAILED = 'TOTAL: %d FAILED, %d SUCCESS'.red + '\n';
}

this.onRunComplete = function (browsers, results) {
//spec reporter handlers
self.logSlowPoke = function(result) {
self.slowPokes.push(result);
};

self.logFinalSlow = function(slowPokes) {
self.writeCommonMsg('\n\n');
self.WHITESPACE = ' ';
slowPokes
.sort(function(next, prev) {
if (next.time > prev.time) {
return -1;
} else if (next.time < prev.time) {
return 1;
} else {
return 0;
}
})
.forEach(function(slowPoke, index) {
// Only show the top 5
if (index > 4) {
return;
}

index = index + 1;

if (index == 1) {
self.writeCommonMsg(('SLOW: ' + slowPokes.length + '\n\n').yellow);
self.writeCommonMsg(('5 Slowest: ' + '\n').yellow);
}
self.writeCommonMsg((index + ') ' + slowPoke.fullName + ' (' + slowPoke.time + ')' + '\n').yellow);
}, self);
};

//base reporter event handler overrides
self.onRunComplete = function (browsers, results) {
//NOTE: the renderBrowser function is defined in karma/reporters/Base.js
if (!this.suppressSummary) {
this.writeCommonMsg('\n' + browsers.map(this.renderBrowser)
if (!self.suppressSummary) {
self.writeCommonMsg('\n' + browsers.map(self.renderBrowser)
.join('\n') + '\n');
}

if (browsers.length >= 1 && !results.disconnected && !results.error) {
if (!results.failed) {
if (!this.suppressSummary) {
this.write(this.TOTAL_SUCCESS, results.success);
if (!self.suppressSummary) {
self.write(self.TOTAL_SUCCESS, results.success);
}
} else {
if (!this.suppressSummary) {
this.write(this.TOTAL_FAILED, results.failed, results.success);
if (!self.suppressSummary) {
self.write(self.TOTAL_FAILED, results.failed, results.success);
}
if (!this.suppressErrorSummary) {
this.logFinalErrors(this.failures);
if (!self.suppressErrorSummary) {
self.logFinalErrors(self.failures);
}
}
if (this.reportSlowerThan) {
this.logFinalSlow(this.slowPokes);
if (self.reportSlowerThan) {
self.logFinalSlow(self.slowPokes);
}
}

this.write('\n');
this.failures = [];
this.currentSuite = [];
this.slowPokes = [];
self.write('\n');
self.failures = [];
self.currentSuite = [];
self.slowPokes = [];
};

this.logFinalErrors = function (errors) {
this.writeCommonMsg('\n\n');
this.WHITESPACE = ' ';
self.logFinalErrors = function (errors) {
self.writeCommonMsg('\n\n');
self.WHITESPACE = ' ';

errors.forEach(function (failure, index) {
index = index + 1;

if (index > 1) {
this.writeCommonMsg('\n');
self.writeCommonMsg('\n');
}

this.writeCommonMsg((index + ') ' + failure.description + '\n').red);
this.writeCommonMsg((this.WHITESPACE + failure.suite.join(' ') + '\n').red);
self.writeCommonMsg((index + ') ' + failure.description + '\n').red);
self.writeCommonMsg((self.WHITESPACE + failure.suite.join(' ') + '\n').red);
failure.log.forEach(function (log) {
if (reporterCfg.maxLogLines) {
log = log.split('\n').slice(0, reporterCfg.maxLogLines).join('\n');
}
this.writeCommonMsg(this.WHITESPACE + formatError(log)
self.writeCommonMsg(self.WHITESPACE + formatError(log)
.replace(/\\n/g, '\n').grey);
}, this);
}, this);
}, self);
}, self);

this.writeCommonMsg('\n');
self.writeCommonMsg('\n');
};

this.logFinalSlow = function(slowPokes) {
this.writeCommonMsg('\n\n');
this.WHITESPACE = ' ';
slowPokes
.sort(function(next, prev) {
if (next.time > prev.time) {
return -1;
} else if (next.time < prev.time) {
return 1;
} else {
return 0;
}
})
.forEach(function(slowPoke, index) {
// Only show the top 5
if (index > 4) {
return;
}

index = index + 1;

if (index == 1) {
this.writeCommonMsg(('SLOW: ' + slowPokes.length + '\n\n').yellow);
this.writeCommonMsg(('5 Slowest: ' + '\n').yellow);
}
this.writeCommonMsg((index + ') ' + slowPoke.fullName + ' (' + slowPoke.time + ')' + '\n').yellow);
}, this);
};

this.currentSuite = [];
this.writeSpecMessage = function (status) {
self.writeSpecMessage = function (status) {
return (function (browser, result) {
var suite = result.suite;
var indent = " ";
suite.forEach(function (value, index) {
if (index >= this.currentSuite.length || this.currentSuite[index] != value) {
if (index >= self.currentSuite.length || self.currentSuite[index] != value) {
if (index === 0) {
this.writeCommonMsg('\n');
self.writeCommonMsg('\n');
}

this.writeCommonMsg(indent + value + '\n');
this.currentSuite = [];
self.writeCommonMsg(indent + value + '\n');
self.currentSuite = [];
}

indent += ' ';
}, this);
}, self);

this.currentSuite = suite;
self.currentSuite = suite;

var specName = result.description;
var browserName = reporterCfg.showBrowser ? ' [' + browser.name + ']' : '';
var elapsedTime = reporterCfg.showSpecTiming ? ' (' + result.time + 'ms)' : '';

if (config.reportSlowerThan && result.time > config.reportSlowerThan) {
this.logSlowPoke(result);
self.logSlowPoke(result);
}

if (this.USE_COLORS) {
if (self.USE_COLORS) {
if (result.skipped) specName = specName.cyan;
else if (!result.success) specName = specName.red;
}
Expand All @@ -160,56 +186,68 @@ var SpecReporter = function (baseReporterDecorator, formatError, config) {
msg += '\n' + formatError(log, '\t');
});

this.writeCommonMsg(msg + '\n');
self.writeCommonMsg(msg + '\n');

// NOTE: other useful properties
// browser.id;
// browser.fullName;
}).bind(this);
}).bind(self);
};

this.LOG_SINGLE_BROWSER = '%s LOG: %s\n';
this.LOG_MULTI_BROWSER = '%s %s LOG: %s\n';
var doLog = config && config.browserConsoleLogOptions && config.browserConsoleLogOptions.terminal;
this.onBrowserLog = doLog ? function (browser, log, type) {
if (this._browsers && this._browsers.length === 1) {
this.write(this.LOG_SINGLE_BROWSER, type.toUpperCase(), this.USE_COLORS ? log.cyan : log);
} else {
this.write(this.LOG_MULTI_BROWSER, browser, type.toUpperCase(), this.USE_COLORS ? log.cyan : log);
self.onBrowserLog = (browser, log, type) => {
if (!self.doLog) {
if (self._browsers && self._browsers.length === 1) {
self.write(self.LOG_SINGLE_BROWSER, type.toUpperCase(), self.USE_COLORS ? log.cyan : log);
} else {
self.write(self.LOG_MULTI_BROWSER, browser, type.toUpperCase(), self.USE_COLORS ? log.cyan : log);
}
}
}

//suppress if option passed and use custom prefixes
self.specSuccess = () => {
if (reporterCfg.suppressFailed) {
return;
}
} : noop;
self.writeSpecMessage(self.USE_COLORS ? self.prefixes.success.green : self.prefixes.success);
}

self.specFailure = () => {
if (reporterCfg.suppressFailed) {
return;
}
self.writeSpecMessage(self.USE_COLORS ? self.prefixes.success.green : self.prefixes.success);
}

function noop() {
self.specSkipped = () => {
if (reporterCfg.suppressSkipped) {
return;
}
self.writeSpecMessage(self.USE_COLORS ? self.prefixes.skipped.cyan : self.prefixes.skipped);
}

this.onSpecFailure = function (browsers, results) {
this.failures.push(results);
this.writeSpecMessage(this.USE_COLORS ? this.prefixes.failure.red : this.prefixes.failure).apply(this, arguments);
self.specFailure = function (browsers, results) {
if (reporterCfg.suppressFailed) {
return;
}
self.failures.push(results);
self.writeSpecMessage(self.USE_COLORS ? self.prefixes.failure.red : self.prefixes.failure).apply(self, arguments);
//check for fail fast option
if (reporterCfg.failFast) {
throw new Error('Fail fast active for tests, exiting(failFast option is enabled)');
}
};

this.logSlowPoke = function(result) {
this.slowPokes.push(result);
};

this.specSuccess = reporterCfg.suppressPassed
? noop
: this.writeSpecMessage(this.USE_COLORS ? this.prefixes.success.green : this.prefixes.success);
this.specSkipped = reporterCfg.suppressSkipped
? noop
: this.writeSpecMessage(this.USE_COLORS ? this.prefixes.skipped.cyan : this.prefixes.skipped);
this.specFailure = reporterCfg.suppressFailed ? noop : this.onSpecFailure;
this.suppressSummary = reporterCfg.suppressSummary || false;
this.suppressErrorSummary = reporterCfg.suppressErrorSummary || false;
this.showSpecTiming = reporterCfg.showSpecTiming || false;
this.showBrowser = reporterCfg.showBrowser || false;
this.reportSlowerThan = config.reportSlowerThan || false;
};

SpecReporter.$inject = ['baseReporterDecorator', 'formatError', 'config'];

const SpecColorReporter = (baseReporterDecorator, formatError, config) => {
SpecReporter.call(this, baseReporterDecorator, formatError, config);
BaseColorReporter.call(this);
this.EXCLUSIVELY_USE_COLORS = true;
}

module.exports = {
'reporter:spec': ['type', SpecReporter]
'reporter:spec': ['type', SpecReporter],
'reporter:spec_color': ['type', SpecColorReporter]
};

0 comments on commit f0ee528

Please sign in to comment.