-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to retry beforeAll(before) hook #19458
Comments
In the before hook, we want to set up our test data in the backend. If something went wrong in the test case, we need to restore the test data before we start again. |
I have a similar issue. I'm looking into writing something to retry the before hook myself but it would be extremely helpfull if this could be added in future versions! |
@SenneVProceedix I wrote a little workaround. See my answer here: https://stackoverflow.com/questions/71285827/cypress-e2e-before-hook-not-working-on-retries/71377694#71377694 |
Hi @Michel73 Thank you for the information. Do you have any information in regards to (safely) retrying the before hook? |
This is kinda horrible and hacky but technically you could get the mocha runner from a beforeEach hook and if it's a retry call any before hook functions again... I use the cypress cucumber preprocessor plugin, so some of this is a bit specific but... In
|
@SenneVProceedix Sorry but I haven't any further information about this. |
/**
* A `before()` alternative that gets run when a failing test is retried.
*
* By default cypress `before()` isn't run when a test below it fails
* and is retried. Because we use `before()` as a place to setup state
* before running assertions inside `it()` this means we can't make use
* of cypress retry functionality to make our suites more reliable.
*
* https://github.com/cypress-io/cypress/issues/19458
* https://stackoverflow.com/questions/71285827/cypress-e2e-before-hook-not-working-on-retries
*/
export const retryableBefore = (fn) => {
let shouldRun = true;
// we use beforeEach as cypress will run this on retry attempt
// we just abort early if we detected that it's already run
beforeEach(() => {
if (!shouldRun) return;
shouldRun = false;
fn();
});
// When a test fails we flip the `shouldRun` flag back to true
// so when cypress retries and runs the `beforeEach()` before
// the test that failed, we'll run the `fn()` logic once more.
Cypress.on('test:after:run', (result) => {
if (result.state === 'failed') {
if (result.currentRetry < result.retries) {
shouldRun = true;
}
}
});
}; Use in place of describe('my suite', () => {
retryableBefore(() => {
// reset database and seed with test data …
cy.visit('/some/page');
});
it('my test 2', () => {
…
});
it('test 2', () => {
…
});
describe('my suite', () => {
retryableBefore(() => {
// do something in ui
});
it('my test 3', () => {
…
});
it('test 4', () => {
…
});
});
}); If any of the tests fail and you have |
That's a lot cleaner than my mess |
@wilsonpage what about cases where the function placed in the before all hook fails, but causes the the condition variable to update boolean value? I'm doing something similar with my test. It works if another beforeEach/afterEach or the test itself fails, but from local tests against the before itself might cause user test errors. Work around that would be to place the condition varible in a resolve or then block to promise to be executed only after your hook fully executed and didn't retry after. |
Hi, in which file write this function retryableBefore? |
@stokrattt it doesn't need to be in any special file, it's just a simple JS function, you can place it anywhere and |
@wilsonpage thanks, but I have currentRetry is not defined in the runnable.currentRetry. Please help |
related to: #17321 |
We ran also into the issue and thanks to your solution it works now. |
i have this problem too |
Due to various API and database limitations, we often need to use the UI to configure test state. This is expensive enough that we don't want to do it before every test, so we have been using So, we're left with a choice:
None of those are very good options, but number 3 is clearly the best of the bunch and is what we will employ until something better comes along. |
I'm impressed that there are 2 issues about the need of this feature, they are at least 2 and a half years old, with comments per year, and yet we don't have a solution from The issues:
|
Is there anyway of doing this for the "after" hook? |
What would you like?
Copied across from kuceb/cypress-plugin-retries#50
If there was an option to retry the beforeAll(before) hook when it fails that would be great.
Why is this needed?
Currently if the
before
hook in any test suite fails, the whole E2E fails and we have to manually trigger the E2E again. This is a pain.Other
No response
The text was updated successfully, but these errors were encountered: