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.

Callback form of should chained off cy.window receives null when retried

See original GitHub issue

Current behavior

My goal is to have a retriable check of a window property that my app exposes. To achieve that, I’m using the callback form of should.

The window object is correctly passed into the callback only for the first time. If the assertions fail and trigger a retry, the callback argument is null. (See the repro code)

Maybe I missed something, but I went through the docs and I don’t think this is expected behavior.

image

Desired behavior

The callback of should always receives the window object

Test code to reproduce

it('retries should on window object', () => {
    cy.window().should(win => {
      // this logs <window> on the first run, but null on the next
      cy.log('window =', win).then(() => {
        // failing to trigger a retry
        expect(true).to.equal(false)
      })
    })
})

Cypress Version

10.3.0

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
BlueWindscommented, Jul 13, 2022

Very interesting, thanks for the precise repro. I’m currently working in this area - how subjects are determined and how commands are retried - in relation to https://github.com/cypress-io/cypress/issues/7306.

The short version is that should is not really designed for this use case, and we don’t provide anything that is designed for this use case either. ^^;; I’m aiming to address that in my work. I’m going to assign myself here so I remember to follow up on this specific case.

A temporary workaround would be to ensure that you don’t have any cypress commands inside your should callback:

it.only('retries should on window object', () => {
      let count = 0
      cy.window().should(win => {
        count++
        if (count < 4) { throw new Error('foobar') }
        expect(win).to.haveOwnProperty('location')
      })
    })

This passes; it’s only when the should callback contains other cypress commands that cy gets confused over what the subject should be during retries.

0reactions
cypress-bot[bot]commented, Dec 6, 2022

Released in 12.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v12.0.0, please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

then | Cypress Documentation
The callback function of . then() is not retried. It is unsafe to return DOM elements directly from the callback and then use...
Read more >
Cypress Tips and Tricks - Gleb Bahmutov
readFile retries reading the file until the should(cb) passes // https://on.cypress.io/readfile cy.get('.new-todo') .type('todo A{enter}')
Read more >
API Reference | Vitest
In Jest, TestFunction can also be of type (done: DoneCallback) => void . If this form is used, the test will not be...
Read more >
Node.js v19.3.0 Documentation
Usage; Node-API version matrix; Environment life cycle APIs ... The callbacks will be inherited via the prototype chain:
Read more >
Reactor 3 Reference Guide
Even though the first deployed artifact of a PATCH cycle will ... IDs are retrieved in less than 800ms or, if it takes...
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