pelican
pelican

Reputation: 6234

Stop cucumberJs when scenario fails - protractor and cucumberjs

I want to stop cucumber from running a failing test till the end because that wastes time for our continuous integration since we need rapid feedback.

I am using the javascript implementation of cucumber, cucumberJs with protractor.

Here's what I have but doesn't work:

hooks.js

 this.registerHandler('AfterScenario', function (scenario, callback) {
    console.log('\n After each scenario...');
    if (scenario.isFailed() ===true) {
      console.log('\n Scenario failed \n\n\n\n\n\n\n...');
      callback.fail(new Error("\n\n\n\nThis scenario definitely failed!!"));
    }
  });

I included this hook.js in my protractor.conf.js like this:

 cucumberOpts: {
    require: [
      conf.paths.e2e + '/utilities/hooks.js', 
    ],
  }

I know the hook is getting triggered from the error output below; however, can't figure out why or how to fix error:

e #01-1] Specs failed ? : C:\someFeature.feature
[chrome #01-1] Scenario failed! Creating snapshot at: C:\someFeature.png
[chrome #01-1]
[chrome #01-1]
[chrome #01-1]  After each scenario...
[chrome #01-1] TypeError: e2e\utilities\hooks.js:34 scenario.isFailed is not a function
[chrome #01-1]     at C:\Hooks.js:36:18
[chrome #01-1]     at nextTickCallbackWith0Args (node.js:420:9)
[chrome #01-1]     at process._tickCallback (node.js:349:13)

Upvotes: 4

Views: 2055

Answers (2)

kian
kian

Reputation: 55

Solid question - it looks like in cucumber-js 1.3.1 and before, errors raised during these registerHandler hooks will be considered fatal and will kill the test run at the event of failure; It's looking like the 2.0.0-rc.6 has some better handling for registeredHandler errors passed up to the callback in events such as AfterScenario, though setting up 2.0.0 will require some restructuring on the user end in defining their stepdefs.

In order to get around this in my use case with 1.3.1, I've pulled the desired AfterScenario code into a step I'm explicitly adding to the end of the scenario -- A bit bulky.

Upvotes: 0

Ram Pasala
Ram Pasala

Reputation: 5231

If I am correct there is a fail-fast option in cucumber which fails the scenarios if the first one fails. Give it a try-

  cucumberOpts: {
  require: [
  conf.paths.e2e + '/utilities/hooks.js', 
  ],
  format: 'pretty',
  'fail-fast': true
  },

I think the quotes are needed!

Upvotes: 2

Related Questions