Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

retry afterEach #7

Closed
lukeapage opened this issue Mar 24, 2019 · 5 comments
Closed

retry afterEach #7

lukeapage opened this issue Mar 24, 2019 · 5 comments
Labels

Comments

@lukeapage
Copy link

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
Copy link
Owner

kuceb commented Mar 25, 2019

@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
Copy link
Author

lukeapage commented Mar 25, 2019

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 kuceb changed the title afterEach failure retry afterEach Mar 25, 2019
@kuceb
Copy link
Owner

kuceb commented Mar 25, 2019

@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()

@kuceb kuceb added the cantfix label Oct 10, 2019
@kylane
Copy link

kylane commented Jun 26, 2020

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
Copy link
Owner

kuceb commented Aug 25, 2020

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

@kuceb kuceb closed this as completed Aug 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants