Search code examples
angularcucumbercypressgherkincypress-cucumber-preprocessor

How do I make transformation work using badeball/cypress-cucumber-preprocessor library?


I've spent many hours on it already and it doesn't work as intended. Here's one of attempts to make it clear:

// something.feature
Feature: asdf
  Scenario: asdf asdf
    # any of steps below called here gives the same error
    # When something "second parameter"
    When something option1 "second parameter"
    # When something secondOption "second parameter"
// steps.ts
When('something {OptionType} {string}', (
  firstParameter: OptionType, 
  secondParameter: string) => {
    // todo
});
// transformations.ts
import { defineParameterType } from '@badeball/cypress-cucumber-preprocessor';

export enum OptionType {
  defaultOption,
  option1,
  secondOption,
  blablabla,
}

defineParameterType({
  name: 'OptionType',
  regexp: /^(option1|secondOption|blablabla)?$/,
  transformer: (value: string) => {
    let result = OptionType.defaultOption;
    switch(value) {
      case "":
        result = OptionType.defaultOption;
        break;
      case "option1":
        result = OptionType.option1;
        break;
      case "secondOption":
        result = OptionType.secondOption;
        break;
      case "blablabla":
        result = OptionType.blablabla;
        break;
      default:
        throw new Error("No suitable option");
    }
    return result;
  },
});

So what I want is that I want first parameter to be optional so that if it's called as first call in feature file firstParameter has value OptionType.defaultOption and if there's some other option, then it has corresponding enum value. I want to change second parameter to a class object later but I think that if I accomplish this enum thing - I'll handle class as well.

All my attempts failed so far. Right now it says that the step is missing implementation. When I tried to change {OptionType} to (.*) in the step definition - it gives the same error.

Any ideas on what's wrong here?

I'm using these versions: "@angular/common": "15.1.1", "@nrwl/cypress": "15.5.2", "@badeball/cypress-cucumber-preprocessor": "^15.1.4", "nx": "15.5.2".

Also path is not default cypress/.... It's placed inside a separate app. Not sure, but probably that could influence something as well? I've imported step definitions and transformations so they are definitely not missing right now for cypress. But probably something else does?

Here's a documentation on this topic but it seems that it's missing a more complete example or it's not designed to be used with nx and in a different folder then default cypress folder or everything above.

Also please note that "cypress-cucumber-preprocessor" library is an old outdated one. Currently ownership has changed and latest up to date library is "badeball/cypress-cucumber-preprocessor". So when you write an answer please make sure it's not referring to the old one because it might not work any more.

P.S. This is not 100% actual code from my project. I've changed things to make sure I don't violate NDA.


Solution

  • It turned out that the root cause of this error were ^ and $ characters in the regex of OptionType in the defineParameterType call. Deleting them makes everything work.

    Call of the step without specifying the first parameter still requires editing (like overloading of the step or removing 1 space from step definition string and adding a space instead to every option in OptionType regex) but it starts working.

    My best guess is that "{OptionType}" part of the regex that goes as first parameter to When function is replaced by OptionType regex and as result there is this regex:

    something ^(option1|secondOption|blablabla)?$ ".*"

    So evantually since there are characters "^" and "$" in the middle of a regular expression - there's no way any string would match that. It's because those characters mean start and end of the string and they are placed in the middle.

    So the result regex has to be like this to work:

    something (option1|secondOption|blablabla)? ".*"

    and regex parameter of defineParameterType has to be like this:

    regexp: /(option1|secondOption|blablabla)?/,