badeball / cypress-cucumber-preprocessor

Run cucumber/gherkin-syntaxed specs with Cypress
MIT License
1.32k stars 147 forks source link

IParameterTypeDefinition should have parameter "transformer" set to optional #1179

Closed hammzj closed 5 months ago

hammzj commented 5 months ago

Current behavior

https://github.com/badeball/cypress-cucumber-preprocessor/blob/055d8df6a62009c94057b0d894a30e142cb87b94/lib/public-member-types.ts#L3-L7

transformer is required, but should be optional. If a transformer is not provided for a parameter type that returns a single capture group, then it will return the original value as a string.

However, if a parameter type has two capture groups, it needs a transformer that returns all arguments, or else it only returns only the first found argument in a capture group. That, however, does not require a transformer as the code will still execute.

Desired behavior

Make transformer be optional:

export interface IParameterTypeDefinition<T, C extends Mocha.Context> {
  name: string;
  regexp: RegExp;
  transformer?: (this: C, ...match: string[]) => T;
}

Test code to reproduce

defineParameterType({
    name: "threeChoices",
    regexp: /(now|later|maybe)/
});

defineParameterType({
    name: "someNumber",
    regexp: /(\d+)/
});

Given('call me {threeChoices}', function(choice) {
  cy.log(choice, typeof choice) // => string
});

Given('number of {someNumber}', function(x) {
  cy.log(x, typeof x) // => string
});
Feature: Demo

  Scenario Outline: Test
    * call me <choice>
    * number of 2
    * number of 3.14
    Examples:
      | choice |
      | now    |
      | later  |
      | maybe  |

Versions

Checklist

hammzj commented 5 months ago

See constructor: https://github.com/cucumber/cucumber-expressions/blob/b4d5d7025ecfe7405b5235bade7acce1d827b30b/javascript/src/ParameterType.ts#L45-L62

This also supports type to directly define the type, but not sure if needed.

badeball commented 5 months ago

Hi @hammzj, I've fixed with this v20.0.4.