How to skip all remaining scenarios in Cucumberjs

Viewed 465

I'm trying to implement some logic to skip the remaining cucumber scenarios if a certain number of tests fail. I'm wondering if there is a method from Cucumberjs that allows you to skip all remaining scenarios in a test run.

2 Answers

There isn't a built-in way of doing this with CucumberJS, but you can use hooks to achieve a similar behavior:

// skip all remaining tests once this number has been reached.
const FAILURE_THRESHOLD = 3;

// a place to track the number of failed scenarios.
// Note: this value cannot be stored in "world" because it must persist between scenarios.
let failCount = 0;

// check if the fail count has been exceeded.
Before(function ({ pickle }) {
    if (failCount >= FAILURE_THRESHOLD) {
        return 'skipped';
    }
});

// increments fail count if the test failed.
After(function ({ pickle, result }) {
    if (result.status.toLowerCase() === 'failed') {
        failCount += 1;
    }
});

Live Demonstration: https://testjam.io/?p=PjYJrTK50FJ375kp3ULv

The Cucumber CLI has a --fail-fast flag you can use. If you're using a conf file like in Courgette, you can add it to cucumberOpts.

If it's after > 1 tests failed you could knock something up like:

After(function (scenario, callback) {
        if (scenario.isFailed()) {
            if (!this.failed) {
              this.failed = 1
            } else {
              this.failed++
            }
            if (this.failed > 3) {
              // terminate the process
            }
        } else {
            callback();
        }
    });

Hope that helps in any way.

Related