Skip to content

Commit

Permalink
Prepare BaseReporter for conversion to class (#3017)
Browse files Browse the repository at this point in the history
* refactor: prepared BaseReporter for converstion to ES2015 class

* refactor: import BaseReporter directly instead of using mocks
  • Loading branch information
devoto13 authored and johnjbarton committed May 24, 2018
1 parent 685f988 commit 8e54248
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 102 deletions.
98 changes: 51 additions & 47 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
var util = require('util')
'use strict'

var constants = require('../constants')
var helper = require('../helper')
const util = require('util')

var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions, adapter) {
const constants = require('../constants')
const helper = require('../helper')

const BaseReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions, adapter) {
this.adapters = [adapter || process.stdout.write.bind(process.stdout)]

this.onRunStart = function () {
this.USE_COLORS = false
this.EXCLUSIVELY_USE_COLORS = undefined
this.LOG_SINGLE_BROWSER = '%s: %s\n'
this.LOG_MULTI_BROWSER = '%s %s: %s\n'

this.SPEC_FAILURE = '%s %s FAILED' + '\n'
this.SPEC_SLOW = '%s SLOW %s: %s\n'
this.ERROR = '%s ERROR\n'

this.FINISHED_ERROR = ' ERROR'
this.FINISHED_SUCCESS = ' SUCCESS'
this.FINISHED_DISCONNECTED = ' DISCONNECTED'

this.X_FAILED = ' (%d FAILED)'

this.TOTAL_SUCCESS = 'TOTAL: %d SUCCESS\n'
this.TOTAL_FAILED = 'TOTAL: %d FAILED, %d SUCCESS\n'

this.onRunStart = () => {
this._browsers = []
}

this.onBrowserStart = function (browser) {
this.onBrowserStart = (browser) => {
this._browsers.push(browser)
}

this.renderBrowser = function (browser) {
var results = browser.lastResult
var totalExecuted = results.success + results.failed
var msg = util.format('%s: Executed %d of %d', browser, totalExecuted, results.total)
this.renderBrowser = (browser) => {
const results = browser.lastResult
const totalExecuted = results.success + results.failed
let msg = util.format('%s: Executed %d of %d', browser, totalExecuted, results.total)

if (results.failed) {
msg += util.format(this.X_FAILED, results.failed)
Expand All @@ -43,32 +63,31 @@ var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleL
return msg
}

this.renderBrowser = this.renderBrowser.bind(this)

this.write = function () {
var msg = util.format.apply(null, Array.prototype.slice.call(arguments))
var self = this
this.adapters.forEach(function (adapter) {
const msg = util.format.apply(null, Array.prototype.slice.call(arguments))
this.adapters.forEach((adapter) => {
if (!helper.isDefined(adapter.colors)) {
adapter.colors = useColors
}
if (!helper.isDefined(self.EXCLUSIVELY_USE_COLORS) || adapter.colors === self.EXCLUSIVELY_USE_COLORS) {
if (!helper.isDefined(this.EXCLUSIVELY_USE_COLORS) || adapter.colors === this.EXCLUSIVELY_USE_COLORS) {
return adapter(msg)
}
})
}

this.writeCommonMsg = this.write
this.writeCommonMsg = function () {
this.write.apply(this, arguments)
}

this.onBrowserError = function (browser, error) {
this.onBrowserError = (browser, error) => {
this.writeCommonMsg(util.format(this.ERROR, browser) + formatError(error, ' '))
}

this.onBrowserLog = function (browser, log, type) {
this.onBrowserLog = (browser, log, type) => {
if (!browserConsoleLogOptions || !browserConsoleLogOptions.terminal) return
type = type.toUpperCase()
if (browserConsoleLogOptions.level) {
var logPriority = constants.LOG_PRIORITIES.indexOf(browserConsoleLogOptions.level.toUpperCase())
const logPriority = constants.LOG_PRIORITIES.indexOf(browserConsoleLogOptions.level.toUpperCase())
if (constants.LOG_PRIORITIES.indexOf(type) > logPriority) return
}
if (!helper.isString(log)) {
Expand All @@ -82,7 +101,7 @@ var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleL
}
}

this.onSpecComplete = function (browser, result) {
this.onSpecComplete = (browser, result) => {
if (result.skipped) {
this.specSkipped(browser, result)
} else if (result.success) {
Expand All @@ -92,28 +111,31 @@ var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleL
}

if (reportSlow && result.time > reportSlow) {
var specName = result.suite.join(' ') + ' ' + result.description
var time = helper.formatTimeInterval(result.time)
const specName = result.suite.join(' ') + ' ' + result.description
const time = helper.formatTimeInterval(result.time)

this.writeCommonMsg(util.format(this.SPEC_SLOW, browser, time, specName))
}
}

this.specSuccess = this.specSkipped = function () {
this.specSuccess = () => {
}

this.specFailure = function (browser, result) {
var specName = result.suite.join(' ') + ' ' + result.description
var msg = util.format(this.SPEC_FAILURE, browser, specName)
this.specSkipped = () => {
}

this.specFailure = (browser, result) => {
const specName = result.suite.join(' ') + ' ' + result.description
let msg = util.format(this.SPEC_FAILURE, browser, specName)

result.log.forEach(function (log) {
result.log.forEach((log) => {
msg += formatError(log, '\t')
})

this.writeCommonMsg(msg)
}

this.onRunComplete = function (browsers, results) {
this.onRunComplete = (browsers, results) => {
if (browsers.length > 1 && !results.error && !results.disconnected) {
if (!results.failed) {
this.write(this.TOTAL_SUCCESS, results.success)
Expand All @@ -122,24 +144,6 @@ var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleL
}
}
}

this.USE_COLORS = false
this.EXCLUSIVELY_USE_COLORS = undefined
this.LOG_SINGLE_BROWSER = '%s: %s\n'
this.LOG_MULTI_BROWSER = '%s %s: %s\n'

this.SPEC_FAILURE = '%s %s FAILED' + '\n'
this.SPEC_SLOW = '%s SLOW %s: %s\n'
this.ERROR = '%s ERROR\n'

this.FINISHED_ERROR = ' ERROR'
this.FINISHED_SUCCESS = ' SUCCESS'
this.FINISHED_DISCONNECTED = ' DISCONNECTED'

this.X_FAILED = ' (%d FAILED)'

this.TOTAL_SUCCESS = 'TOTAL: %d SUCCESS\n'
this.TOTAL_FAILED = 'TOTAL: %d FAILED, %d SUCCESS\n'
}

BaseReporter.decoratorFactory = function (formatError, reportSlow, useColors, browserConsoleLogOptions) {
Expand Down
Loading

0 comments on commit 8e54248

Please sign in to comment.