Skip to content

Instantly share code, notes, and snippets.

@iamkenos
Forked from jbpros/my.feature
Created May 23, 2021 09:51
Show Gist options
  • Save iamkenos/c42546239c0d9ae393403c4c00d52a61 to your computer and use it in GitHub Desktop.
Save iamkenos/c42546239c0d9ae393403c4c00d52a61 to your computer and use it in GitHub Desktop.
Cucumber.js step definition transforms
# features/my.feature
Feature:
Scenario: cukes
Given I have 10 cucumbers in my BaG
Given I don't have 20 cucumbers in my bAg
Given I have 98775655 cucumbers in my bag
// features/step_definitions/stepdefs.js
var transform = require('./support/transform.js');
module.exports = function () {
this.Transform(/^(\d+)$/, function (matches) { return parseInt(matches[0]); });
this.Transform(/^(have|don't have)$/, function (matches) { return matches[0] == 'have'; });
this.Transform(/^([bB][aA][gG])$/, function () { return 'bag'; });
this.Given(/^I (have|don't have) (\d+) cucumbers in my (.*)$/, function (has, count, where) {
console.log('Given...', has, typeof count, where)
});
}
// features/support/transform.js
var transforms = [];
function cast(value) {
for (var i = 0; i < transforms.length; i++) {
var transform = transforms[i];
var matches = transform.pattern.exec(value);
if (matches) {
return transform.fn(matches);
}
}
return value;
}
function t(fn) {
var args = [];
var body = 'var args = [];';
for (var i = 0; i < fn.length; i++) {
args.push('arg' + i);
body += 'args.push(this.cast(arg' + i + '));\n';
}
body += 'return this.fn.apply(this, args);';
var transformedFn = Function.apply(null, args.concat([body])).bind({fn: fn, cast: cast});
return transformedFn;
}
module.exports = function () {
var _defineStep = this.defineStep;
this.defineStep = function (pattern, fn) {
_defineStep(pattern, t(fn));
};
this.Given = this.When = this.Then = this.defineStep;
this.Transform = function (pattern, fn) {
transforms.push({ pattern: pattern, fn: fn });
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment