Skip to content

Commit 4c2443a

Browse files
authored
fix logFailedTestCases for RerunFormatter (#2292)
1 parent 8fece83 commit 4c2443a

File tree

3 files changed

+78
-32
lines changed

3 files changed

+78
-32
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber
1212
- New option for JUnit test suite name to be passed in `formatOptions` ([#2265](https://github.com/cucumber/cucumber-js/issues/2265))
1313
- Include source reference in emitted messages for parameter types ([#2287](https://github.com/cucumber/cucumber-js/pull/2287))
1414

15+
### Fixed
16+
- Correctly interpret retried scenarios in rerun formatter ([#2292](https://github.com/cucumber/cucumber-js/pull/2292))
17+
1518
## [9.1.2] - 2023-05-07
1619
### Changed
1720
- Only show global install warning in debug mode ([#2285](https://github.com/cucumber/cucumber-js/pull/2285))

features/retry.feature

+43-15
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Feature: Retry flaky tests
2727
Scenario: Failing
2828
Given a failing step
2929
"""
30-
Given a file named "features/step_definitions/cucumber_steps.js" with:
30+
And a file named "features/step_definitions/cucumber_steps.js" with:
3131
"""
3232
const {Given} = require('@cucumber/cucumber')
3333
@@ -47,7 +47,7 @@ Feature: Retry flaky tests
4747
Scenario: Failing
4848
Given a failing step
4949
"""
50-
Given a file named "features/step_definitions/cucumber_steps.js" with:
50+
And a file named "features/step_definitions/cucumber_steps.js" with:
5151
"""
5252
const {Given} = require('@cucumber/cucumber')
5353
@@ -67,7 +67,7 @@ Feature: Retry flaky tests
6767
Scenario: Flaky
6868
Given a flaky step
6969
"""
70-
Given a file named "features/step_definitions/cucumber_steps.js" with:
70+
And a file named "features/step_definitions/cucumber_steps.js" with:
7171
"""
7272
const {Given} = require('@cucumber/cucumber')
7373
@@ -107,7 +107,7 @@ Feature: Retry flaky tests
107107
Scenario: Flaky
108108
Given a flaky step
109109
"""
110-
Given a file named "features/step_definitions/cucumber_steps.js" with:
110+
And a file named "features/step_definitions/cucumber_steps.js" with:
111111
"""
112112
const {Given} = require('@cucumber/cucumber')
113113
@@ -149,7 +149,7 @@ Feature: Retry flaky tests
149149
Scenario: Good
150150
Given a good step
151151
"""
152-
Given a file named "features/step_definitions/cucumber_steps.js" with:
152+
And a file named "features/step_definitions/cucumber_steps.js" with:
153153
"""
154154
const {Given} = require('@cucumber/cucumber')
155155
@@ -197,7 +197,7 @@ Feature: Retry flaky tests
197197
Scenario: Good
198198
Given a good step
199199
"""
200-
Given a file named "features/step_definitions/cucumber_steps.js" with:
200+
And a file named "features/step_definitions/cucumber_steps.js" with:
201201
"""
202202
const {Given} = require('@cucumber/cucumber')
203203
@@ -246,7 +246,7 @@ Feature: Retry flaky tests
246246
Scenario: Bad
247247
Given a bad step
248248
"""
249-
Given a file named "features/step_definitions/cucumber_steps.js" with:
249+
And a file named "features/step_definitions/cucumber_steps.js" with:
250250
"""
251251
const {Given} = require('@cucumber/cucumber')
252252
@@ -305,7 +305,7 @@ Feature: Retry flaky tests
305305
Scenario: Failing
306306
Given a failing step
307307
"""
308-
Given a file named "features/step_definitions/cucumber_steps.js" with:
308+
And a file named "features/step_definitions/cucumber_steps.js" with:
309309
"""
310310
const {Given} = require('@cucumber/cucumber')
311311
@@ -330,7 +330,7 @@ Feature: Retry flaky tests
330330
Scenario: Flaky
331331
Given a flaky step
332332
"""
333-
Given a file named "features/step_definitions/cucumber_steps.js" with:
333+
And a file named "features/step_definitions/cucumber_steps.js" with:
334334
"""
335335
const {Given} = require('@cucumber/cucumber')
336336
@@ -357,7 +357,7 @@ Feature: Retry flaky tests
357357
Scenario: Flaky
358358
Given a flaky step
359359
"""
360-
Given a file named "features/step_definitions/cucumber_steps.js" with:
360+
And a file named "features/step_definitions/cucumber_steps.js" with:
361361
"""
362362
const {Given} = require('@cucumber/cucumber')
363363
@@ -386,7 +386,7 @@ Feature: Retry flaky tests
386386
Scenario: Also Flaky
387387
Given an other flaky step
388388
"""
389-
Given a file named "features/step_definitions/cucumber_steps.js" with:
389+
And a file named "features/step_definitions/cucumber_steps.js" with:
390390
"""
391391
const {Given} = require('@cucumber/cucumber')
392392
@@ -430,7 +430,7 @@ Feature: Retry flaky tests
430430
Scenario: Third Flaky
431431
Given one more flaky step
432432
"""
433-
Given a file named "features/step_definitions/cucumber_steps.js" with:
433+
And a file named "features/step_definitions/cucumber_steps.js" with:
434434
"""
435435
const {Given} = require('@cucumber/cucumber')
436436
@@ -479,7 +479,7 @@ Feature: Retry flaky tests
479479
Scenario: Flaky
480480
Given a flaky step
481481
"""
482-
Given a file named "features/step_definitions/cucumber_steps.js" with:
482+
And a file named "features/step_definitions/cucumber_steps.js" with:
483483
"""
484484
const {Before, After, Given, setWorldConstructor} = require('@cucumber/cucumber')
485485
@@ -526,7 +526,7 @@ Feature: Retry flaky tests
526526
Scenario: Passing
527527
Given a passing step
528528
"""
529-
Given a file named "features/step_definitions/cucumber_steps.js" with:
529+
And a file named "features/step_definitions/cucumber_steps.js" with:
530530
"""
531531
const {Given} = require('@cucumber/cucumber')
532532
@@ -558,7 +558,7 @@ Feature: Retry flaky tests
558558
Scenario: Passing
559559
Given a passing step
560560
"""
561-
Given a file named "features/step_definitions/cucumber_steps.js" with:
561+
And a file named "features/step_definitions/cucumber_steps.js" with:
562562
"""
563563
const {Given} = require('@cucumber/cucumber')
564564
@@ -570,3 +570,31 @@ Feature: Retry flaky tests
570570
And scenario "Failing" attempt 0 step "Given a failing step" has status "failed"
571571
And scenario "Failing" attempt 1 step "Given a failing step" has status "failed"
572572
And scenario "Passing" step "Given a passing step" has status "skipped"
573+
574+
Scenario: RerunFormatter does not report attempts that are retried
575+
Given a file named "features/a.feature" with:
576+
"""
577+
Feature:
578+
Scenario: Flaky
579+
Given a flaky step
580+
"""
581+
And a file named "features/step_definitions/cucumber_steps.js" with:
582+
"""
583+
const {Given} = require('@cucumber/cucumber')
584+
585+
let attemptCountdown = 2
586+
587+
Given(/^a flaky step$/, function() {
588+
if (attemptCountdown == 0) {
589+
return
590+
}
591+
attemptCountdown = attemptCountdown - 1
592+
throw 'fail'
593+
})
594+
"""
595+
When I run cucumber-js with `--retry 1 --format rerun`
596+
Then it outputs the text:
597+
"""
598+
features/a.feature:2
599+
"""
600+
And it fails

src/formatter/rerun_formatter.ts

+32-17
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ interface UriToLinesMap {
1313
[uri: string]: number[]
1414
}
1515

16+
function isFailedAttempt(worstTestStepResult: messages.TestStepResult) {
17+
return worstTestStepResult.status !== messages.TestStepResultStatus.PASSED
18+
}
19+
1620
export default class RerunFormatter extends Formatter {
17-
private readonly separator: string
21+
protected readonly separator: string
1822
public static readonly documentation: string =
1923
'Prints failing files with line numbers.'
2024

@@ -29,31 +33,42 @@ export default class RerunFormatter extends Formatter {
2933
this.separator = valueOrDefault(rerunOptions.separator, DEFAULT_SEPARATOR)
3034
}
3135

32-
logFailedTestCases(): void {
36+
getFailureMap(): UriToLinesMap {
3337
const mapping: UriToLinesMap = {}
3438
this.eventDataCollector
3539
.getTestCaseAttempts()
36-
.forEach(({ gherkinDocument, pickle, worstTestStepResult }) => {
37-
if (
38-
worstTestStepResult.status !== messages.TestStepResultStatus.PASSED
39-
) {
40-
const relativeUri = pickle.uri
41-
const line =
42-
getGherkinScenarioLocationMap(gherkinDocument)[
43-
pickle.astNodeIds[pickle.astNodeIds.length - 1]
44-
].line
45-
if (doesNotHaveValue(mapping[relativeUri])) {
46-
mapping[relativeUri] = []
40+
.forEach(
41+
({ gherkinDocument, pickle, worstTestStepResult, willBeRetried }) => {
42+
if (isFailedAttempt(worstTestStepResult) && !willBeRetried) {
43+
const relativeUri = pickle.uri
44+
const line =
45+
getGherkinScenarioLocationMap(gherkinDocument)[
46+
pickle.astNodeIds[pickle.astNodeIds.length - 1]
47+
].line
48+
if (doesNotHaveValue(mapping[relativeUri])) {
49+
mapping[relativeUri] = []
50+
}
51+
mapping[relativeUri].push(line)
4752
}
48-
mapping[relativeUri].push(line)
4953
}
50-
})
51-
const text = Object.keys(mapping)
54+
)
55+
56+
return mapping
57+
}
58+
59+
formatFailedTestCases(): string {
60+
const mapping = this.getFailureMap()
61+
62+
return Object.keys(mapping)
5263
.map((uri) => {
5364
const lines = mapping[uri]
5465
return `${uri}:${lines.join(':')}`
5566
})
5667
.join(this.separator)
57-
this.log(text)
68+
}
69+
70+
logFailedTestCases(): void {
71+
const failedTestCases = this.formatFailedTestCases()
72+
this.log(failedTestCases)
5873
}
5974
}

0 commit comments

Comments
 (0)