source: trip-planner-front/node_modules/karma/lib/reporters/base.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.6 KB
Line 
1'use strict'
2
3const util = require('util')
4
5const constants = require('../constants')
6const helper = require('../helper')
7
8const BaseReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions, adapter) {
9 this.adapters = [adapter || process.stdout.write.bind(process.stdout)]
10
11 this.USE_COLORS = false
12 this.EXCLUSIVELY_USE_COLORS = undefined
13 this.LOG_SINGLE_BROWSER = '%s: %s\n'
14 this.LOG_MULTI_BROWSER = '%s %s: %s\n'
15
16 this.SPEC_FAILURE = '%s %s FAILED' + '\n'
17 this.SPEC_SLOW = '%s SLOW %s: %s\n'
18 this.ERROR = '%s ERROR\n'
19
20 this.FINISHED_ERROR = ' ERROR'
21 this.FINISHED_SUCCESS = ' SUCCESS'
22 this.FINISHED_DISCONNECTED = ' DISCONNECTED'
23
24 this.X_FAILED = ' (%d FAILED)'
25
26 this.TOTAL_SUCCESS = 'TOTAL: %d SUCCESS\n'
27 this.TOTAL_FAILED = 'TOTAL: %d FAILED, %d SUCCESS\n'
28
29 this.onRunStart = () => {
30 this._browsers = []
31 }
32
33 this.onBrowserStart = (browser) => {
34 this._browsers.push(browser)
35 }
36
37 this.renderBrowser = (browser) => {
38 const results = browser.lastResult
39 const totalExecuted = results.success + results.failed
40 let msg = `${browser}: Executed ${totalExecuted} of ${results.total}`
41
42 if (results.failed) {
43 msg += util.format(this.X_FAILED, results.failed)
44 }
45
46 if (results.skipped) {
47 msg += ` (skipped ${results.skipped})`
48 }
49
50 if (browser.isConnected) {
51 if (results.disconnected) {
52 msg += this.FINISHED_DISCONNECTED
53 } else if (results.error) {
54 msg += this.FINISHED_ERROR
55 } else if (!results.failed) {
56 msg += this.FINISHED_SUCCESS
57 }
58
59 msg += ` (${helper.formatTimeInterval(results.totalTime)} / ${helper.formatTimeInterval(results.netTime)})`
60 }
61
62 return msg
63 }
64
65 this.write = function () {
66 const msg = util.format.apply(null, Array.prototype.slice.call(arguments))
67 this.adapters.forEach((adapter) => {
68 if (!helper.isDefined(adapter.colors)) {
69 adapter.colors = useColors
70 }
71 if (!helper.isDefined(this.EXCLUSIVELY_USE_COLORS) || adapter.colors === this.EXCLUSIVELY_USE_COLORS) {
72 return adapter(msg)
73 }
74 })
75 }
76
77 this.writeCommonMsg = function () {
78 this.write.apply(this, arguments)
79 }
80
81 this.onBrowserError = (browser, error) => {
82 this.writeCommonMsg(util.format(this.ERROR, browser) + formatError(error, ' '))
83 }
84
85 this.onBrowserLog = (browser, log, type) => {
86 if (!browserConsoleLogOptions || !browserConsoleLogOptions.terminal) return
87 type = type.toUpperCase()
88 if (browserConsoleLogOptions.level) {
89 const logPriority = constants.LOG_PRIORITIES.indexOf(browserConsoleLogOptions.level.toUpperCase())
90 if (constants.LOG_PRIORITIES.indexOf(type) > logPriority) return
91 }
92 if (!helper.isString(log)) {
93 // TODO(vojta): change util to new syntax (config object)
94 log = util.inspect(log, false, undefined, this.USE_COLORS)
95 }
96 if (this._browsers && this._browsers.length === 1) {
97 this.writeCommonMsg(util.format(this.LOG_SINGLE_BROWSER, type, log))
98 } else {
99 this.writeCommonMsg(util.format(this.LOG_MULTI_BROWSER, browser, type, log))
100 }
101 }
102
103 this.onSpecComplete = (browser, result) => {
104 if (result.skipped) {
105 this.specSkipped(browser, result)
106 } else if (result.success) {
107 this.specSuccess(browser, result)
108 } else {
109 this.specFailure(browser, result)
110 }
111
112 if (reportSlow && result.time > reportSlow) {
113 const specName = result.suite.join(' ') + ' ' + result.description
114 const time = helper.formatTimeInterval(result.time)
115
116 this.writeCommonMsg(util.format(this.SPEC_SLOW, browser, time, specName))
117 }
118 }
119
120 this.specSuccess = () => {
121 }
122
123 this.specSkipped = () => {
124 }
125
126 this.specFailure = (browser, result) => {
127 const specName = result.suite.join(' ') + ' ' + result.description
128 let msg = util.format(this.SPEC_FAILURE, browser, specName)
129
130 result.log.forEach((log) => {
131 msg += formatError(log, '\t')
132 })
133
134 this.writeCommonMsg(msg)
135 }
136
137 this.onRunComplete = (browsers, results) => {
138 if (browsers.length >= 1 && !results.error && !results.disconnected) {
139 if (!results.failed) {
140 this.write(this.TOTAL_SUCCESS, results.success)
141 } else {
142 this.write(this.TOTAL_FAILED, results.failed, results.success)
143 }
144 }
145 }
146}
147
148BaseReporter.decoratorFactory = function (formatError, reportSlow, useColors, browserConsoleLogOptions) {
149 return function (self) {
150 BaseReporter.call(self, formatError, reportSlow, useColors, browserConsoleLogOptions)
151 }
152}
153
154BaseReporter.decoratorFactory.$inject = [
155 'formatError',
156 'config.reportSlowerThan',
157 'config.colors',
158 'config.browserConsoleLogOptions'
159]
160
161// PUBLISH
162module.exports = BaseReporter
Note: See TracBrowser for help on using the repository browser.