kfairns
kfairns

Reputation: 3057

Cucumber Expressions with CucumberJS 2.0.0+

Background

I'm trying my best to figure out CucumberJS 2.0.0, quite a bit has changed since 1.3.0.

Cucumber Expressions can be used in step definitions to replace Regular Expressions:

When(/^I search for "{searchTerm}"$/, function (searchTerm) {

Instead of:

When(/^I search for "([^"]*)"$/, function (searchTerm) {

We'd like to use the Cucumber Expressions that seem to be part of CJS2, to make our step definitions more readable (in some places we have quite a bit of regex that would baffle a beginner) and we can't figure out how to do it.

The Problem

With reference to the documentation for Cucumber Expressions, all seems fine, but the ParameterRegistry class is undefined within my code.

ParameterRegistry is not a constructor

Basically I'm wondering if anyone in the CucumberJS community has managed to get this to work and would help me get started with the Cucumber Expressions.

Upvotes: 1

Views: 646

Answers (1)

kfairns
kfairns

Reputation: 3057

I found a way to do it:

When('I search for {searchTerm:stringInDoubleQuotes}', function (searchTerm) as an example.

  1. It won't take a regex, so it has to be a string
  2. You can't have a space between the argument and the parameter type.

To make your own, use the addTransform function.

An example:

defineSupportCode(function({addTransform}){

    addTransform({
        captureGroupRegexps: ['\\d+(?:|\\.\\d{2})'],
        transformer: JSON.parse,
        typeName: 'money'
    });
});

Upvotes: 2

Related Questions