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

cypress.clock doesn't work with async await #19620

Closed
shairez opened this issue Jan 7, 2022 · 3 comments
Closed

cypress.clock doesn't work with async await #19620

shairez opened this issue Jan 7, 2022 · 3 comments

Comments

@shairez
Copy link

shairez commented Jan 7, 2022

Current behavior

I have an Angular app which fetches data via ajax.

After the data is being received there is an element showing in the template, and then after a timeout of 5000ms it disappears.

I tried to uses cy.clock to speed things up using cy.tick(6000) but it hangs because the code that should run after the ajax request returns doesn't run.

When I remove the cy.clock everything works good (although delayed).

Desired behavior

I want to be able to use cy.clock and cy.tick with ajax calls in my E2E tests and speed things up.

I believe we might need to add tickAsync option to cypress.

Test code to reproduce

I've uploaded a repository that simulates the issue (instructions are in the README) -

https://github.com/shairez/cypress-clock-timeout-issue

Cypress Version

9.2.0

Other

I think it has to do with the fact it's async await related code.

I noticed sinon (which cy.clock is based on) has a tickAsync method, maybe that could be a good investigation route

@mjhenkes
Copy link
Member

I believe you need to set cy.clock before calling cy.visit.
https://docs.cypress.io/api/commands/clock#Behavior

By making this change in your test case, the test passed.

describe('fetching products', () => {

  it('should fetch products and use fake timers', () => {

+  cy.clock();
    cy.visit('/');
-  cy.clock();
    cy.findByText('Fetch products').click();
    cy.findByText('fetched').should('exist');
    cy.findByText(/You pressed/).should('exist');
    cy.tick(6000);
    cy.findByText(/You pressed/).should('not.exist');

  })
})

@shairez
Copy link
Author

shairez commented Jan 11, 2022

Thanks @mjhenkes !
I verified it as well

This raises another issue (docs related) -

For me, it wasn't clear from the documentation (I read it a few times before I opened this issue) that this is a viable solution to my problem because my setTimeout doesn't run automatically on page load, it runs after I emit an event (click the button).

Are you saying that as a requirement cy.clock should always be called before cy.visit ?

if so, I would put it under "rules" and not "behavior"

If not, why does it work in this specific case then?

Thanks again!

shairez referenced this issue in shairez/cypress-clock-timeout-issue Jan 21, 2022
@sainthkh
Copy link
Contributor

Closing because of the inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants