Skip to content
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

Incorrect attempts in after:run hook with retries enabled #30794

Closed
csvtuda opened this issue Dec 20, 2024 · 3 comments
Closed

Incorrect attempts in after:run hook with retries enabled #30794

csvtuda opened this issue Dec 20, 2024 · 3 comments
Labels
type: duplicate This issue or pull request already exists

Comments

@csvtuda
Copy link

csvtuda commented Dec 20, 2024

Current behavior

When retries are enabled and a test is flaky (i.e. fails at the beginning, then passes in a later retry), the results reported in the "after:run" hook contain duplicate attempts.

describe('page', () => {
  it('works', () => {
    cy.visit('https://example.cypress.io');
    // simulate a flaky test that passes on the third attempt
    if (Cypress.currentRetry < 2) {
      expect(true).to.eq(false)
    } else {
      expect(true).to.eq(true)
    }
  })
})

Config file:

const { writeFileSync } = require("node:fs");

module.exports = {
  e2e: {
    setupNodeEvents(on, config) {
      on("after:run", (results) => {
        writeFileSync("results.json", JSON.stringify(results, null, 2));
      });
    },
    retries: 2
  },
}

The results reported:

"tests": [
  {
    "attempts": [
      {
        "state": "failed"
      },
      {
        "state": "failed"
      },
      {
        "state": "failed"
      },
      {
        "state": "failed"
      },
      {
        "state": "passed"
      }
    ],
    "displayError": null,
    "duration": 1513,
    "state": "passed",
    "title": [
      "page",
      "works"
    ]
  }
],

Desired behavior

In this situation, Cypress should only report two failing and one passing attempt.

"tests": [
  {
    "attempts": [
      {
        "state": "failed"
      },
      {
        "state": "failed"
      },
      {
        "state": "passed"
      }
    ],
    "displayError": null,
    "duration": 1489,
    "state": "passed",
    "title": [
      "page",
      "works"
    ]
  }
],

Test code to reproduce

  1. clone repository with buggy setup: https://github.com/csvtuda/cypress-attempt-bug
  2. npm install
  3. npx cypress run
  4. inspect results.json

Cypress Version

13.17.0

Node version

v22.11.0

Operating System

Windows 10 Pro 19045.5247

Debug Logs

No response

Other

Interestingly, this does not occur when the test does not contain any Cypress command:

describe('page', () => {
  it('works', () => {
    // no cypress visit, but still flaky
    if (Cypress.currentRetry < 2) {
      expect(true).to.eq(false)
    } else {
      expect(true).to.eq(true)
    }
  })
})

The reported results:

"tests": [
  {
    "attempts": [
      {
        "state": "failed"
      },
      {
        "state": "failed"
      },
      {
        "state": "passed"
      }
    ],
    "displayError": null,
    "duration": 1489,
    "state": "passed",
    "title": [
      "page",
      "works"
    ]
  }
],

I hope this is not a duplicate issue, I failed to find anything using GitHub's search.

@jennifer-shehane
Copy link
Member

I feel like this might be related to this issue: #2777 Do the double state go away if a baseUrl is set - and that baseUrl is visiting in the cy.visit?

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Dec 20, 2024
@csvtuda
Copy link
Author

csvtuda commented Dec 20, 2024

Yes, defining a baseUrl and visiting it through / fixes the issue.

@jennifer-shehane
Copy link
Member

Yah this is not a great side effect of that. I'll add this case to that existing issue and close this as a duplicate.

@jennifer-shehane jennifer-shehane added type: duplicate This issue or pull request already exists and removed stage: needs information Not enough info to reproduce the issue labels Dec 20, 2024
@jennifer-shehane jennifer-shehane closed this as not planned Won't fix, can't repro, duplicate, stale Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants