{"id":3828,"date":"2020-11-23T09:22:59","date_gmt":"2020-11-23T07:22:59","guid":{"rendered":"https:\/\/betterqa.co\/?p=3828"},"modified":"2026-02-04T13:54:56","modified_gmt":"2026-02-04T11:54:56","slug":"how-to-use-code-smells-to-fix-flaky-cypress-tests","status":"publish","type":"post","link":"https:\/\/betterqa.co\/how-to-use-code-smells-to-fix-flaky-cypress-tests\/","title":{"rendered":"Using Code Smells to Fix Flaky Cypress Tests"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"3828\" class=\"elementor elementor-3828\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-3c34e1f elementor-section-full_width elementor-section-height-default elementor-section-height-default\" data-id=\"3c34e1f\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-76caf7a\" data-id=\"76caf7a\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-7db63bf elementor-widget elementor-widget-heading\" data-id=\"7db63bf\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">Introduction<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-0ad95eb elementor-section-full_width elementor-section-height-default elementor-section-height-default\" data-id=\"0ad95eb\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-cb3509d\" data-id=\"cb3509d\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-6046b7a elementor-widget elementor-widget-text-editor\" data-id=\"6046b7a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"334\" data-end=\"726\">If you\u2019ve worked with <strong data-start=\"356\" data-end=\"367\">Cypress<\/strong> for any length of time, you\u2019re probably familiar with the occasional frustration of flaky tests. These tests that seem to pass sometimes but fail at other times can be a real headache for developers and QA engineers alike. We recently attended a webinar hosted by <strong data-start=\"632\" data-end=\"643\">Cypress<\/strong> that explored how to use code smells to fix flaky tests, and it was an eye-opener.<\/p>\n<p data-start=\"728\" data-end=\"1067\">In this article, we\u2019ll walk you through the core concepts of <strong data-start=\"789\" data-end=\"804\">code smells<\/strong> in <strong data-start=\"808\" data-end=\"819\">Cypress<\/strong>, how they relate to flaky tests, and how you can use these insights to make your testing scripts more stable and reliable. We\u2019ll focus on three primary code smells that tend to lead to flaky tests and offer practical advice on how to address them.<\/p><div class=\"bqa-sources\">\n  <h3>Sources<\/h3>\n  <ol>\n    <li><a href=\"https:\/\/docs.cypress.io\/guides\/references\/best-practices\" rel=\"noopener nofollow\" target=\"_blank\">Cypress Best Practices Guide<\/a><\/li>\n    <li><a href=\"https:\/\/martinfowler.com\/bliki\/CodeSmell.html\" rel=\"noopener nofollow\" target=\"_blank\">Martin Fowler &#8211; Code Smell<\/a><\/li>\n    <li><a href=\"https:\/\/refactoring.guru\/refactoring\/smells\" rel=\"noopener nofollow\" target=\"_blank\">Refactoring Guru &#8211; Code Smells Catalog<\/a><\/li>\n  <\/ol>\n<\/div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-1719376 elementor-section-full_width elementor-section-height-default elementor-section-height-default\" data-id=\"1719376\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-96ba89e\" data-id=\"96ba89e\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-6dff38a elementor-widget elementor-widget-heading\" data-id=\"6dff38a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">What Are Code Smells?<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t<div class=\"elementor-element elementor-element-03a8049 e-con-full e-flex e-con e-parent\" data-id=\"03a8049\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-52ff8d5 elementor-widget elementor-widget-text-editor\" data-id=\"52ff8d5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"1097\" data-end=\"1530\">Before we dive into the details of fixing flaky <strong data-start=\"1145\" data-end=\"1156\">Cypress<\/strong> tests, it\u2019s important to understand the concept of <strong data-start=\"1208\" data-end=\"1223\">code smells<\/strong>. In the context of testing, <strong data-start=\"1252\" data-end=\"1267\">code smells<\/strong> refer to patterns or issues in your test scripts that signal potential problems, much like bad smells in your code might indicate deeper issues in the system. These <strong data-start=\"1433\" data-end=\"1448\">code smells<\/strong> often lead to unreliable tests that fail intermittently or behave inconsistently.<\/p>\n<p data-start=\"1532\" data-end=\"1729\">In <strong data-start=\"1535\" data-end=\"1546\">Cypress<\/strong>, flaky tests are often a symptom of underlying issues in your code. By identifying and addressing common <strong data-start=\"1652\" data-end=\"1667\">code smells<\/strong>, you can improve the stability and reliability of your tests.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-4ef7c62 e-con-full e-flex e-con e-parent\" data-id=\"4ef7c62\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-01520f5 elementor-widget elementor-widget-heading\" data-id=\"01520f5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">Best Practices to Prevent Flaky Cypress Tests<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-24fcbac e-con-full e-flex e-con e-parent\" data-id=\"24fcbac\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-189d3ac elementor-widget elementor-widget-text-editor\" data-id=\"189d3ac\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"6204\" data-end=\"6401\">While addressing <strong data-start=\"6221\" data-end=\"6236\">code smells<\/strong> is a key strategy for fixing flaky <strong data-start=\"6272\" data-end=\"6283\">Cypress<\/strong> tests, there are also several best practices you can follow to prevent flaky tests from occurring in the first place:<\/p>\n<ul data-start=\"6403\" data-end=\"7157\">\n<li data-start=\"6403\" data-end=\"6571\">\n<p data-start=\"6405\" data-end=\"6571\"><strong data-start=\"6405\" data-end=\"6457\">Use assertions and wait for the right conditions<\/strong>: Always use assertions like <code data-start=\"6486\" data-end=\"6499\">cy.should()<\/code> to ensure elements are in the right state before interacting with them.<\/p>\n<\/li>\n<li data-start=\"6575\" data-end=\"6751\">\n<p data-start=\"6577\" data-end=\"6751\"><strong data-start=\"6577\" data-end=\"6597\">Avoid hard waits<\/strong>: Instead of using <code data-start=\"6616\" data-end=\"6627\">cy.wait()<\/code>, prefer waiting for specific events or conditions, such as waiting for an element to appear or for an API call to complete.<\/p>\n<\/li>\n<li data-start=\"6753\" data-end=\"6926\">\n<p data-start=\"6755\" data-end=\"6926\"><strong data-start=\"6755\" data-end=\"6778\">Keep tests isolated<\/strong>: Each test should ideally be isolated, meaning it shouldn\u2019t depend on the outcome of another test. This helps reduce flakiness due to shared state.<\/p>\n<\/li>\n<li data-start=\"6928\" data-end=\"7157\">\n<p data-start=\"6930\" data-end=\"7157\"><strong data-start=\"6930\" data-end=\"6960\">Use Cypress best practices<\/strong>: Follow the best practices recommended by the <strong data-start=\"7007\" data-end=\"7018\">Cypress<\/strong> documentation, such as leveraging commands like <code data-start=\"7067\" data-end=\"7083\">cy.intercept()<\/code> for network requests and using <code data-start=\"7115\" data-end=\"7129\">cy.request()<\/code> to verify server responses.<\/p>\n<\/li>\n<\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-9d0e9dd e-con-full e-flex e-con e-parent\" data-id=\"9d0e9dd\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-ff78225 elementor-widget elementor-widget-heading\" data-id=\"ff78225\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">Conclusion<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-2cbb541 e-con-full e-flex e-con e-parent\" data-id=\"2cbb541\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-37e671d elementor-widget elementor-widget-text-editor\" data-id=\"37e671d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"7176\" data-end=\"7514\"><strong data-start=\"7176\" data-end=\"7187\">Cypress<\/strong> is an incredibly powerful tool for end-to-end testing, but like any testing framework, it can be susceptible to flaky tests if not used correctly. By identifying and fixing common <strong data-start=\"7368\" data-end=\"7391\">Cypress code smells<\/strong> like unprepared elements, flickering elements, and impatient tests, you can greatly improve the reliability of your tests.<\/p>\n<p data-start=\"7516\" data-end=\"7798\">With the right approach, <strong data-start=\"7541\" data-end=\"7552\">Cypress<\/strong> can help you write more stable, efficient, and maintainable tests. So, whether you&#8217;re just starting with <strong data-start=\"7658\" data-end=\"7669\">Cypress<\/strong> or you&#8217;re a seasoned pro, it\u2019s always worth revisiting your test scripts to look for <strong data-start=\"7755\" data-end=\"7770\">code smells<\/strong> and optimize your workflow.<\/p>\n<p data-start=\"7800\" data-end=\"8029\">If you want to learn more about fixing flaky tests and using <strong data-start=\"7861\" data-end=\"7872\">Cypress<\/strong> to its full potential, check out the <strong data-start=\"7910\" data-end=\"7921\">Cypress<\/strong> webinar and slides here: <a class=\"\" href=\"#\" target=\"_new\" rel=\"noopener\" data-start=\"7947\" data-end=\"8028\">Cypress Webinar: Code Smells<\/a>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-2cf5bc4 elementor-section-full_width elementor-section-height-default elementor-section-height-default\" data-id=\"2cf5bc4\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b9b9ccc\" data-id=\"b9b9ccc\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-2124bde elementor-widget-divider--view-line elementor-widget elementor-widget-global elementor-global-5706 elementor-widget-divider\" data-id=\"2124bde\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"divider.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-divider\">\n\t\t\t<span class=\"elementor-divider-separator\">\n\t\t\t\t\t\t<\/span>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f420ee4 elementor-widget-tablet__width-initial elementor-widget elementor-widget-global elementor-global-5702 elementor-widget-text-editor\" data-id=\"f420ee4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><strong>Stay Updated with the Latest in QA<\/strong><\/p>\n<p>The world of software testing and quality assurance is ever-evolving. To stay abreast of the latest methodologies, tools, and best practices, bookmark our blog. We\u2019re committed to providing in-depth insights, expert opinions, and trend analysis that can help you refine your software quality processes.<\/p>\n<p><a href=\"https:\/\/betterqa.co\/blog\/\" target=\"_new\">Visit our Blog<\/a><\/p>\n<p>Delve deeper into a range of specialized services we offer, tailored to meet the diverse needs of modern businesses. As well, hear what our clients have to say about us on <a href=\"https:\/\/clutch.co\/profile\/betterqa\" rel=\"nofollow noopener\" target=\"_blank\">Clutch!<\/a><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b620777 elementor-align-left elementor-widget elementor-widget-global elementor-global-5701 elementor-widget-button\" data-id=\"b620777\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"button.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div class=\"elementor-button-wrapper\">\n\t\t\t\t\t<a class=\"elementor-button elementor-button-link elementor-size-sm elementor-animation-grow\" href=\"https:\/\/betterqa.co\/software-testing-services\/#\">\n\t\t\t\t\t\t<span class=\"elementor-button-content-wrapper\">\n\t\t\t\t\t\t<span class=\"elementor-button-icon\">\n\t\t\t\t<svg aria-hidden=\"true\" class=\"e-font-icon-svg e-far-arrow-alt-circle-left\" viewBox=\"0 0 512 512\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><path d=\"M8 256c0 137 111 248 248 248s248-111 248-248S393 8 256 8 8 119 8 256zm448 0c0 110.5-89.5 200-200 200S56 366.5 56 256 145.5 56 256 56s200 89.5 200 200zm-72-20v40c0 6.6-5.4 12-12 12H256v67c0 10.7-12.9 16-20.5 8.5l-99-99c-4.7-4.7-4.7-12.3 0-17l99-99c7.6-7.6 20.5-2.2 20.5 8.5v67h116c6.6 0 12 5.4 12 12z\"><\/path><\/svg>\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t<span class=\"elementor-button-text\">See the other Services we provide<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Fix flaky Cypress tests by identifying code smells. Practical patterns for stable, reliable test automation in 2026.<\/p>\n","protected":false},"author":1,"featured_media":3900,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[84,1004],"tags":[749,752,751,750,741],"class_list":["post-3828","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation-testing","category-automation-techniques-and-challenges","tag-code","tag-codesmells","tag-commonmistakes","tag-flakytests","tag-testing"],"acf":[],"_links":{"self":[{"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/posts\/3828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/comments?post=3828"}],"version-history":[{"count":0,"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/posts\/3828\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/media\/3900"}],"wp:attachment":[{"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/media?parent=3828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/categories?post=3828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/betterqa.co\/wp-json\/wp\/v2\/tags?post=3828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}