I need to move 'beforeEach' and 'afterEach' from spec to global config WDIO [mocha, hook, wdio]

958 views Asked by At

I use WebdriverIO for E2E testing with mocha framework

When I run tests, I want that after the first error, all subsequent checks in this test were skipped, and Mocha went to the next test file

as an example, I showed the code below which needs to be taken out from each test file and applied globally.


I need to move beforeEach and afterEach from spec to global config WDIO [mocha, hook, wdio]

I need to move code from spec to global config WDIO

The main goal is to skip the tests after though the test in it has failed

import { expect } from 'chai';

describe('Verify next it is skipped', function () {
    let skipNextIt = false;
    beforeEach(function () {
        if (skipNextIt) {
            this.skip();
        }
    });

    afterEach(function() {
        if(this.currentTest.state === 'failed') {
            skipNextIt = true;
        }
    });

    it('is should pass', function () {
        expect(true).to.equal(true);
    });

    it('is should fail', function () {
        expect(true).to.equal(false);
    });

    it('is should skipp 1', function () {
        expect(true).to.equal(true);
    });

    it('is should skipp 2', function () {
        expect(true).to.equal(true);
    });
});
1

There are 1 answers

1
Benedikt Kromer On

Are you looking for bail(mocha doc)?

Once a test within a spec fails, exit the spec.

FYI: add next time your WDIO version.