question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to retry beforeAll(before) hook

See original GitHub issue

What would you like?

_Copied across from https://github.com/Bkucera/cypress-plugin-retries/issues/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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:21
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
wilsonpagecommented, Apr 21, 2022
/**
 * 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 before():

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 retries config set, Cypress will re-run the retryableBefore() block as expected.

0reactions
emilyrohrboughcommented, Nov 17, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Option to retry beforeAll(before) hook · Issue #50 - GitHub
We run this command in the beforeAll(before) hook and then run our tests. If there was an option to retry the beforeAll(before) hook...
Read more >
Cypress E2E: Before hook not working on retries
As a workaround you can use a beforeEach: let isError = false; beforeEach(() => { cy. once('fail', (err) => { isError = true;...
Read more >
Test Retries - Cypress Documentation
However, failures in before and after hooks will not trigger a retry. The following is a detailed step-by-step example of how test retries...
Read more >
Retry Flaky Tests - WebdriverIO
You can rerun certain tests with the WebdriverIO testrunner that turn out to be unstable due to things like a flaky network or...
Read more >
Chapter 5 - Retry and Timeouts - Test Automation University
Retry is a function available within Mocha to execute the failed test several times. ... Please note that retry will not work with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found