Closed lukeapage closed 4 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.
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);
@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()
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?
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
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() ?