Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1621: Json formatter fails with tagged examples #1651

Merged
merged 10 commits into from
Apr 26, 2021
23 changes: 23 additions & 0 deletions features/scenario_outlines.feature
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,26 @@ Feature: Scenario Outlines and Examples
When I run cucumber-js
Then it fails
And it runs 4 scenarios

Scenario: scenario outlines with tagged examples
Given a file named "features/scenario_outline.feature" with:
"""
Feature: a feature
Scenario Outline: a scenario
Given a step <id>

@tag
aurelien-reeves marked this conversation as resolved.
Show resolved Hide resolved
Examples:
| id |
| 1 |
| 2 |
"""
And a file named "features/step_definitions/cucumber_steps.js" with:
"""
const {Given} = require('@cucumber/cucumber')

Given('a step {int}', function(int) {})
"""
When I run cucumber-js with `-f json`
Then it passes
And it runs 2 scenarios
44 changes: 29 additions & 15 deletions src/formatter/json_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ITag = messages.GherkinDocument.Feature.ITag
import IFeature = messages.GherkinDocument.IFeature
import IPickle = messages.IPickle
import IScenario = messages.GherkinDocument.Feature.IScenario
import IPickleTag = messages.Pickle.IPickleTag
import IEnvelope = messages.IEnvelope
import IRule = messages.GherkinDocument.Feature.FeatureChild.IRule

Expand Down Expand Up @@ -317,20 +318,33 @@ export default class JsonFormatter extends Formatter {
pickle: IPickle
gherkinScenarioMap: { [id: string]: IScenario }
}): IJsonTag[] {
return _.map(pickle.tags, (tagData) => {
const featureSource = feature.tags.find(
(t: ITag) => t.id === tagData.astNodeId
)
const scenarioSource = gherkinScenarioMap[pickle.astNodeIds[0]].tags.find(
(t: ITag) => t.id === tagData.astNodeId
)
const line = doesHaveValue(featureSource)
? featureSource.location.line
: scenarioSource.location.line
return {
name: tagData.name,
line,
}
})
const scenario = gherkinScenarioMap[pickle.astNodeIds[0]]

const asJsonTag = (tagData: IPickleTag): IJsonTag =>
aurelien-reeves marked this conversation as resolved.
Show resolved Hide resolved
this.getScenarioTag(tagData, feature, scenario)

return pickle.tags.map(asJsonTag)
}

getScenarioTag(
aurelien-reeves marked this conversation as resolved.
Show resolved Hide resolved
tagData: IPickleTag,
feature: IFeature,
scenario: IScenario
): IJsonTag {
const byAstNodeId = (tag: ITag): Boolean => tag.id === tagData.astNodeId
const flatten = (acc: ITag[], val: ITag[]): ITag[] => acc.concat(val)

const tag =
feature.tags.find(byAstNodeId) ||
scenario.tags.find(byAstNodeId) ||
scenario.examples
.map((e) => e.tags)
.reduce(flatten, [])
.find(byAstNodeId)

return {
name: tagData.name,
line: tag?.location?.line,
}
}
}
58 changes: 58 additions & 0 deletions src/formatter/json_formatter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../test/fixtures/json_formatter_steps'
import FakeTimers, { InstalledClock } from '@sinonjs/fake-timers'
import timeMethods from '../time'
import { IJsonFeature, IJsonScenario } from '../../lib/formatter/json_formatter'

describe('JsonFormatter', () => {
let clock: InstalledClock
Expand Down Expand Up @@ -357,6 +358,63 @@ describe('JsonFormatter', () => {
])
})
})

describe(' with tagged examples', () => {
it('outputs the examples', async () => {
// Arrange
const sources = [
{
data: [
'Feature: my feature',
' Scenario: my scenario',
' Given a step <id>',
'',
' @tag-1-2',
' Examples:',
' |id|',
' | 1|',
' | 2|',
'',
' @tag @tag-3-4',
' Examples:',
' |id|',
' | 3|',
' | 4|',
].join('\n'),
uri: 'a.feature',
},
]

const supportCodeLibrary = getJsonFormatterSupportCodeLibrary(clock)

// Act
const output = await testFormatter({
sources,
supportCodeLibrary,
type: 'json',
})

// Assert
const jsonFeature: IJsonFeature = JSON.parse(output)[0]
const jsonScenarios = jsonFeature.elements
const jsonScenarioTags = jsonScenarios.map((s: IJsonScenario) => s.tags)

const expectedTags = [
[{ line: 5, name: '@tag-1-2' }],
[{ line: 5, name: '@tag-1-2' }],
[
{ line: 11, name: '@tag' },
{ line: 11, name: '@tag-3-4' },
],
[
{ line: 11, name: '@tag' },
{ line: 11, name: '@tag-3-4' },
],
]

expect(jsonScenarioTags).to.eql(expectedTags)
})
})
})

describe('one rule with several examples (scenarios)', () => {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/json_formatter_steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export function getJsonFormatterSupportCodeLibrary(
Given('a step that attaches', async function (this: World) {
await this.attach(Buffer.from([137, 80, 78, 71]), 'image/png')
})

Given('a step {int}', function (_int: Number) {})
})
}

Expand Down