4

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)
pelican
  • 5,846
  • 9
  • 43
  • 67

2 Answers2

2

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!

Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • This works Ram; but it stops the suite of tests from executing any further. What I had in mind is this: a) Run all tests in bulk(test suite) but if one of the tests fails, fail-fast i.e stop executing that failing test and move on to the next test in the bulk/suite. b) If yet again, the third test in the suite fails, stop executing it any further to save time and move on to the next test. Right now, your solution stops a failing test from executing any further which is good but it doesn't execute any of the other tests in the suite. Any idea? – pelican Sep 21 '16 at 13:55
0

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.

kian
  • 55
  • 9