-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathusage_json_formatter.ts
34 lines (30 loc) · 1.02 KB
/
usage_json_formatter.ts
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
import * as messages from '@cucumber/messages'
import { doesHaveValue } from '../value_checker'
import { getUsage } from './helpers'
import Formatter, { IFormatterOptions } from './'
import IEnvelope = messages.Envelope
export default class UsageJsonFormatter extends Formatter {
public static readonly documentation: string =
'Does what the Usage Formatter does, but outputs JSON, which can be output to a file and then consumed by other tools.'
constructor(options: IFormatterOptions) {
super(options)
options.eventBroadcaster.on('envelope', (envelope: IEnvelope) => {
if (doesHaveValue(envelope.testRunFinished)) {
this.logUsage()
}
})
}
logUsage(): void {
const usage = getUsage({
stepDefinitions: this.supportCodeLibrary.stepDefinitions,
eventDataCollector: this.eventDataCollector,
})
this.log(JSON.stringify(usage, this.replacer, 2))
}
replacer(key: string, value: any): any {
if (key === 'seconds') {
return parseInt(value)
}
return value
}
}