dmtrKovalenko / cypress-real-events

Fire native system events from Cypress.
MIT License
755 stars 67 forks source link

Hover state is preserved between tests although Test Isolation is active #626

Closed sebbayer closed 6 months ago

sebbayer commented 7 months ago

Since Cypress 12.0.0 the new Option testIsolation is enabled by default, so cookies, cache, localStorage and so on should be reset between tests. Also you have to call cy.visit() at the beginning of each test now because it does not stay on the page between tests anymore.

https://docs.cypress.io/guides/core-concepts/test-isolation

When one Cypress test sets the mouse position via .realHover(), the element is still hovered in the following test(s). So following assertions may fail. I think this behaviour is unexpected.

Proof of concept would be something like:

<style>
  a { text-decoration-line: underline; }
  a:hover { text-decoration-line: none; }
</style>

<a href="test">test</a>

And in the test:

it('calls realHover', () => {
  cy.visit('/')
  cy.get('a').eq(0).realHover()
})

it('should not hover the link anymore', () => {
  cy.visit('/')
  // This fails because the link is still hovered. The underline is also not visible in the Cypress window.
  cy.get('a').eq(0).should('have.css', 'text-decoration-line', 'underline')
})

A workaround would be something like this at the end of the first test:

cy.get('body').realMouseMove(0, 0)
dmtrKovalenko commented 6 months ago

https://github.com/dmtrKovalenko/cypress-real-events?tab=readme-ov-file#2-when-i-am-doing-cyrealhover-hovering-state-is-not-resetting-after-my-checks

kyle-wilson commented 5 months ago

I have also encountered this issue, thank you for the recommended solution. Do you have any details about why the position of the mouse persists? In particular I am surprised it is not "reset" following cy.visit(). Thanks!