2

I couldn't be able to find any good explanation of how the method works exactly and how it could be useable. In the documentation I found the description:

setDefinitionFunctionWrapper(fn, options)

Set a function used to wrap step / hook definitions. When used, the result is wrapped again to ensure it has the same length of the original step / hook definition. options is the step specific wrapperOptions and may be undefined.

I'm not experienced programmer and I do not understand what "wrapping" means in this context. I'd be glad if someone will explain the subject more effectively

Community
  • 1
  • 1
mathsicist
  • 181
  • 4
  • 15

3 Answers3

1

I tried using the snippet Jorge Chip posted and It does not work. You should use this instead:

const {setDefinitionFunctionWrapper} = require('cucumber');
        
setDefinitionFunctionWrapper(function(fn){
  if(condition){//you want to do before and after step stuff
    return async function(){
      //do before step stuff
      await fn.apply(this, arguments)
      //do after step stuff
      }
  }
  else{//just want to run the step
    return fn
  }
}

in the snippet he posted he used args which will not work he also used await inside of a non async function which will also not work

Gilad Shnoor
  • 374
  • 3
  • 12
0

A wrapper function is a subroutine in a software library or a computer program whose main purpose is to call a second subroutine or a system call with little or no additional computation.

Usually programmers wrap functions with another function to do extra small actions before or after the wrapped function.

myWrappedFunction () { doSomething }
myWrapper () { doSmallBits; myWrappedFunction(); doSmallBits; }

(1) https://en.wikipedia.org/wiki/Wrapper_function


Digging into CucumberJS, setDefinitionFunctionWrapper is a function that will be called on every step definition and should return the function you want to execute when that step is called.

An example of a dummy setDefinitionFunctionWrapper would be something like:

setDefinitionFunctionWrapper(function (fn, opts) {
  return await fn.apply(this, args);
}
Jorge Chip
  • 81
  • 3
  • 9
0

As of cucumber 2.3.1. https://github.com/cucumber/cucumber-js/blob/2.x/src/support_code_library/builder.js

After you install the cucumber library, see the source code in /node_modules/cucumber/lib/support_code_library/builder.js

At the line 95:

  if (definitionFunctionWrapper) {
    definitions.forEach(function (definition) {
      var codeLength = definition.code.length;
      var wrappedFn = definitionFunctionWrapper(definition.code, definition.options.wrapperOptions);
      if (wrappedFn !== definition.code) {
        definition.code = (0, _utilArity2.default)(codeLength, wrappedFn);
      }
    });
  } else {
...

definitionFunctionWrapper bascially takes code(fn) and opts and return the new code(fn).

Understanding this piece of code we can make this function in our step file:

var { setDefinitionFunctionWrapper } = require('cucumber');

// Wrap around each step
setDefinitionFunctionWrapper(function (fn, opts) {
    return function() {
        console.log('print me in each step');
        return fn.apply(this, arguments);
    };
});
Zihao Zhao
  • 439
  • 5
  • 9