Skip to content

Cypress can't intercept fetch requests with no leading slash #31215

@DeniDoman

Description

@DeniDoman

Current behavior

I have a react component containing the code:

const response = await fetch('.data/me');

My component tests has the intercept:

  it('shows name after successful fetch', () => {
    cy.intercept('GET', '**/.data/me', {
      statusCode: 200,
      body: { name: 'John' }
    }).as('getData1')

    cy.mount(<App />)
    cy.contains('Hello, John').should('be.visible')
  })

But the test failing in UI with the "Timed out retrying after 4000ms: Expected to find content: 'Hello, John' but never did." error, because Cypress failed to mock the request.

According to DevTools, it was successfully passed to http://localhost:5173/__cypress/iframes/.data/me and response contains some cypress internal code.

Desired behavior

I'd expect that fetches without leading slash should be mocked. I understand that it's a bad pattern, but some apps use it, and it may be impossible to change this logic on the app side.

Test code to reproduce

I created a project to reproduce the issue: https://github.com/DeniDoman/vite-react-starter

Component code: https://github.com/DeniDoman/vite-react-starter/blob/main/src/App.tsx
Test code: https://github.com/DeniDoman/vite-react-starter/blob/main/src/App.cy.tsx

Cypress Version

14.1.0

Node version

v18.19.1

Operating System

macOS 14.5 (23F79)

Metadata

Metadata

Assignees

No one assigned

    Labels

    stage: needs investigatingSomeone from Cypress needs to look at thisstaleno activity on this issue for a long period

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions