Skip to content

Commit

Permalink
Fail the step rather than Cucumber when a parameter transform fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Feb 18, 2017
1 parent d7642f0 commit e5f317e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
41 changes: 32 additions & 9 deletions features/custom_parameter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,66 @@ Feature: Custom Parameter
"""
Feature: a feature
Scenario: a scenario
Given a passing step
Given a generic step
"""

Scenario: custom parameter
Given a file named "features/step_definitions/passing_steps.js" with:
Given a file named "features/step_definitions/parameterized_steps.js" with:
"""
import assert from 'assert'
import {defineSupportCode} from 'cucumber'
defineSupportCode(({Given, addParameter}) => {
addParameter({
captureGroupRegexps: /passing|failing|undefined|pending/,
captureGroupRegexps: /generic|specific/,
transformer: s => s.toUpperCase(),
typeName: 'status'
})
Given('a {status} step', function(status) {
assert.equal(status, 'PASSING')
assert.equal(status, 'GENERIC')
})
})
"""
When I run cucumber.js
Then the step "a passing step" has status "passed"
Then the step "a generic step" has status "passed"

Scenario: custom parameter (legacy API)
Given a file named "features/step_definitions/passing_steps.js" with:
Given a file named "features/step_definitions/parameterized_steps.js" with:
"""
import assert from 'assert'
import {defineSupportCode} from 'cucumber'
defineSupportCode(({Given, addTransform}) => {
addTransform({
captureGroupRegexps: /passing|failing|undefined|pending/,
captureGroupRegexps: /generic|specific/,
transformer: s => s.toUpperCase(),
typeName: 'status'
})
Given('a {status} step', function(status) {
assert.equal(status, 'PASSING')
assert.equal(status, 'GENERIC')
})
})
"""
When I run cucumber.js
Then the step "a passing step" has status "passed"
Then the step "a generic step" has status "passed"

Scenario: custom transform throwing exception
Given a file named "features/step_definitions/parameterized_steps.js" with:
"""
import assert from 'assert'
import {defineSupportCode} from 'cucumber'
defineSupportCode(({Given, addParameter}) => {
addParameter({
captureGroupRegexps: /generic|specific/,
transformer: s => { throw new Error(`the parameter transform of [${s}] failed`) },
typeName: 'status'
})
Given('a {status} step', function(status) {
throw new Error('should never get here')
})
})
"""
When I run cucumber.js
Then it fails
And the step "a generic step" failed with:
"""
the parameter transform of [generic] failed
"""
35 changes: 20 additions & 15 deletions src/runtime/step_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ const {beginTiming, endTiming} = Time

async function run({attachmentManager, defaultTimeout, scenarioResult, step, stepDefinition, parameterRegistry, world}) {
beginTiming()
const parameters = stepDefinition.getInvocationParameters({scenarioResult, step, parameterRegistry})
const timeoutInMilliseconds = stepDefinition.options.timeout || defaultTimeout

let error, result
const validCodeLengths = stepDefinition.getValidCodeLengths(parameters)
if (_.includes(validCodeLengths, stepDefinition.code.length)) {
const data = await UserCodeRunner.run({
argsArray: parameters,
fn: stepDefinition.code,
thisArg: world,
timeoutInMilliseconds
})
error = data.error
result = data.result
} else {
error = stepDefinition.getInvalidCodeLengthMessage(parameters)

try {
const parameters = stepDefinition.getInvocationParameters({scenarioResult, step, parameterRegistry})
const timeoutInMilliseconds = stepDefinition.options.timeout || defaultTimeout

const validCodeLengths = stepDefinition.getValidCodeLengths(parameters)
if (_.includes(validCodeLengths, stepDefinition.code.length)) {
const data = await UserCodeRunner.run({
argsArray: parameters,
fn: stepDefinition.code,
thisArg: world,
timeoutInMilliseconds
})
error = data.error
result = data.result
} else {
error = stepDefinition.getInvalidCodeLengthMessage(parameters)
}
} catch(err) {
error = err
}

const attachments = attachmentManager.getAll()
Expand Down

0 comments on commit e5f317e

Please sign in to comment.