kuceb / cypress-plugin-retries

A Cypress plugin to retry failed tests.
MIT License
237 stars 20 forks source link

retry afterEach #7

Closed lukeapage closed 4 years ago

lukeapage commented 5 years ago

Thanks for the plugin - a great step forward - it seems overall to work for us, except.. we have some assertions in afterEach which cause failures.. and we want to retry.

Are there any plans to add support for this? Or any ideas to always run an assertion on every test other than calling a custom command in every it() ?

kuceb commented 5 years ago

@lukeapage I have only ever used an AfterEach for teardown code, but I understand why you would want an assertion there. I'll give this some thought.

You could try proxying the it method like so:

      describe.only('some suite', () => {

        const itWithTest = (fn) => (...args) => {
          it(args[0], () => {
            args[1]()
            fn()
          })
        }

        const it_ = itWithTest(() => {
          expect('foo').ok
        })

        it_('can test', () => {
          expect(true).ok

        })

        it_('can test 2', () => {
          expect(true).ok
        })

      })

but then you'll lose calls to .only/.skip unless you add more complexity to the itWithTest function.

lukeapage commented 5 years ago

Thanks for the work-around! I can apply this patching to the global it since thats where I previously had the afterEach..

function afterEachCode() {
    // custom test code
    expect('foo').ok
}

const _it = it;

const makeItFn = (it) => (name, fn) => {
    // internal implementation is to use it without a fn for skips
    if (!fn) {
        it(name);
        return;
    }

    it(name, () => {
        fn();
        afterEachCode();
    });
};

// eslint-disable-next-line no-global-assign
it = makeItFn(_it);
it.only = makeItFn(_it.only);
it.skip = makeItFn(_it.skip);
kuceb commented 5 years ago

@lukeapage sounds like a good solution. I would also recommend putting a Cypress.log in there like so, to quickly identify where a failure is.

fn()
Cypress.log({type:'afterEach'})` 
afterEachCode()
kylane commented 4 years ago

I can apply this patching to the global it since thats where I previously had the afterEach..

Would love to get more info on how to implement this?

kuceb commented 4 years ago

afterEach hooks are retried as part of test retries added in Cypress 5.0. https://docs.cypress.io/guides/references/migration-guide.html#Tests-retries