Skip to content

Commit

Permalink
chore(runner): update this to be more safe and organize file
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcgee123 committed Apr 12, 2022
1 parent 02db1fe commit 6c1abbc
Show file tree
Hide file tree
Showing 4 changed files with 528 additions and 376 deletions.
277 changes: 138 additions & 139 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,181 @@
var colors = require('colors');
const colors = require('colors');

var SpecReporter = function (baseReporterDecorator, formatError, config) {
function noop() {}

const SpecReporter = function (baseReporterDecorator, formatError, config) {
baseReporterDecorator(this);
let self = this;

var platform = process ? process.platform : 'unknown';
var selectPrefix = function(defaultMarker, win32Marker) {
const platform = process ? process.platform : 'unknown';
const doLog = config && config.browserConsoleLogOptions && config.browserConsoleLogOptions.terminal;
const selectPrefix = function(defaultMarker, win32Marker) {
return platform === 'win32' ? win32Marker : defaultMarker;
}
var reporterCfg = config.specReporter || {};
this.prefixes = Object.assign({
const reporterCfg = config.specReporter || {};
self.prefixes = Object.assign({
success: selectPrefix('✓ ', '\u221A '),
failure: selectPrefix('✗ ', '\u00D7 '),
skipped: selectPrefix('- ', '\- ')
}, reporterCfg.prefixes);

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

// 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) {
//NOTE: the renderBrowser function is defined in karma/reporters/Base.js
if (!this.suppressSummary) {
this.writeCommonMsg('\n' + browsers.map(this.renderBrowser)
self.onBrowserLog = doLog ? function (browser, log, type) {
if (this._browsers && this._browsers.length === 1) {
self.write(self.LOG_SINGLE_BROWSER, type.toUpperCase(), config.colors ? log.cyan : log);
} else {
self.write(self.LOG_MULTI_BROWSER, browser, type.toUpperCase(), config.colors ? log.cyan : log);
}
} : noop;

self.onSpecFailure = function (browsers, results) {
if (!reporterCfg.suppressFailed) {
self.failures.push(results);
if (reporterCfg.failFast) {
throw new Error('Fail fast active for tests, exiting(failFast option is enabled)');
}
}
};

self.onSpecComplete = function(browser, result) {
const suite = result.suite;
let indent = " ";
(function(self){
suite.forEach(function (value, index) {
if (index >= self.currentSuite.length || self.currentSuite[index] != value) {
if (index === 0) {
self.writeCommonMsg('\n');
}

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

indent += ' ';
});
}(self))

self.currentSuite = suite;

const specName = result.description;
const browserName = reporterCfg.showBrowser ? ' [' + browser.name + ']' : '';
let status = '';

if (result.skipped) {
if (reporterCfg.suppressSkipped) {
return;
}
status = self.prefixes.skipped;
if (config.colors) {
specName = specName.cyan;
status = self.prefixes.skipped.cyan;
}
} else if (result.success) {
if (reporterCfg.suppressPassed) {
return;
}
status = self.prefixes.success;
if (config.colors) {
status = self.prefixes.success.green;
}
} else {
if (reporterCfg.suppressFailed) {
return;
}
status = self.prefixes.failure;
if (config.colors) {
specName = specName.red;
}
}

const elapsedTime = reporterCfg.showSpecTiming ? ' (' + result.time + 'ms)' : '';
let msg = indent + status + specName + browserName + elapsedTime;

result.log.forEach(function (log) {
if (reporterCfg.maxLogLines) {
log = log.split('\n').slice(0, reporterCfg.maxLogLines).join('\n');
}
msg += '\n' + formatError(log, '\t');
});

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

self.onRunComplete = function (browsers, results) {
if (!reporterCfg.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 (!reporterCfg.suppressSummary) {
self.write(self.TOTAL_SUCCESS, results.success);
}
} else {
if (!this.suppressSummary) {
this.write(this.TOTAL_FAILED, results.failed, results.success);
if (!reporterCfg.suppressSummary) {
self.write(self.TOTAL_FAILED, results.failed, results.success);
}
if (!this.suppressErrorSummary) {
this.logFinalErrors(this.failures);
if (!reporterCfg.suppressErrorSummary) {
self.logFinalErrors(self.failures);
}
}
if (this.reportSlowerThan) {
this.logFinalSlow(this.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);
});
});

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

this.logFinalSlow = function(slowPokes) {
this.writeCommonMsg('\n\n');
this.WHITESPACE = ' ';
self.logFinalSlow = function(slowPokes) {
self.writeCommonMsg('\n\n');
self.WHITESPACE = ' ';
slowPokes
.sort(function(next, prev) {
if (next.time > prev.time) {
Expand All @@ -111,101 +195,16 @@ var SpecReporter = function (baseReporterDecorator, formatError, config) {
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) {
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 === 0) {
this.writeCommonMsg('\n');
}

this.writeCommonMsg(indent + value + '\n');
this.currentSuite = [];
self.writeCommonMsg(('SLOW: ' + slowPokes.length + '\n\n').yellow);
self.writeCommonMsg(('5 Slowest: ' + '\n').yellow);
}

indent += ' ';
self.writeCommonMsg((index + ') ' + slowPoke.fullName + ' (' + slowPoke.time + ')' + '\n').yellow);
}, this);

this.currentSuite = suite;

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

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

if (this.USE_COLORS) {
if (result.skipped) specName = specName.cyan;
else if (!result.success) specName = specName.red;
}

var msg = indent + status + specName + browserName + elapsedTime;

result.log.forEach(function (log) {
if (reporterCfg.maxLogLines) {
log = log.split('\n').slice(0, reporterCfg.maxLogLines).join('\n');
}
msg += '\n' + formatError(log, '\t');
});

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

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

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);
}
} : noop;

function noop() {
}

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

this.logSlowPoke = function(result) {
this.slowPokes.push(result);
self.logSlowPoke = function(result) {
self.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'];
Expand Down
Loading

0 comments on commit 6c1abbc

Please sign in to comment.