Cypress tests often become flaky when developers assume cy.wait('@alias') waits for every new request. It doesn’t. Aliases capture only the first match, so later waits may resolve instantly. The fix: re-intercept before each occurrence or use times: 1 to create one-shot intercepts that “consume” themselves. But the real solution is avoiding network waits altogether. Instead, rely on user-visible, accessible UI states (spinners, aria-busy, disabled buttons, status messages). This makes tests stable, realistic, and far more reliable than waiting on network events.Cypress tests often become flaky when developers assume cy.wait('@alias') waits for every new request. It doesn’t. Aliases capture only the first match, so later waits may resolve instantly. The fix: re-intercept before each occurrence or use times: 1 to create one-shot intercepts that “consume” themselves. But the real solution is avoiding network waits altogether. Instead, rely on user-visible, accessible UI states (spinners, aria-busy, disabled buttons, status messages). This makes tests stable, realistic, and far more reliable than waiting on network events.

Achieving Reliable E2E Tests in Cypress: Overcome cy.wait Pitfalls

2025/11/26 13:14

Cypress gives frontend engineers a superpower: the ability to write E2E tests that watch our app behave just like a real user would. But with great power comes… well, a lot of subtle flakiness if you’re not careful.

The cy.wait Illusion: What's Really Happening

The scenario is simple: you have a component that loads data, and after a user action, it loads new data using the same API endpoint. To ensure the new data has arrived, you intercept the request and then use cy.wait('@requestAlias') multiple times.

// A common, flawed approach: cy.intercept('GET', '/api/items/*', { fixture: 'item-1' }).as('getItems'); cy.visit('/items'); // 1. Wait for the initial load cy.wait('@getItems'); // ... User performs an action that triggers the SAME request ... // 2. Wait for the second load cy.wait('@getItems'); // <-- THIS IS THE PROBLEM

The Flaw

Cypress's cy.intercept logic is designed to capture a single match for an alias. When you call cy.wait('@getItems') for the first time, it finds the initial request, waits for its resolution, and then the alias is fulfilled.

When you call cy.wait('@getItems') a second time, Cypress does not reset the listener. Instead, it checks if a request has already been resolved with that alias. Because the first request has resolved, the second cy.wait command resolves immediately, without waiting for the new network call to finish. Your test is now racing against the network, not waiting for it.

Fix #1: Re-intercept before each expected request

(Works, explicit, but verbose)

cy.intercept('GET', '/api/items').as('getItems_1') cy.get('[data-testid=refresh]').click() cy.wait('@getItems_1') cy.intercept('GET', '/api/items').as('getItems_2') cy.get('[data-testid=load-more]').click() cy.wait('@getItems_2')

Clear, deterministic, but repetitive.

Fix #2: Use times: 1 to force Cypress to “consume” intercepts

(Cleaner: Cypress forgets the intercept after one match)

This is the missing tool many engineers don’t realize exists.

cy.intercept({ method: 'GET', pathname: '/api/items', times: 1 }).as('getItems') // trigger request 1 cy.get('[data-testid=refresh]').click() cy.wait('@getItems') cy.intercept({ method: 'GET', pathname: '/api/items', times: 1 }).as('getItems') // trigger request 2 cy.get('[data-testid=load-more]').click() cy.wait('@getItems')

Why this works:

  • times: 1 means Cypress removes the intercept after a single matching request
  • Re-declaring the intercept creates a fresh listener
  • Each cy.wait('@getItems') now truly waits for the next occurrence

This technique gives you explicit, occurrence-specific intercepts without alias clutter. For tests that must assert network behavior (payloads, headers, error flows), it’s a clean and robust pattern.

Fix #3: Stop waiting for requests altogether

(The best fix. UI > network.)

Here’s the golden rule:

That means the most stable tests assert what the user sees:

  • A loading spinner appears → disappears
  • A button becomes disabled → enabled
  • A success message appears when an action is complete.
  • The newly loaded element is now visible in the DOM.

Example with user-visible cues:

cy.get('[data-testid=refresh]').click() cy.get('[data-testid=spinner]').should('exist') cy.get('[data-testid=spinner]').should('not.exist') cy.get('[data-testid=item-list]') .children() .should('have.length.at.least', 1)

No reliance on internal network timing. No alias lifecycle. Zero flake.

Accessibility makes this even more robust

Accessible UI patterns make great Cypress hooks:

aria-busy attribute

<ul data-testid="item-list" aria-busy="true">

Test:

cy.get('[data-testid=item-list]').should('have.attr', 'aria-busy', 'false')

role="status" with live regions

<div role="status" aria-live="polite" data-testid="status"> Loading… </div>

Test:

cy.get('[data-testid=status]').should('contain', 'Loaded 10 items')

Disabled states for actions

cy.get('[data-testid=submit]').should('be.disabled') cy.get('[data-testid=submit]').should('not.be.disabled')

These patterns aid screen reader users and produce stable, deterministic E2E tests.

When waiting for requests is appropriate

There ARE valid scenarios:

  • Asserting payloads or query params
  • Mocking backend responses
  • Validating request ordering
  • Verifying retry logic
  • Testing error handling flows

For those cases: Combine times: 1 with explicit, fresh intercepts defined right before triggers.

For other cases: the test should rely on the UI state.

A combined real-world example

(Network + UI, the best of both worlds)

// UI-driven loading signal cy.get('[data-testid=create]').click() cy.get('[data-testid=spinner]').should('exist') // Network contract check cy.intercept({ method: 'POST', pathname: '/api/items', times: 1 }).as('postItem') cy.get('[data-testid=create]').click() cy.wait('@postItem') .its('request.body') .should('deep.include', { title: 'New item' }) // Final user-visible assertion cy.get('[data-testid=status]').should('contain', 'Item created')

The network part is accurate. The UI part is resilient. The test is rock-solid.

Final checklist

For accessible, deterministic, non-flaky Cypress tests

  • Prefer user-visible UI state, not network events
  • Use aria-busy, role="status", aria-live, and disabled states
  • When waiting for requests:
  • Re-intercept before each occurrence, OR
  • Use times: 1 to auto-expire the intercept
  • Avoid global, long-lived intercepts
  • Never assume cy.wait('@alias') waits “for the next request”
  • Make loading and completion states accessible (good for tests, good for users)

\

Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact service@support.mexc.com for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.

You May Also Like

Strive CEO Urges MSCI to Reconsider Bitcoin-Holding Firms’ Index Exclusion

Strive CEO Urges MSCI to Reconsider Bitcoin-Holding Firms’ Index Exclusion

The post Strive CEO Urges MSCI to Reconsider Bitcoin-Holding Firms’ Index Exclusion appeared on BitcoinEthereumNews.com. MSCI’s proposed Bitcoin exclusion would bar companies with over 50% digital asset holdings from indexes, potentially costing firms like Strategy $2.8 billion in inflows. Strive CEO Matt Cole urges MSCI to let the market decide, emphasizing Bitcoin holders’ roles in AI infrastructure and structured finance growth. Strive’s letter to MSCI argues exclusion limits passive investors’ access to high-growth sectors like AI and digital finance. Nasdaq-listed Strive, the 14th-largest Bitcoin treasury firm, highlights how miners are diversifying into AI power infrastructure. The 50% threshold is unworkable due to Bitcoin’s volatility, causing index flickering and higher costs; JPMorgan analysts estimate significant losses for affected firms. Discover MSCI Bitcoin exclusion proposal details and Strive’s pushback. Learn impacts on Bitcoin treasury firms and AI diversification. Stay informed on crypto index changes—read now for investment insights. What is the MSCI Bitcoin Exclusion Proposal? The MSCI Bitcoin exclusion proposal seeks to exclude companies from its indexes if digital asset holdings exceed 50% of total assets, aiming to reduce exposure to volatile cryptocurrencies in passive investment vehicles. This move targets major Bitcoin treasury holders like Strategy, potentially disrupting billions in investment flows. Strive Enterprises, a key player in the space, has formally opposed it through a letter to MSCI’s leadership. How Does the MSCI Bitcoin Exclusion Affect Bitcoin Treasury Firms? The proposal could deliver a substantial setback to Bitcoin treasury firms by limiting their inclusion in widely tracked MSCI indexes, which guide trillions in passive investments globally. According to JPMorgan analysts, Strategy alone might see a $2.8 billion drop in assets under management if excluded from the MSCI World Index, as reported in their recent market analysis. This exclusion would hinder these firms’ ability to attract institutional capital, forcing them to compete at a disadvantage against traditional finance entities. Strive CEO Matt Cole, in his letter to…
Share
BitcoinEthereumNews2025/12/06 11:33
Snowflake and Anthropic Forge $200M AI Partnership for Global Enterprises

Snowflake and Anthropic Forge $200M AI Partnership for Global Enterprises

The post Snowflake and Anthropic Forge $200M AI Partnership for Global Enterprises appeared on BitcoinEthereumNews.com. Peter Zhang Dec 04, 2025 16:52 Snowflake and Anthropic unveil a $200 million partnership to integrate AI capabilities into enterprise data environments, enhancing AI-driven insights with Claude models across leading cloud platforms. In a strategic move to enhance AI capabilities for global enterprises, Snowflake and Anthropic have announced a significant partnership valued at $200 million. This multi-year agreement aims to integrate Anthropic’s Claude models into Snowflake’s platform, offering advanced AI-driven insights to over 12,600 global customers through leading cloud services such as Amazon Bedrock, Google Cloud Vertex AI, and Microsoft Azure, according to Anthropic. Expanding AI Capabilities This collaboration marks a pivotal step in deploying AI agents across the world’s largest enterprises. By leveraging Claude’s advanced reasoning capabilities, Snowflake aims to enhance its internal operations and customer offerings. The partnership facilitates a joint go-to-market initiative, enabling enterprises to extract insights from both structured and unstructured data while adhering to stringent security standards. Internally, Snowflake has already been utilizing Claude models to boost developer productivity and innovation. The Claude-powered GTM AI Assistant, built on Snowflake Intelligence, empowers sales teams to centralize data and query it using natural language, thereby streamlining deal cycles. Innovative AI Solutions for Enterprises Thousands of Snowflake customers are processing trillions of Claude tokens monthly via Snowflake Cortex AI. The partnership’s next phase will focus on deploying AI agents capable of complex, multi-step analysis. These agents, powered by Claude’s reasoning and Snowflake’s governed data environment, allow business users to ask questions in plain English and receive accurate answers, achieving over 90% accuracy on complex text-to-SQL tasks based on internal benchmarks. This collaboration is especially beneficial for regulated industries like financial services, healthcare, and life sciences, enabling them to transition from pilot projects to full-scale production confidently. Industry Impact and Customer…
Share
BitcoinEthereumNews2025/12/06 11:17