Skip to content

Commit d2b773a

Browse files
authored
omit filtered-out pickles from counts, filters (#273)
* add example ndjson * add failing test * apply fix * add comment * better implementation * update CHANGELOG.md
1 parent cde2cc3 commit d2b773a

File tree

6 files changed

+95
-10
lines changed

6 files changed

+95
-10
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
### Changed
1010
- Single line breaks in descriptions are rendered less surprisingly ([#227](https://github.com/cucumber/react-components/pull/227))
1111

12+
### Fixed
13+
- Omit filtered-out pickles from counts, filters ([#273](https://github.com/cucumber/react-components/pull/273))
14+
1215
## [20.1.0] - 2022-05-27
1316
### Changed
1417
- Allow showing detail of every example, simplify examples table ([#159](https://github.com/cucumber/react-components/pull/159))

src/EnvelopesQueryContext.ts

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export class EnvelopesQuery {
2727
public getTestRunFinished(): messages.TestRunFinished | undefined {
2828
return this.find((envelope) => !!envelope.testRunFinished)?.testRunFinished
2929
}
30+
31+
// TODO add to cucumber query
32+
public hasTestCase(pickleId: string): boolean {
33+
return !!this.find((envelope) => envelope.testCase?.pickleId === pickleId)
34+
}
3035
}
3136

3237
export default React.createContext(new EnvelopesQuery())

src/components/app/FilteredResults.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const FilteredResults: React.FunctionComponent<IProps> = ({ className })
2323
const allDocuments = gherkinQuery.getGherkinDocuments()
2424

2525
const { scenarioCountByStatus, statusesWithScenarios, totalScenarioCount } =
26-
countScenariosByStatuses(gherkinQuery, cucumberQuery)
26+
countScenariosByStatuses(gherkinQuery, cucumberQuery, envelopesQuery)
2727

2828
const search = new Search(gherkinQuery)
2929
for (const gherkinDocument of allDocuments) {

src/countScenariosByStatuses.spec.ts

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import { SupportCode } from '@cucumber/fake-cucumber'
22
import { Query as GherkinQuery } from '@cucumber/gherkin-utils'
33
import * as messages from '@cucumber/messages'
4-
import { SourceReference, TestStepResultStatus } from '@cucumber/messages'
4+
import { Envelope, SourceReference, TestStepResultStatus } from '@cucumber/messages'
55
import { Query as CucumberQuery } from '@cucumber/query'
6+
import fs from 'fs'
7+
import path from 'path'
68

79
import { runFeature } from '../test-utils'
810
import countScenariosByStatuses from './countScenariosByStatuses'
11+
import { EnvelopesQuery } from './EnvelopesQueryContext'
912

1013
const sourceReference: SourceReference = {}
1114

1215
describe('countScenariosByStatuses', () => {
1316
let gherkinQuery: GherkinQuery
1417
let cucumberQuery: CucumberQuery
18+
let envelopesQuery: EnvelopesQuery
1519
let supportCode: SupportCode
1620

1721
jest.setTimeout(3000)
1822

1923
beforeEach(() => {
2024
gherkinQuery = new GherkinQuery()
2125
cucumberQuery = new CucumberQuery()
26+
envelopesQuery = new EnvelopesQuery()
2227
supportCode = new SupportCode()
2328
supportCode.defineStepDefinition(sourceReference, 'a passed step', () => null)
2429
supportCode.defineStepDefinition(sourceReference, 'a failed step', () => {
@@ -45,10 +50,11 @@ Feature: statuses
4550
const envelopes = await runFeature(feature, gherkinQuery, supportCode)
4651
for (const envelope of envelopes) {
4752
cucumberQuery.update(envelope)
53+
envelopesQuery.update(envelope)
4854
}
4955

5056
const { scenarioCountByStatus, statusesWithScenarios, totalScenarioCount } =
51-
countScenariosByStatuses(gherkinQuery, cucumberQuery)
57+
countScenariosByStatuses(gherkinQuery, cucumberQuery, envelopesQuery)
5258

5359
expect(scenarioCountByStatus[messages.TestStepResultStatus.PASSED]).toEqual(2)
5460
expect(scenarioCountByStatus[messages.TestStepResultStatus.FAILED]).toEqual(1)
@@ -79,10 +85,11 @@ Feature: statuses
7985
const envelopes = await runFeature(feature, gherkinQuery, supportCode)
8086
for (const envelope of envelopes) {
8187
cucumberQuery.update(envelope)
88+
envelopesQuery.update(envelope)
8289
}
8390

8491
const { scenarioCountByStatus, statusesWithScenarios, totalScenarioCount } =
85-
countScenariosByStatuses(gherkinQuery, cucumberQuery)
92+
countScenariosByStatuses(gherkinQuery, cucumberQuery, envelopesQuery)
8693

8794
expect(scenarioCountByStatus[messages.TestStepResultStatus.PASSED]).toEqual(1)
8895
expect(scenarioCountByStatus[messages.TestStepResultStatus.FAILED]).toEqual(1)
@@ -94,4 +101,29 @@ Feature: statuses
94101
])
95102
expect(totalScenarioCount).toEqual(3)
96103
})
104+
105+
it('only includes pickles that were slated for execution as test cases', () => {
106+
const raw = fs.readFileSync(
107+
path.join(__dirname, '../test-utils/messages/filtered-pickles.ndjson'),
108+
{
109+
encoding: 'utf-8',
110+
}
111+
)
112+
const envelopes: Envelope[] = JSON.parse('[' + raw.trim().split('\n').join(',') + ']')
113+
const gherkinQuery = new GherkinQuery()
114+
const cucumberQuery = new CucumberQuery()
115+
envelopes.forEach((envelope) => {
116+
gherkinQuery.update(envelope)
117+
cucumberQuery.update(envelope)
118+
envelopesQuery.update(envelope)
119+
})
120+
121+
const { totalScenarioCount } = countScenariosByStatuses(
122+
gherkinQuery,
123+
cucumberQuery,
124+
envelopesQuery
125+
)
126+
127+
expect(totalScenarioCount).toEqual(1)
128+
})
97129
})

src/countScenariosByStatuses.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { GherkinDocumentWalker, Query as GherkinQuery } from '@cucumber/gherkin-
22
import { getWorstTestStepResult, TestStepResultStatus } from '@cucumber/messages'
33
import { Query as CucumberQuery } from '@cucumber/query'
44

5+
import { EnvelopesQuery } from './EnvelopesQueryContext'
6+
57
export function makeEmptyScenarioCountsByStatus(): Record<TestStepResultStatus, number> {
68
return {
79
[TestStepResultStatus.UNKNOWN]: 0,
@@ -16,7 +18,8 @@ export function makeEmptyScenarioCountsByStatus(): Record<TestStepResultStatus,
1618

1719
export default function countScenariosByStatuses(
1820
gherkinQuery: GherkinQuery,
19-
cucumberQuery: CucumberQuery
21+
cucumberQuery: CucumberQuery,
22+
envelopesQuery: EnvelopesQuery
2023
): {
2124
scenarioCountByStatus: Record<TestStepResultStatus, number>
2225
statusesWithScenarios: readonly TestStepResultStatus[]
@@ -33,11 +36,13 @@ export default function countScenariosByStatuses(
3336
const pickleIds = gherkinQuery.getPickleIds(gherkinDocument.uri, scenario.id)
3437

3538
pickleIds.forEach((pickleId) => {
36-
const status = getWorstTestStepResult(
37-
cucumberQuery.getPickleTestStepResults([pickleId])
38-
).status
39-
40-
scenarioCountByStatus[status] = scenarioCountByStatus[status] + 1
39+
// if no test case then this pickle was omitted by filtering e.g. tags
40+
if (envelopesQuery.hasTestCase(pickleId)) {
41+
const status = getWorstTestStepResult(
42+
cucumberQuery.getPickleTestStepResults([pickleId])
43+
).status
44+
scenarioCountByStatus[status] = scenarioCountByStatus[status] + 1
45+
}
4146
})
4247
},
4348
}

0 commit comments

Comments
 (0)