How to avoid anti-patterns in Cypress?

How to avoid anti-patterns in Cypress?

Hey Brett,

When you first start using Cypress, it is easy to fall into anti-patterns. They tend to be caused by a misunderstanding of how Cypress works and learning more about Cypress will help ameliorate these issues.

Cypress comes with a lot of out-of-the-box features that make it easy to use, but they can also lead inexperienced users astray. One example is “waiting” - Cypress waits for promises and XHR requests to resolve before moving on to the next command, but it does not wait for anything else that might cause your test to fail. We call this “implicit assertions”, and if you aren’t aware of this then your tests are going to “flakily pass”.

Anti-patterns are common solutions or patterns used by many people when they start using Cypress.

For example, not using data attributes to store data between multiple tests is an anti-pattern because it leads to maintenance issues and hard-to-read tests.

Here are some more examples:

  • Using the cy.wait() command with the default timeout value of 4 seconds

  • Using the cypress-axe library without customizing Axe rules

  • Not using viewport options for responsive testing (e.g., cy.viewport(1024, 768))

To deep dive into anti-patterns in Cypress, please go through the following article: