Skip to content

Commit

Permalink
omit filtered-out pickles from counts, filters (#273)
Browse files Browse the repository at this point in the history
* add example ndjson

* add failing test

* apply fix

* add comment

* better implementation

* update CHANGELOG.md
  • Loading branch information
davidjgoss authored Sep 13, 2022
1 parent cde2cc3 commit d2b773a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Single line breaks in descriptions are rendered less surprisingly ([#227](https://github.com/cucumber/react-components/pull/227))

### Fixed
- Omit filtered-out pickles from counts, filters ([#273](https://github.com/cucumber/react-components/pull/273))

## [20.1.0] - 2022-05-27
### Changed
- Allow showing detail of every example, simplify examples table ([#159](https://github.com/cucumber/react-components/pull/159))
Expand Down
5 changes: 5 additions & 0 deletions src/EnvelopesQueryContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export class EnvelopesQuery {
public getTestRunFinished(): messages.TestRunFinished | undefined {
return this.find((envelope) => !!envelope.testRunFinished)?.testRunFinished
}

// TODO add to cucumber query
public hasTestCase(pickleId: string): boolean {
return !!this.find((envelope) => envelope.testCase?.pickleId === pickleId)
}
}

export default React.createContext(new EnvelopesQuery())
2 changes: 1 addition & 1 deletion src/components/app/FilteredResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const FilteredResults: React.FunctionComponent<IProps> = ({ className })
const allDocuments = gherkinQuery.getGherkinDocuments()

const { scenarioCountByStatus, statusesWithScenarios, totalScenarioCount } =
countScenariosByStatuses(gherkinQuery, cucumberQuery)
countScenariosByStatuses(gherkinQuery, cucumberQuery, envelopesQuery)

const search = new Search(gherkinQuery)
for (const gherkinDocument of allDocuments) {
Expand Down
38 changes: 35 additions & 3 deletions src/countScenariosByStatuses.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { SupportCode } from '@cucumber/fake-cucumber'
import { Query as GherkinQuery } from '@cucumber/gherkin-utils'
import * as messages from '@cucumber/messages'
import { SourceReference, TestStepResultStatus } from '@cucumber/messages'
import { Envelope, SourceReference, TestStepResultStatus } from '@cucumber/messages'
import { Query as CucumberQuery } from '@cucumber/query'
import fs from 'fs'
import path from 'path'

import { runFeature } from '../test-utils'
import countScenariosByStatuses from './countScenariosByStatuses'
import { EnvelopesQuery } from './EnvelopesQueryContext'

const sourceReference: SourceReference = {}

describe('countScenariosByStatuses', () => {
let gherkinQuery: GherkinQuery
let cucumberQuery: CucumberQuery
let envelopesQuery: EnvelopesQuery
let supportCode: SupportCode

jest.setTimeout(3000)

beforeEach(() => {
gherkinQuery = new GherkinQuery()
cucumberQuery = new CucumberQuery()
envelopesQuery = new EnvelopesQuery()
supportCode = new SupportCode()
supportCode.defineStepDefinition(sourceReference, 'a passed step', () => null)
supportCode.defineStepDefinition(sourceReference, 'a failed step', () => {
Expand All @@ -45,10 +50,11 @@ Feature: statuses
const envelopes = await runFeature(feature, gherkinQuery, supportCode)
for (const envelope of envelopes) {
cucumberQuery.update(envelope)
envelopesQuery.update(envelope)
}

const { scenarioCountByStatus, statusesWithScenarios, totalScenarioCount } =
countScenariosByStatuses(gherkinQuery, cucumberQuery)
countScenariosByStatuses(gherkinQuery, cucumberQuery, envelopesQuery)

expect(scenarioCountByStatus[messages.TestStepResultStatus.PASSED]).toEqual(2)
expect(scenarioCountByStatus[messages.TestStepResultStatus.FAILED]).toEqual(1)
Expand Down Expand Up @@ -79,10 +85,11 @@ Feature: statuses
const envelopes = await runFeature(feature, gherkinQuery, supportCode)
for (const envelope of envelopes) {
cucumberQuery.update(envelope)
envelopesQuery.update(envelope)
}

const { scenarioCountByStatus, statusesWithScenarios, totalScenarioCount } =
countScenariosByStatuses(gherkinQuery, cucumberQuery)
countScenariosByStatuses(gherkinQuery, cucumberQuery, envelopesQuery)

expect(scenarioCountByStatus[messages.TestStepResultStatus.PASSED]).toEqual(1)
expect(scenarioCountByStatus[messages.TestStepResultStatus.FAILED]).toEqual(1)
Expand All @@ -94,4 +101,29 @@ Feature: statuses
])
expect(totalScenarioCount).toEqual(3)
})

it('only includes pickles that were slated for execution as test cases', () => {
const raw = fs.readFileSync(
path.join(__dirname, '../test-utils/messages/filtered-pickles.ndjson'),
{
encoding: 'utf-8',
}
)
const envelopes: Envelope[] = JSON.parse('[' + raw.trim().split('\n').join(',') + ']')
const gherkinQuery = new GherkinQuery()
const cucumberQuery = new CucumberQuery()
envelopes.forEach((envelope) => {
gherkinQuery.update(envelope)
cucumberQuery.update(envelope)
envelopesQuery.update(envelope)
})

const { totalScenarioCount } = countScenariosByStatuses(
gherkinQuery,
cucumberQuery,
envelopesQuery
)

expect(totalScenarioCount).toEqual(1)
})
})
17 changes: 11 additions & 6 deletions src/countScenariosByStatuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { GherkinDocumentWalker, Query as GherkinQuery } from '@cucumber/gherkin-
import { getWorstTestStepResult, TestStepResultStatus } from '@cucumber/messages'
import { Query as CucumberQuery } from '@cucumber/query'

import { EnvelopesQuery } from './EnvelopesQueryContext'

export function makeEmptyScenarioCountsByStatus(): Record<TestStepResultStatus, number> {
return {
[TestStepResultStatus.UNKNOWN]: 0,
Expand All @@ -16,7 +18,8 @@ export function makeEmptyScenarioCountsByStatus(): Record<TestStepResultStatus,

export default function countScenariosByStatuses(
gherkinQuery: GherkinQuery,
cucumberQuery: CucumberQuery
cucumberQuery: CucumberQuery,
envelopesQuery: EnvelopesQuery
): {
scenarioCountByStatus: Record<TestStepResultStatus, number>
statusesWithScenarios: readonly TestStepResultStatus[]
Expand All @@ -33,11 +36,13 @@ export default function countScenariosByStatuses(
const pickleIds = gherkinQuery.getPickleIds(gherkinDocument.uri, scenario.id)

pickleIds.forEach((pickleId) => {
const status = getWorstTestStepResult(
cucumberQuery.getPickleTestStepResults([pickleId])
).status

scenarioCountByStatus[status] = scenarioCountByStatus[status] + 1
// if no test case then this pickle was omitted by filtering e.g. tags
if (envelopesQuery.hasTestCase(pickleId)) {
const status = getWorstTestStepResult(
cucumberQuery.getPickleTestStepResults([pickleId])
).status
scenarioCountByStatus[status] = scenarioCountByStatus[status] + 1
}
})
},
}
Expand Down
Loading

0 comments on commit d2b773a

Please sign in to comment.