Skip to content

Commit 2089c60

Browse files
DevSidecharlierudolph
authored andcommitted
print text step attachments (#1041)
1 parent 58caad3 commit 2089c60

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

docs/support_files/attachments.md

+17
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,20 @@ After(function (testCase) {
8181
}
8282
});
8383
```
84+
85+
Attachments are also printed by the progress, progress-bar and summary formatters.
86+
They appear right after the step and only `text/plain` content is visible.
87+
It can be used to debug scenarios, especially in parallel mode.
88+
89+
```javascript
90+
// Step definition
91+
Given(/^a basic step$/, function() {
92+
this.attach('Some info.')
93+
this.attach('{"some", "JSON"}}', 'application/json')
94+
})
95+
96+
// Result format
97+
// ✔ Given a basic step # path:line
98+
// Attachment (text/plain): Some info.
99+
// Attachment (application/json)
100+
```

features/error_formatting.feature

+4-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ Feature: Error formatting
5454
"""
5555
import {Given} from 'cucumber'
5656
57-
Given(/^a basic step$/, function() {})
58-
Given(/^a step with a doc string$/, function(str) {})
57+
Given(/^a basic step$/, function() { this.attach('Some info.') })
58+
Given(/^a step with a doc string$/, function(str) { this.attach('{"name": "some JSON"}', 'application/json') })
5959
Given(/^a pending step$/, function() { return 'pending' })
6060
"""
6161
When I run cucumber-js
@@ -65,10 +65,12 @@ Feature: Error formatting
6565
6666
1) Scenario: some scenario # features/a.feature:3
6767
✔ Given a basic step # features/step_definitions/cucumber_steps.js:3
68+
Attachment (text/plain): Some info.
6869
✔ And a step with a doc string # features/step_definitions/cucumber_steps.js:4
6970
\"\"\"
7071
my doc string
7172
\"\"\"
73+
Attachment (application/json)
7274
? And a pending step # features/step_definitions/cucumber_steps.js:5
7375
Pending
7476
"""

src/formatter/helpers/issue_helpers.js

+8
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ function formatStep({
104104
text += indentString(`${colorFn(str)}\n`, 4)
105105
}
106106
}
107+
108+
if (testStep.attachments) {
109+
testStep.attachments.forEach(({ media, data }) => {
110+
const message = media.type === 'text/plain' ? `: ${data}` : ''
111+
text += indentString(`Attachment (${media.type})${message}\n`, 4)
112+
})
113+
}
114+
107115
const message = getStepMessage({
108116
colorFns,
109117
keywordType,

src/formatter/helpers/issue_helpers_spec.js

+58
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,63 @@ describe('IssueHelpers', () => {
235235
)
236236
})
237237
})
238+
239+
describe('step with attachment text', () => {
240+
beforeEach(function() {
241+
this.testCase.steps[0].result = this.passedStepResult
242+
this.testCase.steps[0].attachments = [
243+
{
244+
data: 'Some info.',
245+
media: {
246+
type: 'text/plain',
247+
},
248+
},
249+
{
250+
data: '{"name": "some JSON"}',
251+
media: {
252+
type: 'application/json',
253+
},
254+
},
255+
{
256+
data: Buffer.from([]),
257+
media: {
258+
type: 'image/png',
259+
},
260+
},
261+
]
262+
this.testCase.steps[1] = {
263+
actionLocation: { line: 3, uri: 'steps.js' },
264+
sourceLocation: { line: 4, uri: 'a.feature' },
265+
result: {
266+
exception: 'error',
267+
status: Status.FAILED,
268+
},
269+
}
270+
this.testCase.steps[1].attachments = [
271+
{
272+
data: 'Other info.',
273+
media: {
274+
type: 'text/plain',
275+
},
276+
},
277+
]
278+
this.testCase.steps[2].result = this.skippedStepResult
279+
this.formattedIssue = formatIssue(this.options)
280+
})
281+
282+
it('prints the scenario', function() {
283+
expect(this.formattedIssue).to.eql(
284+
'1) Scenario: my scenario # a.feature:2\n' +
285+
` ${figures.tick} Given step1 # steps.js:2\n` +
286+
` Attachment (text/plain): Some info.\n` +
287+
` Attachment (application/json)\n` +
288+
` Attachment (image/png)\n` +
289+
` ${figures.cross} When step2 # steps.js:3\n` +
290+
` Attachment (text/plain): Other info.\n` +
291+
' error\n' +
292+
' - Then step3 # steps.js:4\n\n'
293+
)
294+
})
295+
})
238296
})
239297
})

0 commit comments

Comments
 (0)