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.

_currentRetry value in beforeEach hook is always 0

See original GitHub issue

The issue

We use a command to reset the database before each spec is run. Unfortunately, it is quite slow (takes about 7 seconds to reset DB fixtures), so we can’t really execute it in a beforeEach hook as that would slow down the tests significantly. However, if a test fails mid-execution, it can make some changes in the database that will prevent the test from retrying. I wanted to make a workaround where i would execute the DB reset command in a beforeEach hook if the current attempt counter is above 0, but that didn’t work (see current behavior below). If there is any other way to make this work, please let me know.

Current behavior

When accessing attempt counter in the beforeEach hook (as described here), the attempts counter is 0 regardless of actual attempt count.

изображение

Desired behavior

Since the beforeEach hook is executed for each test attempt, i expected the attempt counter to contain current attempt counter.

Test code to reproduce

describe('Home page', () => {
    beforeEach(() => {
        console.log('---Start new attempt---');
        const attempt = cy.state('runnable')._currentRetry;
        console.log('Attempt count in "beforeEach": ' + attempt);
    });
    it.only('Opens homepage', {
        retries: {
            openMode: 1,
        },
    }, () => {
        cy.visit('https://github.com');
        cy.contains('not github');
        const attempt = cy.state('runnable')._currentRetry;
        console.log('Attempt count in test: ' + attempt);
    });
});

Versions

6.3.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
MDjhowleycommented, Feb 18, 2021

@adesko I have testing using this solution and it seems to work: https://github.com/cypress-io/cypress/issues/8404#issuecomment-680122825

perhaps the documentation should be expanded to reference this? FYI cy.state has open ticket for documentation… (https://github.com/cypress-io/cypress-documentation/issues/108)

0reactions
sainthkhcommented, May 4, 2021

Sorry for the late reply. Closing in favor of #8404.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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; throw ......
Read more >
Test Retries - Cypress Documentation
The above attempt variable will have values 0 through 3 (the first default test execution plus three allowed retries). The retries constant in...
Read more >
Skip Before Each Hook - Gleb Bahmutov
So the beforeEach hook code can find the test configuration values inside the this.currentTest._testConfig.unverifiedTestConfig object. If the ...
Read more >
runner.js - Documentation - Mocha
suite');; var HOOK_TYPE_BEFORE_EACH = Suite.constants. ... State set to this value when the Runner has started running; */; STATE_RUNNING: 'running', ...
Read more >
mocha/mocha.js - chromium/src/third_party - Git at Google
var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; ... IE10 has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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