forked from cucumber/cucumber-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_protocol_formatter.js
51 lines (46 loc) · 1.21 KB
/
event_protocol_formatter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import escapeStringRegexp from 'escape-string-regexp'
import Formatter from './'
import path from 'path'
const EVENT_NAMES = [
'source',
'attachment',
'gherkin-document',
'pickle',
'pickle-accepted',
'pickle-rejected',
'test-run-started',
'test-case-prepared',
'test-case-started',
'test-step-started',
'test-step-attachment',
'test-step-finished',
'test-case-finished',
'test-run-finished',
]
export default class EventProtocolFormatter extends Formatter {
constructor(options) {
super(options)
EVENT_NAMES.forEach(eventName => {
options.eventBroadcaster.on(eventName, data =>
this.logEvent(eventName, data)
)
})
const pathSepRegexp = new RegExp(escapeStringRegexp(path.sep), 'g')
const pathToRemove =
this.cwd.replace(pathSepRegexp, path.posix.sep) + path.posix.sep
this.pathRegexp = new RegExp(escapeStringRegexp(pathToRemove), 'g')
}
logEvent(eventName, data) {
const text = JSON.stringify(
{ type: eventName, ...data },
::this.formatJsonData
)
this.log(`${text}\n`)
}
formatJsonData(key, value) {
if (value instanceof Error) {
return value.stack.replace(this.pathRegexp, '')
}
return value
}
}