Skip to content
Advertisement

Invoke cucumber cli programatically through index.js of node

I have been given an automation framework in CucumberJS and node with selenium. But it has an older version of cucumber which relies on promises. In an attempt to avail latest synchronous step feature, I decided to upgrade the cucumberJS version (1.3.3 to 4.2.1) Now the problem is the code that was used to invoke cucumber cli programmatically in index.js isnt working anymore. I made all the other changes in step definitions and world.js, but I am not able to figure out how do I run this thing via node, something like

node index.js --tags @SampleFeature

This used to work before with the older version but not anymore.

Code that was working before –

// execute cucumber
let cucumberCli = Cucumber.Cli(process.argv);

cucumberCli.run(succeeded => {
  var code = succeeded ? 0 : 1;

 function exitNow() {
 process.exit(code);
  }

 if (process.stdout.write('')) {
   exitNow();
 } else {
   process.stdout.on('drain', exitNow);
  }
 });

Now it throws the error like this after version update

/Users/../node_modules/babel-runtime/helpers/classCallCheck.js:7
    throw new TypeError("Cannot call a class as a function");
    ^

TypeError: Cannot call a class as a function
    at exports.default (/Users/../node_modules/babel-runtime/helpers/classCallCheck.js:7:11)
    at Object.Cli (/Users/../node_modules/cucumber/lib/cli/index.js:78:34)
    at Object.<anonymous> (/Users/../index.js:90:10)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
    at startup (internal/bootstrap/node.js:228:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:576:3)

I googled a lot but did not find an alternative. Tried multiple things as calling Cli as class with new keyword, didn’t work. Tried removing it and running via plain cucumber cli runner, didn’t work.

PS. I come from a Cucumber with Java background where things were simpler 🙂

Advertisement

Answer

You need to create a new CLI object, then use it’s .run method:

let runArgs = ['The cucumber args array here'];
let cliArgs = {argv : runArgs, cwd: process.cwd(), stdout: process.stdout};
let cli = (new require('cucumber').Cli)(cliArgs);

cli.run(); //Returns a promise
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement