forked from cucumber/cucumber-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_formatter.js
173 lines (161 loc) · 5.08 KB
/
json_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import _ from 'lodash'
import Formatter from './'
import Status from '../status'
import { formatLocation, GherkinDocumentParser, PickleParser } from './helpers'
import { buildStepArgumentIterator } from '../step_arguments'
import { format } from 'assertion-error-formatter'
const {
getStepLineToKeywordMap,
getScenarioLineToDescriptionMap,
} = GherkinDocumentParser
const {
getScenarioDescription,
getStepLineToPickledStepMap,
getStepKeyword,
} = PickleParser
export default class JsonFormatter extends Formatter {
constructor(options) {
super(options)
options.eventBroadcaster.on('test-run-finished', ::this.onTestRunFinished)
}
convertNameToId(obj) {
return obj.name.replace(/ /g, '-').toLowerCase()
}
formatDataTable(dataTable) {
return {
rows: dataTable.rows.map(row => ({ cells: _.map(row.cells, 'value') })),
}
}
formatDocString(docString) {
return {
content: docString.content,
line: docString.location.line,
}
}
formatStepArguments(stepArguments) {
const iterator = buildStepArgumentIterator({
dataTable: this.formatDataTable.bind(this),
docString: this.formatDocString.bind(this),
})
return _.map(stepArguments, iterator)
}
onTestRunFinished() {
const groupedTestCaseAttempts = {}
_.each(this.eventDataCollector.getTestCaseAttempts(), testCaseAttempt => {
if (!testCaseAttempt.result.retried) {
const { uri } = testCaseAttempt.testCase.sourceLocation
if (!groupedTestCaseAttempts[uri]) {
groupedTestCaseAttempts[uri] = []
}
groupedTestCaseAttempts[uri].push(testCaseAttempt)
}
})
const features = _.map(groupedTestCaseAttempts, (group, uri) => {
const gherkinDocument = this.eventDataCollector.gherkinDocumentMap[uri]
const featureData = this.getFeatureData(gherkinDocument.feature, uri)
const stepLineToKeywordMap = getStepLineToKeywordMap(gherkinDocument)
const scenarioLineToDescriptionMap = getScenarioLineToDescriptionMap(
gherkinDocument
)
featureData.elements = group.map(testCaseAttempt => {
const { pickle } = testCaseAttempt
const scenarioData = this.getScenarioData({
featureId: featureData.id,
pickle,
scenarioLineToDescriptionMap,
})
const stepLineToPickledStepMap = getStepLineToPickledStepMap(pickle)
let isBeforeHook = true
scenarioData.steps = testCaseAttempt.testCase.steps.map(
(testStep, index) => {
isBeforeHook = isBeforeHook && !testStep.sourceLocation
return this.getStepData({
isBeforeHook,
stepLineToKeywordMap,
stepLineToPickledStepMap,
testStep,
testStepAttachments: testCaseAttempt.stepAttachments[index],
testStepResult: testCaseAttempt.stepResults[index],
})
}
)
return scenarioData
})
return featureData
})
this.log(JSON.stringify(features, null, 2))
}
getFeatureData(feature, uri) {
return {
description: feature.description,
keyword: feature.keyword,
name: feature.name,
line: feature.location.line,
id: this.convertNameToId(feature),
tags: this.getTags(feature),
uri,
}
}
getScenarioData({ featureId, pickle, scenarioLineToDescriptionMap }) {
const description = getScenarioDescription({
pickle,
scenarioLineToDescriptionMap,
})
return {
description,
id: `${featureId};${this.convertNameToId(pickle)}`,
keyword: 'Scenario',
line: pickle.locations[0].line,
name: pickle.name,
tags: this.getTags(pickle),
type: 'scenario',
}
}
getStepData({
isBeforeHook,
stepLineToKeywordMap,
stepLineToPickledStepMap,
testStep,
testStepAttachments,
testStepResult,
}) {
const data = {}
if (testStep.sourceLocation) {
const { line } = testStep.sourceLocation
const pickleStep = stepLineToPickledStepMap[line]
data.arguments = this.formatStepArguments(pickleStep.arguments)
data.keyword = getStepKeyword({ pickleStep, stepLineToKeywordMap })
data.line = line
data.name = pickleStep.text
} else {
data.keyword = isBeforeHook ? 'Before' : 'After'
data.hidden = true
}
if (testStep.actionLocation) {
data.match = { location: formatLocation(testStep.actionLocation) }
}
if (testStepResult) {
const { exception, status } = testStepResult
data.result = { status }
if (!_.isUndefined(testStepResult.duration)) {
data.result.duration = testStepResult.duration
}
if (status === Status.FAILED && exception) {
data.result.error_message = format(exception)
}
}
if (_.size(testStepAttachments) > 0) {
data.embeddings = testStepAttachments.map(attachment => ({
data: attachment.data,
mime_type: attachment.media.type,
}))
}
return data
}
getTags(obj) {
return _.map(obj.tags, tagData => ({
name: tagData.name,
line: tagData.location.line,
}))
}
}